本文介绍了Discord Bot(使用C#)不执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Discord Bot.它是用C#开发的.我的命令列表已填写,命令值接收到此列表.但是该命令在调用时不会执行代码.

I wrote a Discord Bot. It's developed with C#. My command list is filled, the command value receives this list. But the command does not execute the code when calling it.

我的前缀字符为'!'然后是命令.我的基类看起来像这样:

My prefix char is '!' followed by the command. My base class looks this:

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是相关的.

I devided the code into two classes, the eventController and the commandController. The commandController is the relevant one.

我的命令类如下:

 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不会回复.

So when I call a command like "test0" or "UseHelp()" the command receives the string content. All 5 commands are listed to the bot. But when i use a command in Discord the Bot does not reply.

已连接并填充了命令"数据...

It is connected and the "command" data is filled...

推荐答案

首先,看看这个:

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()前面的参数中.

In discord.net , when you make a PrefixChar already, the PrefixChar will always appear inside the argument of command.CreateCommand() at the front by default. So hence there is no need to place another '!' inside. If you do that, you have to call a command by using !!test0 . Simply, treat it as the system has automatically added the prefix in the argument on command.CreateCommand() automatically at the front.

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

To fix it : simply remove char argument '!' at the front in command.CreateCommand('!' + tuple.Item1). Test the bot by calling !test0 or something, it should work.

这篇关于Discord Bot(使用C#)不执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:52