feat: add config option to run silently (no logs, no commands, but doing everything else needed for logging)

This commit is contained in:
sam 2024-10-25 16:18:27 +02:00
parent f7f88ff98f
commit 33b78a7ac5
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
5 changed files with 100 additions and 1 deletions

View file

@ -0,0 +1,78 @@
// Copyright (C) 2021-present sam (starshines.gay)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using Microsoft.Extensions.Options;
using Remora.Commands.Services;
using Remora.Commands.Tokenization;
using Remora.Commands.Trees;
using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.Commands.Responders;
using Remora.Discord.Commands.Services;
using Remora.Discord.Gateway.Responders;
using Remora.Results;
namespace Catalogger.Backend.Bot.Responders;
/// <summary>
/// Wrapper for Remora.Discord's default interaction responder, that ignores all events if test mode is enabled.
/// </summary>
public class CustomInteractionResponder(
Config config,
ILogger logger,
CommandService commandService,
IOptions<InteractionResponderOptions> options,
IDiscordRestInteractionAPI interactionAPI,
ExecutionEventCollectorService eventCollector,
IServiceProvider services,
ContextInjectionService contextInjection,
IOptions<TokenizerOptions> tokenizerOptions,
IOptions<TreeSearchOptions> treeSearchOptions,
ITreeNameResolver? treeNameResolver = null
) : IResponder<IInteractionCreate>
{
private readonly ILogger _logger = logger.ForContext<CustomInteractionResponder>();
private readonly InteractionResponder _inner =
new(
commandService,
options,
interactionAPI,
eventCollector,
services,
contextInjection,
tokenizerOptions,
treeSearchOptions,
treeNameResolver
);
public async Task<Result> RespondAsync(
IInteractionCreate gatewayEvent,
CancellationToken ct = default
)
{
if (config.Discord.TestMode)
{
_logger.Information(
"Not responding to interaction create event {InteractionId} in {ChannelId} as test mode is enabled",
gatewayEvent.ID,
gatewayEvent.Channel.Map(c => c.ID).OrDefault()
);
return Result.Success;
}
return await _inner.RespondAsync(gatewayEvent, ct);
}
}

View file

@ -57,6 +57,9 @@ public class Config
public string? SupportGuild { get; init; } public string? SupportGuild { get; init; }
public bool EnableDash { get; init; } = false; public bool EnableDash { get; init; } = false;
// If enabled, nothing will be logged.
public bool TestMode { get; init; } = false;
} }
public class WebConfig public class WebConfig

View file

@ -204,6 +204,11 @@ public static class StartupExtensions
var config = scope.ServiceProvider.GetRequiredService<Config>(); var config = scope.ServiceProvider.GetRequiredService<Config>();
var slashService = scope.ServiceProvider.GetRequiredService<SlashService>(); var slashService = scope.ServiceProvider.GetRequiredService<SlashService>();
if (config.Discord.TestMode)
logger.Warning(
"Catalogger is running in test mode. This means no logs will be sent and no commands will be responded to."
);
if (config.Discord.ApplicationId == 0) if (config.Discord.ApplicationId == 0)
{ {
logger.Warning( logger.Warning(

View file

@ -81,7 +81,11 @@ builder
] ]
); );
}) })
.AddDiscordCommands(enableSlash: true, useDefaultCommandResponder: false) .AddDiscordCommands(
enableSlash: true,
useDefaultCommandResponder: false,
useDefaultInteractionResponder: false
)
.AddCommandTree() .AddCommandTree()
// Start command tree // Start command tree
.WithCommandGroup<MetaCommands>() .WithCommandGroup<MetaCommands>()

View file

@ -97,6 +97,15 @@ public class WebhookExecutorService(
if (channelId == 0) if (channelId == 0)
return; return;
if (config.Discord.TestMode)
{
_logger.Information(
"Should have logged to {ChannelId}, but test mode is enabled, ignoring",
channelId
);
return;
}
var attachments = files var attachments = files
.Select<FileData, OneOf.OneOf<FileData, IPartialAttachment>>(f => f) .Select<FileData, OneOf.OneOf<FileData, IPartialAttachment>>(f => f)
.ToList(); .ToList();