DSharpPlus Command-Registration mit Sharding?

Moin Moin.

Ich habe Sharding implementiert und wollte meine Slash-Commands zu Testzwecken erstmal auf einer speziellen Guilde registrieren.

Das Problem ist, dass sich die Commands nur global registrieren lassen. (Sie werden zwar in Discord angezeigt, werfen beim Ausführen allerdings einen Fehler "Error while executing command: A slash command was executed, but no command was registered for it.")

Hier der wesentliche Code:

public class BotBackgroundService : BackgroundService
{
    private readonly InteractionManager _interactionManager;
    private readonly DiscordShardedClient _client;
    private IReadOnlyDictionary<int, SlashCommandsExtension> _slashCommands;
    private readonly DiscordConfig _config;
    private readonly Logger _logger;
    private readonly IServiceScopeFactory _scopeFactory;
    private bool _isRunning => _executingTask != null && !_executingTask.IsCompleted;
    public bool IsRunning
    {
        get => _isRunning;
        private set { }
    }


    private CancellationTokenSource _internalCancellationTokenSource;
    private Task _executingTask;


    public BotBackgroundService(IOptions<DiscordConfig> config, DiscordShardedClient discordClient, Logger logger, IServiceProvider serviceProvider, IServiceScopeFactory scopeFactory)
    {
        _config = config.Value;
        _client = discordClient;
        _interactionManager = new InteractionManager();
        Setup(serviceProvider).Wait();
        _logger = logger;
        _internalCancellationTokenSource = new CancellationTokenSource();
        _scopeFactory = scopeFactory;
    }
    public async Task Setup(IServiceProvider serviceProvider)
    {
        _slashCommands = await _client.UseSlashCommandsAsync(new SlashCommandsConfiguration
        {
            Services = serviceProvider
        });
    }


    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        _executingTask = RunBotAsync(_internalCancellationTokenSource.Token);


        return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
    }


    private async Task RunBotAsync(CancellationToken internalToken)
    {
        await _logger.Log("Discord Bot is starting...", Logger.LogPriority.Info);




        foreach (var cmd in _slashCommands.Values)
        {
            cmd.RegisterCommands<AdminCommands>(_config.MainServerId);
            cmd.RegisterCommands<BaseCommands>(_config.MainServerId);


            cmd.SlashCommandErrored += async (s, e) =>
            {
                if (e.Exception is SlashExecutionChecksFailedException slex)
                {
                    foreach (var check in slex.FailedChecks)
                        if (check is OnlyTeam att)
                            await e.Context.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent($"Only team members can run this command!").AsEphemeral());
                }


                await _logger.Log($"Error while executing command: {e.Exception.Message}", Logger.LogPriority.Error);
            };
        }


        await _client.StartAsync();


        try
        {
            await Task.Delay(-1, internalToken);
        }
        catch (TaskCanceledException)
        {
            await _logger.Log("Discord Bot internal shutdown.", Logger.LogPriority.Info);
        }
        finally
        {
            await _client.StopAsync();
        }
    }


    public override async Task StopAsync(CancellationToken stoppingToken)
    {
        if (_executingTask == null) return;


        _internalCancellationTokenSource.Cancel();


        await Task.WhenAny(_executingTask, Task.Delay(-1, stoppingToken));


        await base.StopAsync(stoppingToken);
    }
}

Die Ressourcen zu DSharpPlus sind dahingehend etwas beschränkt. Habe dazu leider nicht viel gefunden.

Ich habe schon probiert, die Commands auf die Shards zu registrieren. Hat leider nicht funktioniert.

C Sharp, Code, Visual Studio, Discord, Discord Bot

Meistgelesene Beiträge zum Thema Visual Studio