My discord bot cannot recieve messages from text channel on server

106 Views Asked by At

I'm writing my first Discord bot and I'm having a problem. The bot doesn't recognize messages sent in a server text channel, but it works perfectly fine in private messages. No matter what I write in a private message to the bot, it identifies each message, but if I write in a server text channel, it doesn't identify anything, it just returns an empty string. I can't even guess what the problem is, I think I need to change something in InstallCommandsAsync but I don't know what. I'll attach screenshots of the console output when sending messages to the bot in private messages and in a text channel so you can better understand the problem. I also searched for a solution online but I found little information for Discord.NET. I ask you to give me advice, help me solve the problem, or a direction in which I need to move to solve this problem.

using System.Reflection;
using System.Threading.Channels;
using Discord;
using Discord.Commands;
using Discord.WebSocket;

namespace GemniBot;



public class CommandHandler
{
    private readonly DiscordSocketClient _client;
    private readonly CommandService _commands;
    private string _textChannelPrefix = "!"; 

    public void SetTextChannelPrefix(string prefix)
    {
        _textChannelPrefix = prefix;
    }

    public CommandHandler(DiscordSocketClient client, CommandService commands)
    {
        _commands = commands;
        _client = client;
    }
    
    public async Task InstallCommandsAsync()
    {
        // Hook the MessageReceived event into our command handler
        _client.MessageReceived += HandleCommandAsync;
        
        await _commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(),  services: null);
    }

    private async Task HandleCommandAsync(SocketMessage messageParam)
    {

        var message = messageParam as SocketUserMessage;
        Console.WriteLine($"Message chanel: {message.Channel}");
        Console.WriteLine($"Message: {message}");
        if (message == null) return;
        
        int argPos = 0;
        
        if (!(message.HasStringPrefix(_textChannelPrefix, ref argPos) || 
              message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
            message.Author.IsBot)
            return;


        var context = new SocketCommandContext(_client, message);

        await _commands.ExecuteAsync(
            context: context, 
            argPos: argPos,
            services: null);
    }
}

public class InfoModule : ModuleBase<SocketCommandContext>
{

    [Command("say")]
    [Summary("Echoes a message.")]
    public Task SayAsync([Remainder] [Summary("The text to echo")] string echo)
    {

        ReplyAsync(echo);
        Console.WriteLine($"Sent message: {echo}");
        return Task.CompletedTask;
    }
    
}

Output when i text in personal messages: enter image description here Output when i text in Text channel on the server(Bot is admin on this server and this server is completely new: enter image description here

I may have problems formulating my thoughts, but I couldn't find similar problems on discord.net or any solutions. I tried to use artificial intelligence (ChatGPT, Google Bard) to find a solution but nothing. I expect that when I solve the problem, I will be able to process messages from all text channels that the bot has access to. I would be very grateful for your help! Thank you in advance.

0

There are 0 best solutions below