【问题标题】:Discord Bot ( using C# ) does not execute the commandDiscord Bot(使用 C#)不执行命令
【发布时间】:2017-06-04 11:54:20
【问题描述】:

我写了一个 Discord Bot。它是用 C# 开发的。我的命令列表被填满了,命令值接收到这个列表。但是该命令在调用时并没有执行代码。

我的前缀字符是 '!'接着是命令。我的基类是这样的:

public class Bot
    {
        string token = "#######################"; // my Token

        CommandService command; // The commands holder
        EventController eventController = new EventController(); // event class
        CommandController commandController = new CommandController(); // commands class

        public Bot()
        {
            var client = new DiscordClient(); // my client

            client = new DiscordClient(input =>
            {
                input.LogLevel = LogSeverity.Info;
                input.LogHandler = Log;
            });

            client.UsingCommands(input =>
            {
                input.PrefixChar = '!';              // the prefix char to call commands
                input.AllowMentionPrefix = true;
            });

            eventController.HandleEvents(client); // reference to events

            command = client.GetService<CommandService>();

            commandController.HandleCommands(command, client); // reference to commands

            client.ExecuteAndWait(async() =>
            {
                while (true)
                {
                    await client.Connect(token, TokenType.Bot);
                    break;
                }
            });
        }

        private void Log(object sender, LogMessageEventArgs e)
        {
            Console.WriteLine(e.Message);
        }

我将代码分为两个类,eventController 和 commandController。 commandController 是相关的。

我的命令类如下所示:

 private List<Tuple<string, string, string>> commandList = new List<Tuple<string, string, string>>(); // the List holding all commands

        public void HandleCommands(CommandService command, DiscordClient client)
        {
            FillCommandList(); // Fill the List with the commands

            foreach (Tuple<string, string, string> tuple in commandList)
            {
                command.CreateCommand('!' + tuple.Item1).Do(async (e) =>
                {
                    await e.Channel.SendMessage(tuple.Item2); // Create all commands from the List
                });
            }
        }

        private void Add(string commandName, string textToReturn, string commandDescription)
        {
            commandList.Add(new Tuple<string, string, string>(commandName, textToReturn, commandDescription)); // Method to lower the mass of code
        }

        private void FillCommandList()
        {
            Add("test0", "success0", "info0"); // commands for testing
            Add("test1", "success1", "info1");
            Add("test2", "success2", "info2");
            Add("test3", "success3", "info3");

            Add("help", UseHelp(), "List all Commands"); // call the help
        }

        private string UseHelp()
        {
            string commandItems = "";
            foreach (Tuple<string, string, string> tuple in commandList)
            {
                commandItems += "- !" + tuple.Item1 + " - " + tuple.Item3 + "\r\n"; // List all commands
            }
            return commandItems;
        }

因此,当我调用“test0”或“UseHelp()”之类的命令时,该命令会接收字符串内容。所有 5 个命令都列出给机器人。但是当我在 Discord 中使用命令时,Bot 没有回复。

已连接,“命令”数据已填充...

【问题讨论】:

    标签: c# bots discord


    【解决方案1】:

    首先,看看这个:

    client.UsingCommands(input =>
                {
                    input.PrefixChar = '!';              // the prefix char to call commands
                    input.AllowMentionPrefix = true;
                });
    

    现在,这个: command.CreateCommand('!' + tuple.Item1)

    在 discord.net 中,当你已经创建了 PrefixChar 时,PrefixChar 默认总是出现在前面command.CreateCommand() 的参数中。因此,无需在内部放置另一个 '!'。如果这样做,则必须使用 !!test0 调用命令。简单来说就是系统自动在command.CreateCommand()的参数前面自动添加了前缀。

    要修复它:只需删除 command.CreateCommand('!' + tuple.Item1) 前面的 char 参数 '!'。通过调用!test0 或其他方式测试机器人,它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2020-10-30
      • 2018-09-12
      • 2020-09-03
      • 2021-01-06
      • 2021-04-07
      • 2020-12-15
      • 2021-08-28
      相关资源
      最近更新 更多