【问题标题】:How to get a discord bot to call its own commands如何让不和谐的机器人调用自己的命令
【发布时间】:2017-12-25 01:05:11
【问题描述】:

所以我有一个机器人,我希望它调用我在代码中创建的命令。我试过 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 部分?

【问题讨论】:

    标签: c# bots discord


    【解决方案1】:

    扩大范围,也就是将命令代码从匿名函数中取出,并在更高的范围内定义它,这样你就可以从两个地方调用它。

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

    【讨论】:

    • 啊哈!是的,这很有意义。而不是在不和谐的不可调用函数中定义它......在可调用的函数中定义东西...... :')
    • 是的,有时是小事让我们挂了。坚持下去。
    猜你喜欢
    • 2018-11-10
    • 2021-11-15
    • 2018-07-21
    • 2021-12-07
    • 2020-11-10
    • 2020-06-22
    • 2021-08-13
    • 2017-05-31
    • 2021-10-21
    相关资源
    最近更新 更多