discord.js - 只有命令A执行成功才能执行命令B
问题描述
我正在尝试制作一个智能问题机器人,我想知道如果先执行命令 A,是否有办法允许执行命令 B
} else if(message.content.match(/Discord/gi)){
const Embed = new Discord.MessageEmbed()
}
这会查找包含 Discord(小写或大写)的消息 我不希望机器人查找包含 Discord 的每条消息。我希望它只有在先执行前面的命令时才会执行,然后它也会被禁用,听起来很复杂但可能
解决方案
假设我正确理解了您的意图,您只想在调用命令 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”匹配。从那里,您可以执行命令 B 功能。
请注意,用户在执行命令 A 后立即发送消息后,收集器结束。这意味着如果用户在 1 次尝试中未输入包含“Discord”的消息,则收集器结束,用户必须再次执行命令 A 才能再次尝试执行命令 B。如果您希望允许用户进行更多尝试执行命令 B,或者如果您希望用户在执行命令 A 后能够连续执行命令 B 多次,那么您需要增加max
.
推荐阅读
- macos - 系统调用常量 syscall.ENONET 在 Go 中未定义
- google-tag-manager - 将 GCLID 参数转换为 Squarespace 隐藏字段
- flutter - 如何在颤动中剪辑可变大小的小部件?
- javascript - .outerHTML 中不存在命名空间属性
- python - 如何创建多个标签和条目小部件,并在 Tkinter 中使用循环从中获取数据
- mysql - galera puppet 代码创建两个集群而不是一个集群
- r - 如何求解给定 x 的函数?
- prolog - 如何在 Prolog 中使两个列表相交
- swift - 使用协议将数据从一个 vc 传递到另一个 vc 的问题
- spring - 日期分钟子集上的 Spring mongoDB 聚合组