FritzFurtz
FritzFurtz

Reputation: 89

Discord Bot [C#] does not execute the Command

I started writing a Discord bot, but i already did manage to run into a problem. I pretty much just wrote what he wrote with some minor changes that shouldn't affect the program too much. I have 2 Classes, the Main class which just gets the token for the bot and then creates the bot with

MyBot bot = MyBot(token)

Here is the MyBot.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;

namespace Coding_Bot
{
    class MyBot
    {
        DiscordClient discord;
        String botToken;

        public MyBot(String tempToken)
        {

            botToken = tempToken;
            discord = new DiscordClient(x =>
            {
                x.LogLevel = LogSeverity.Info;
                x.LogHandler = Log;
            });
            Console.WriteLine("[BOT] Connecting...");
            discord.ExecuteAndWait(async () =>
            {
                await discord.Connect(botToken, TokenType.Bot);
            });


            discord.UsingCommands(x =>
            {
                x.PrefixChar = '.';
                x.AllowMentionPrefix = true;
            });

            var commands = discord.GetService<CommandService>();

            commands.CreateCommand("info").Do(async (e) =>
            {
                Console.WriteLine("!info executed");
                await e.Channel.SendMessage("Coding Bot");
            });
        }

        private void Log(object sender, LogMessageEventArgs e)
        {
            Console.WriteLine("[BOT] " + e.Message);
        }
    }
}

It does connect and the Bot does come online. This is the output in my console:

[BOT] Connecting...
[BOT] Connected
[BOT] GUILD_AVAILABLE: BotTestServer

When i now type .info in #general nothing happens. Nothing in the console and nothing in #general. I already looked at this, but it didn't solve my problem

EDIT: I know i should use a CommandHandler class and not just put all my commands there. I will not do that in the future, but this was just for testing.

Upvotes: 4

Views: 3047

Answers (2)

Domen Jesenovec
Domen Jesenovec

Reputation: 89

I know this is stupid, but have you checked the bots posting permissions? (This happened to me.)

Upvotes: 2

WQYeo
WQYeo

Reputation: 4071

I copied down your code to one of my test bot and changed a little bit and my bot was able to connect, only that it did not respond to commands.
These are what I changed :

First change

MyBot bot = new MyBot(token); //instead of MyBot bot = MyBot(token);

I changed that because I got this error :enter image description here

Second change : The bot didn't respond to commands

Console.WriteLine("[BOT] Connecting...");
            discord.ExecuteAndWait(async () =>
            {
                await discord.Connect(botToken, TokenType.Bot);
            });

This above was placed in the wrong place of the code, so I moved it to underneath

commands.CreateCommand("info").Do(async (e) =>
        {
            Console.WriteLine("!info executed");
            await e.Channel.SendMessage("Coding Bot");
        });

So in the end it will be placed here :enter image description here

Other details : Check your .NET framework version, I was using 4.6.2 when testing this code, and the API Discord.NET version was at 0.9.6

If problem persists , you can seek help here : https://discord.gg/JBp6gSN

Slight note
Also, a newest version of Discord.NET came out recently , v1.0.0 . It involves major changes from v1 to v0.9.6 , making everything run based on async. If this problem gives you a lot of headache, you can easily escape the situation by using Discord.NET v1 instead.

Upvotes: 3

Related Questions