我写了一篇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;
}
所以当我打电话给&#34; test0&#34;或&#34; UseHelp()&#34;该命令接收字符串内容。所有5个命令都列在机器人中。但是当我在Discord中使用命令时,Bot没有回复。
它是连接的&#34;命令&#34;数据已填满......
答案 0 :(得分:0)
首先,看看这个:
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
或其他东西测试机器人,它应该可以工作。