#Command Content Is Empty

1 messages · Page 1 of 1 (latest)

warm shale
#

I decided to go back to a bot project that I was working on in 2022. I upgraded it from .NetFramework to .Net Core 7.0, and it builds. (somehow)

I've run into an issue though. When I send a command, my bot receives the event, but the message content is empty (As shown in the image). The DSharp+ version being used previously was 4.2.0.0 (According to the CSProj), I'm now using 4.4.3.

This is the method that handles commands once they're sent (Gotten from some YouTube tutorial):

    private async Task CommandHandler(DiscordClient client, MessageCreateEventArgs e)
    {
        var cnext = client.GetCommandsNext();
        var msg = e.Message;

        var cmdStart = msg.GetStringPrefixLength("!");

        if (cmdStart == -1)
            cmdStart = msg.GetStringPrefixLength("/");

        if (cmdStart == -1)
            return;

        var cmdString = msg.Content[cmdStart..];
        var command = cnext.FindCommand(cmdString, out var args);

        if (command is null)
        {
            Logging.LogError($"\n[CB] Command \n<\n  Author  : {msg.Author}\n  Message : {msg.Content}\n " +
                $" Channel : {msg.Channel}\n  Guild   : {msg.Channel.Guild.Name} ({msg.Channel.Guild.Id})\n>" +
                $"\nFailed execution => Command not found");

            Logging.Send($"Could not find a command by the name of '{cmdString.Split()[0]}'", e.Channel.Id);
        }
        else
            Logging.Log($"\n[CB] Command found \n<\n  Author  : {msg.Author}\n  Message : {msg.Content}\n " +
                $" Channel : {msg.Channel}\n  Guild   : {msg.Channel.Guild.Name} ({msg.Channel.Guild.Id})\n>\n");
    }

Congrats if you managed to get through this code without having a seizure, cause I sure did.

Is there something wrong with this code, did I set it up wrong somewhere else?
I'll gladly give more code/information if needed.

Thanks in advance!

balmy tokenBOT
#
Automated suggestion: You don't have the Message Content Intent enabled

As of September 1st 2022, Discord started requiring message content intent for bots that want to read message content. This is a privileged intent!

If your bot has under 100 guilds, all you have to do is flip the switch in the developer dashboard (over at https://discord.com/developers/applications, also see the image) and then specifying the intent in your DiscordConfiguration (see the code example)
If your bot has over 100 guilds, you'll need approval from Discord's end.

Even though you can just use this to get away with it, it is recommended by Discord to use Slash Commands whenever possible, so look into that if you can (Slash Commands Documentation)

More info:
https://support-dev.discord.com/hc/en-us/articles/4404772028055-Message-Content-Privileged-Intent-FAQ

Code Example:

DiscordConfiguration config = new() {
    // ...
    // If you do not set this, your bot will fail to start with the error code: "Disallowed intent(s)"
    Intents = DiscordIntents.AllUnprivileged | DiscordIntents.MessageContents,
    // ...
}
warm shale
#

Dumbest thing ever