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);
}
I devided the code into two classes, the eventController and the commandController. The commandController is the relevant one.
My command class looks this:
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;
}
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...
First, take a look at this :
and now , this :
command.CreateCommand('!' + tuple.Item1)
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 oncommand.CreateCommand()
automatically at the front.To fix it : simply remove char argument
'!'
at the front incommand.CreateCommand('!' + tuple.Item1)
. Test the bot by calling!test0
or something, it should work.