如何让不和谐的机器人调用自己的命令
How to get a discord bot to call its own commands
所以我有一个机器人,我想让它调用我在代码中创建的命令。我试过 e.Channel.SendMessage("!command"),其中!是前缀,'command'是命令
private newCommand()
{
cmd.CreateCommand("command")
.Do(async (e) =>
{
// Some command stuff
}
}
就像,如果我或其他人键入 !command,命令会正常执行,但我怎样才能让机器人本身从代码中调用命令。
举个例子,我有这个代码:
discord.MessageReceived += async (s, e) =>
{
if (!e.Message.IsAuthor && e.Message.User.Id == blah)
{
await e.Channel.SendMessage("Yo man");
// how do I perform command??
await e.Channel.SendMessage("!command"); // doesn't do anything
}
};
有什么方法可以做到这一点,而无需将我的命令中的重复代码粘贴到 MessageReceived 部分?
扩大作用域,也就是将命令代码从 anon 函数中取出并在更高的作用域中定义它,以便您可以从两个地方调用它。
private newCommand()
{
cmd.CreateCommand("command")
.Do(async (e) =>
{
ExecuteCommand();
}
}
private void ExecuteCommand()
{
// some command stuff
}
然后,从您的其他方法调用它:
discord.MessageReceived += async (s, e) =>
{
if (!e.Message.IsAuthor && e.Message.User.Id == blah)
{
await e.Channel.SendMessage("Yo man");
// how do I perform command??
ExecuteCommand();
}
};
所以我有一个机器人,我想让它调用我在代码中创建的命令。我试过 e.Channel.SendMessage("!command"),其中!是前缀,'command'是命令
private newCommand()
{
cmd.CreateCommand("command")
.Do(async (e) =>
{
// Some command stuff
}
}
就像,如果我或其他人键入 !command,命令会正常执行,但我怎样才能让机器人本身从代码中调用命令。
举个例子,我有这个代码:
discord.MessageReceived += async (s, e) =>
{
if (!e.Message.IsAuthor && e.Message.User.Id == blah)
{
await e.Channel.SendMessage("Yo man");
// how do I perform command??
await e.Channel.SendMessage("!command"); // doesn't do anything
}
};
有什么方法可以做到这一点,而无需将我的命令中的重复代码粘贴到 MessageReceived 部分?
扩大作用域,也就是将命令代码从 anon 函数中取出并在更高的作用域中定义它,以便您可以从两个地方调用它。
private newCommand()
{
cmd.CreateCommand("command")
.Do(async (e) =>
{
ExecuteCommand();
}
}
private void ExecuteCommand()
{
// some command stuff
}
然后,从您的其他方法调用它:
discord.MessageReceived += async (s, e) =>
{
if (!e.Message.IsAuthor && e.Message.User.Id == blah)
{
await e.Channel.SendMessage("Yo man");
// how do I perform command??
ExecuteCommand();
}
};