#TaskCanceledException, Command code not executing

1 messages · Page 1 of 1 (latest)

radiant fractal
#

I'm attempting to build a discord bot using Commands, and while the commands are registering (I can see them in autocomplete), I'm seeing this exception rather regularly:

System.Threading.Tasks.TaskCanceledException: A task was canceled.
  at DSharpPlus.Net.Gateway.GatewayClient.HandleEventsAsync(CancellationToken ct) : A task was canceled.
  at DSharpPlus.Net.Gateway.GatewayClient.HandleEventsAsync(CancellationToken ct)```
I don't see any other errors in the logs, but attempting to use my commands results in "the application did not respond", and setting a breakpoint in the command results in the breakpoint not being hit, making me think this error is preventing the bot from receiving commands.
#
        {
            string token = ConfigHelper.Current.Token;
            DiscordClientBuilder builder = DiscordClientBuilder.CreateDefault(token, DiscordIntents.AllUnprivileged | DiscordIntents.MessageContents);

            builder.UseCommands(
                extension =>
                {
                    extension.AddCommands([typeof(Commands.RegistrationCommands)]);
                },
                new CommandsConfiguration()
                {
                    DebugGuildId = 0,
                    RegisterDefaultCommandProcessors = true,
                    UseDefaultCommandErrorHandler = false
                });

            DiscordClient client = builder.Build();

            DiscordActivity status = new DiscordActivity("with itself", DiscordActivityType.Playing);

            await client.ConnectAsync(status, DiscordUserStatus.Online);

            while (!stoppingToken.IsCancellationRequested)
            {
                await Task.Delay(5000, stoppingToken);
            }
        }```
#
    class RegistrationCommands
    {
        [Command("add")]
        public static ValueTask AddAsync(CommandContext context)
        {
            return context.RespondAsync("Still not implemented");
        }

        [Command("remove")]
        public static ValueTask RemoveAsync(CommandContext context)
        {
            return context.RespondAsync("This isn't implemented either");
        }
    }```
#

I don't know what I'm missing or getting wrong, but maybe someone else can spot it?

dire flower
#

What does your program.cs look like?

radiant fractal
#
builder.Services.AddHostedService<Worker>();

IConfiguration configuration = builder.Configuration;

ConfigHelper.Current = new ConfigHelper(configKey => configuration.GetValue<string>(configKey));

var host = builder.Build();
host.Run();
dire flower
#

Its likely not fixing your bug but in this case where you have a generic host setup please use the methods on the service collection to build the client instead of using the builder

radiant fractal
#

I rewrote the application to use the service collector to build the client, and got the same results (as expected), but along the way noticed something interesting: if I remove DiscordIntents.MessageContents when setting up the discord client, the error goes away, and my bot works as expected (with slash commands).

dire flower
#

Oh do you enabled that intent in the dashboard? Its a privileged intent you have to request

radiant fractal
#

They make the dashboard nice and confusing to follow; is that the permissions I select when I created the discord invite link?

dire flower
#

Its under the bot section

radiant fractal
#

Ah! I missed that one; that would be the cause!

dire flower
#

I think it's another instance of ##2046