Search code examples
c#discordbotsdiscord.net

Discord.Net Received Message Content always Empty


I'm trying to make A Discord bot but all Messages that the Bot receives are empty for some reason.

I guess it has something to do with the permissions in Discord (cause the slash commands are also not working) but I just can't figure it out.

I added the bot to my Discord Sever with Admin permissions. The code should be right cause I already programemd a bot a Year ago (which worked and still works) and only copied it but neverless here's my code (summarised cause there's more stuff thats irrelevant to this problem):

public async Task StartAsync()
{
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("config.json", false, true)
        .Build();

    using IHost host = Host.CreateDefaultBuilder()
        .ConfigureServices((_, services) => services
        .AddSingleton(config)
        .AddSingleton(x => new DiscordSocketClient(new DiscordSocketConfig
        {
            GatewayIntents = GatewayIntents.AllUnprivileged,
            AlwaysDownloadUsers = true,
        }))
        .AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordSocketClient>()))
        .AddSingleton<InteractionHandler>()
        .AddSingleton(x => new CommandService(new CommandServiceConfig
        {
            LogLevel = LogSeverity.Debug,
            DefaultRunMode = Discord.Commands.RunMode.Async
        }))
        .AddSingleton<PrefixHandler>())
        .Build();

    await RunAsync(host);
}
public async Task RunAsync(IHost host)
{
    using IServiceScope servicScope = host.Services.CreateScope();
    IServiceProvider provider = servicScope.ServiceProvider;

    var sCommands = provider.GetRequiredService<InteractionService>();
    _client = provider.GetRequiredService<DiscordSocketClient>();
    var config = provider.GetRequiredService<IConfigurationRoot>();

    await provider.GetRequiredService<InteractionHandler>().InitializeAsync(); //For Slash commands

    var pCommands = provider.GetRequiredService<PrefixHandler>();
    pCommands.AddModule<PrefixModule>();
    await pCommands.InitializeAsync();

    _client.Ready += async () =>
    {
        await sCommands.RegisterCommandsToGuildAsync(UInt64.Parse(config["testGuild"]));
    };
    await _client.LoginAsync(TokenType.Bot, config["token"]);
    await _client.StartAsync();

    await Task.Delay(-1);
}

And here the PrefixHandler that Handles normal Commands:

internal sealed class PrefixHandler
{
    readonly DiscordSocketClient _client;
    readonly CommandService _commands;
    readonly IConfigurationRoot _config;

    public PrefixHandler(DiscordSocketClient client, CommandService commands, IConfigurationRoot config)
    {
        _client = client;
        _commands = commands;
        _config = config;
    }

    public async Task InitializeAsync()
    {
        _client.MessageReceived += HandleCommandAsync;
    }

    public void AddModule<T>()
    {
        _commands.AddModuleAsync<T>(null);
    }

    async Task HandleCommandAsync(SocketMessage messageParam)
    {
        Console.WriteLine("Message: '" + messageParam.Content + "'"); //Always Prints Empty ''
    }
}

If I forgot anything or if I should add something feel free to let me know, so I can add it. I hope the problem gets resolved and Thanks in advance


Solution

  • If you haven't already, enable "Message Content Intent" in the Discord developer portal and add the MessageContent intent to the GatewayIntents property in your DiscordSocketClient constructor.

    GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent