C# Discord bot failing to retrieve message content from server channels

I’m struggling with my C# Discord bot using Discord.NET. It works fine in DMs but has issues in server channels. Here’s what’s happening:

The bot can see who sent a message and where, but it can’t read the content in server channels. I’ve double-checked and the Message Content Intent is on in the Dev Portal.

Here’s a simplified version of my code:

async Task MessageReceived(SocketMessage msg)
{
    if (msg.Author.Id != botId)
    {
        Console.WriteLine($"{msg.Author} in {msg.Channel}: {msg.Content}");
        await ReplyToMessage(msg.Channel, msg);
    }
}

async Task ReplyToMessage(ISocketMessageChannel channel, SocketMessage msg)
{
    await channel.SendMessageAsync($"Got message from {msg.Author} in {msg.Channel}. Content: {msg.Content}");
}

In DMs, it works perfectly. But in servers, the Content part is always empty. I’ve tried creating a new bot, but the problem persists.

I’ve set up the Gateway intent and included GatewayIntent.MessageContent in my DiscordSocketConfig. The bot can read its own messages, which I guess is normal.

Any ideas what I’m doing wrong? Thanks for any help!

Have you checked your bot’s OAuth2 scopes when inviting it to servers? This is a common oversight. Make sure you’ve included the ‘bot’ and ‘applications.commands’ scopes, as well as the necessary bot permissions.

Another thing to consider is rate limiting. Discord has strict rate limits, especially for reading message content. Try implementing a short delay between message reads or use a queue system to manage requests.

If you’re still having issues, it might be worth logging the raw payload received from Discord to see what data you’re actually getting. This can help pinpoint if it’s a permission issue or something else entirely.

Lastly, ensure your bot’s token hasn’t been compromised. If it has, regenerate it and update your code accordingly.

I faced a similar issue before. The first thing to check is whether you’re setting up your DiscordSocketClient with the correct intents. Ensure that your configuration includes both all unprivileged intents and the MessageContent intent:

var config = new DiscordSocketConfig {
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent
};
var client = new DiscordSocketClient(config);

Also, confirm that you’re calling LoginAsync with the proper overload that accepts a TokenType parameter:

await client.LoginAsync(TokenType.Bot, “your-bot-token-here”);

If the problem persists, consider removing the bot from the server and re-inviting it with all the right scopes. This can sometimes resolve permission issues that are not immediately obvious.

I hope this helps resolve the content retrieval issue.

hey there, i had a similar issue. make sure ur using the latest discord.net version (3.x+). also, double check ur bot’s role permissions in the server. sometimes it needs ‘read message history’ to grab content. if that doesn’t work, try re-inviting the bot with the correct scopes. good luck!