如何让discord bot调用自己的命令

所以我有一个机器人,我想让它调用我在代码中创建的命令 . 我试过e.Channel.SendMessage(“!command”),在哪里!是前缀,'command'是命令

private newCommand()
{
    cmd.CreateCommand("command")
        .Do(async (e) =>
        {
        // 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??
                await e.Channel.SendMessage("!command"); // doesn't do anything
            }
        };

我有没有办法在没有粘贴到MessageReceived部分的命令重复代码的情况下执行此操作?

回答(1)

2 years ago

扩展范围,也就是说,从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();
    }
};