Discord Bot (using C #) doesn't execute command

I wrote a conversation. It is developed using C #. My list of commands is populated, the value of the command gets this list. But the command does not execute the code when it is called.

My char prefix is ​​'!' followed by a team. My base class looks like 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);
        }

I divided the code into two classes: eventController and commandController. The command controller is relevant.

My class of commands is as follows:

 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;
        }

Therefore, when I call a command like "test0" or "UseHelp ()", the command gets the string contents. All 5 teams are listed to the bot. But when I use the command in Discord, Bot is not responding.

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

0
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() .

: char '!' command.CreateCommand('!' + tuple.Item1). , !test0 - , .

0

Source: https://habr.com/ru/post/1668855/


All Articles