discord.js - 如何测试用户对消息做出反应的表情符号?
问题描述
我正在尝试制作一个系统,用户可以在其中对消息做出反应,并且会回复一些文本。文本将根据他们对哪个表情符号做出反应而有所不同。我研究过反应收集器,但我仍在努力寻找一个我想做的例子。
这是我正在使用的基本代码,我从 Discord's guide to collections here获得。
message.react('');
const filter = (reaction, user) => {
return reaction.emoji.name === '';
};
const collector = message.createReactionCollector(filter, { max: 100 });
collector.on('collect', (reaction, user) => {
message.channel.send('Collecting...')
});
collector.on('end', collected => {
message.channel.send('Done');
});
此代码有效,但是collector.on('collect'...
无论对哪个表情符号做出反应,它都会执行代码。我希望能够执行不同的代码,例如当用户对不同的表情符号做出反应时发送不同的嵌入。谢谢!
解决方案
您的收集器过滤器将只允许收集表情符号,因此您应该删除它以使机器人在添加其他反应时表现不同。您可以使用
reaction
和user
参数来确定要执行的操作:
// This will make it collect every reaction, without checking the emoji
const collector = message.createReactionCollector(() => true, { max: 100 })
collector.on('collect', (reaction, user) => {
if (reaction.emoji.name == '') {
// The user has reacted with the emoji
} else {
// The user has reacted with a different emoji
}
})
collector.on('end', collected => {
// The bot has finished collecting reaction, because either the max number
// has been reached or the time has finished
})
在这些if/else
语句中,您可以添加任何您想要的内容(发送消息、嵌入,...)
推荐阅读
- haskell - GHC 如何将 Haskell 代码编译为汇编?
- linux - 从文件中消除多个空间并修改原始文件
- react-native-flatlist - react-native flatList 呈现空白屏幕
- javascript - JavaScript Canvas toDataURL 安全注意事项
- rust - 如何声明一个函数而不实现它?
- php - PHP MS SQL,查询未执行,但php返回true
- date - 日期时间格式全球化变化
- c# - 在 C# 中使用 FsCheck.Xunit,在执行完所有测试后如何清理/拆除?
- angular - Angular Httpclient PUT 在参数中设置布尔值和正文
- javascript - 如何在 JavaScript 中使用 getElementById("").style.color 更改对象的颜色