javascript - 如何为命令 discord.js 将多条消息连续串在一起
问题描述
我正在制作一个不和谐的机器人,我想让它做一个小石头剪刀布游戏,你输入命令“rps”,它会询问你是否要选择石头剪刀布。
问题是我当前处理命令的方式是它接收您发送的消息,检查它是否以“>”开头,然后将命令拆分为参数。问题是我真的不知道如何让它做石头剪刀布并检查答案,因为现在它只是运行一个函数,如果命令是我识别的东西。
如何让它检查答案?
解决方案
我认为你可以使用.awaitMessages
.
这是文档中的链接:[文档] ( https://discordjs.guide/popular-topics/collectors.html ) 和 ( https://discord.js.org/#/docs/main/stable/class/ TextChannel?scrollTo=awaitMessages )
文档中的一个示例:
// Await !vote messages
const filter = m => m.content.startsWith('!vote');
// Errors: ['time'] treats ending because of the time limit as an error
channel.awaitMessages(filter, { max: 4, time: 60000, errors: ['time'] })
.then(collected => console.log(collected.size))
.catch(collected => console.log(`After a minute, only ${collected.size} out of 4 voted.`));
2 awaitMessages(awaitReactions 基本上是一样的东西):
const filterOne = m => m.content === 'firstFilter'
//This is a filter and the 'm' is 'message' I believe. This is a filter and doesn't have to be m.content, but I will put it in this because its a little easier to understand
message.channel.send('This is the first awaitMessage, so type firstFilter').then(() => {
message.channel.awaitFilter(filterOne, {max: 1, time: 5000, errors: ['time']}) //Max of 1 messages, time in milliseconds and error if time runs out
.then(collected => {
//If the user's message passes through the filter
const filterTwo = m => m.content === 'secondFilter' //Since the user passed the first filter, this is filter 2
message.channel.send('Passed through filter one, this is filter two').then(() => {
message.channel.awaitMessages(filterTwo, {max: 1, time: 5000, errors: ['time']})
.then(collected => {
//User passes through second filter too
message.channel.send('Congratulations, you passed through both filters')
})
.catch(collected => {
//User passed through first filter but not the second
message.channel.send('You did not pass filter 2')
})
})
})
})
希望这可以帮助!
推荐阅读
- ios - 尝试显示带有消息的 UIAlertController 时应用程序崩溃:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序
- ibm-cloud-private - 如何更改 Microclimate 推送存储库?
- apache - 可以通过命令行启动 Apache,但不能通过 Wamp Server
- python - 替换并添加到python3中的数组切片
- matplotlib - 向 Shapefile 地图添加标签
- haskell-stack - 如何让堆栈看到依赖关系?
- php - PHP iconv 从 utf-8 到 windows-1252,没有特殊字符
- python - Pandas 在单个条形图上绘制多列
- excel - 将更新相同值列中的单元格的触发器
- python - 对于目录中的每个文件,使用 np.loadtxt?