Search code examples
c#commanddiscorddiscord.net

c# Discord Bot Won't Take Commands During Command Delay


I've been messing around with Discord bots and I've been trying to make my own, however, I've come across an issue. One of the bots' commands says something, waits a minute, then deletes its message. The issue is not with that command, however with executing other commands. When you execute that command and are in that one minute waiting period and try to execute another command, the bot does not execute that second command. It will only execute it once that one minute waiting period is over.

Here's the code to the command:

    [Command("test")]
    public async Task PlayAsync()
    {
        string text = "Message To Delete"; //text that bot shows
        var m = await ReplyAsync(text); //bot displays text
        const int delay = 10000; //delay to wait
        await Task.Delay(delay); //starting delay
        await Context.Message.DeleteAsync(); //deleting messages after delay
        await m.DeleteAsync();
    }

Now when you execute that command and try to execute another one, that second command isn't received/executed by the bot. I'm not sure why this is as they are two different commands, if anyone knows anything about this, I'll take anything.


Solution

  • [Command("test", RunMode = RunMode.Async)]
    public async Task PlayAsync()
    {
        string text = "Message To Delete"; //text that bot shows
        var m = await ReplyAsync(text); //bot displays text
        const int delay = 10000; //delay to wait
        await Task.Delay(delay); //starting delay
        await Context.Message.DeleteAsync(); //deleting messages after delay
        await m.DeleteAsync();
    }
    

    Although PlayAsync is being run asynchronously, the command is still running synchronously. Adding Runmode = RunMode.Async should do the trick.