0

我写了一个 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 没有回复。

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

4

1 回答 1

0

首先,看看这个:

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

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

在 discord.net 中,当您已经制作 PrefixChar 时command.CreateCommand(),默认情况下 PrefixChar 将始终出现在最前面的参数中。因此,无需在'!'内部放置另一个。如果这样做,则必须使用!!test0. command.CreateCommand()简单来说,就当系统自动在最前面的参数上自动添加了前缀。

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

于 2017-01-20T13:41:23.203 回答