Discord Bot [C#]不执行命令

我开始写一个Discord机器人,但我已经设法遇到问题.我几乎只是编写了he所写的内容,并进行了一些不会对程序造成太大影响的微小更改.我有2个类,Main类只获取机器人的令牌,然后创建机器人

MyBot bot = MyBot(token)

这是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);
        }
    }
}

它确实连接,Bot确实上线了.这是我的控制台中的输出:

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

当我现在在#general中键入.info时没有任何反应.控制台中没有任何内容,#general中没有任何内容.
我已经看了this,但它没有解决我的问题

编辑:我知道我应该使用CommandHandler类而不只是将所有命令放在那里.我不会在将来这样做,但这只是为了测试.

解决方法:

我将你的代码复制到我的一个测试机器人并稍微更改了一下,我的机器人能够连接,只是它没有响应命令.
这些是我改变的:

第一次改变

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

我改变了,因为我收到了这个错误:Discord Bot [C#]不执行命令

第二个变化:机器人没有响应命令

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

上面这个放在代码的错误位置,所以我把它移到了下面

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

所以最后它将放在这里:Discord Bot [C#]不执行命令

其他细节:检查您的.NET框架版本,我在测试此代码时使用4.6.2,并且API Discord.NET版本为0.9.6

如果问题仍然存在,您可以在此处寻求帮助:https://discord.gg/JBp6gSN

轻微的注意
最近,最新版本的Discord.NET出版了,v1.0.0.它涉及从v1到v0.9.6的重大更改,使所有内容都基于异步运行.如果这个问题给你带来很多麻烦,你可以通过使用Discord.NET v1轻松逃脱这种情况.

上一篇:解决不*就能连上discord,或者discord一直处于更新无法进入客户端程序


下一篇:Understanding Binomial Confidence Intervals 二项分布的置信区间