如果命令A成功执行,命令B才能执行

时间:2020-10-08 17:07:30

标签: discord.js

我正在尝试制作一个智能问题机器人,我想知道是否存在一种方法,如果先执行命令A,则允许执行命令B

    } else if(message.content.match(/Discord/gi)){
    const Embed = new Discord.MessageEmbed()
}

这将查找包含不和谐的消息(小写或大写) 我不希望该漫游器查找包含Discord的每条消息。 我希望只有先执行前面的命令才能执行,然后再禁用它,听起来很复杂,但是可能

1 个答案:

答案 0 :(得分:1)

假设我正确理解了您的意图,则只想在调用命令A后再查找命令B,然后一旦执行了命令B,就想停止寻找它。这可以通过使用搜索命令B的消息收集器来实现。这是一些示例代码(在最新版本中有效):

if (message.content.match("Command A")) {
    //Execute your command A code
    message.channel.send("Did some command A stuff");

    //After command A code is executed, do the following:

    var filter = m => m.author.id == message.author.id;

    //Creates a message collector, to collect messages sent by the same user
    //When max is 1, only 1 message will be collected and then the collector dies
    const collector = new Discord.MessageCollector(message.channel, filter, {max: 1});

    collector.on("collect", msg => {
        //msg is the new message that the user just sent, so check if it contains Discord
        if(msg.content.match(/Discord/gi)){
            //Do your command B stuff:
            const Embed = new Discord.MessageEmbed()
        }
    });
}

此代码检查命令A,并执行命令A代码。然后,它创建一个消息收集器。消息收集器将一直等到刚刚执行命令A的用户发送另一条消息。用户发送另一条消息后,它将运行代码以侦听collect事件。因此,我们采用收集的消息(在这种情况下为msg),并检查它是否与“ Discord”匹配。从那里,您可以执行Command B功能。

请注意,用户在执行命令A后发送消息后,收集器立即结束。这意味着,如果用户在1次尝试中未输入包含“ Discord”的消息,则收集器将结束,并且用户必须再次执行命令A,才能再次尝试执行命令B。如果要允许用户更多尝试执行命令B,或者希望用户在执行命令A之后可以连续执行命令B多次,则需要增加max

相关问题