javascript - 尝试在 react emoji 上做 discord.js 得到 dm
问题描述
当您对一个或两个表情符号做出反应时,我如何制作一个向您发送 dm 的机器人?我不擅长 JavaScript,所以我请求你的帮助。
bot.on('message', message => {
if(message.author.bot)
{
if(message.embeds)
{
const embedMsg = message.embeds.find(msg => msg.title === 'server roles');
if(embedMsg)
{
embedMsg.message.react('706754740767424572')
.then(reaction => reaction.message.react('706761878721789982'))
.then(msg => console.log("Deleted message"))
.catch(err => console.error);
}
}
return;
}
if(message.content.toLowerCase() === '!roles')
{
const embed = new Discord.RichEmbed();
embed.setTitle("server roles");
embed.setColor("BLUE")
embed.setDescription("<:Patreon:706754740767424572> - patreonas")
message.channel.send(embed);
}
});
bot.on('messageReactionAdd', (reaction, user) => {
if(reaction.message.id !== '737968410377453578') return
user.send('...')
.catch(console.error)
});
解决方案
在机器人事件 messageReactionAdd 中,您需要添加:
let msgId = [];
client.on("message", (message) => {
if(message.content === "command") {
message.channel.send(
new discord.MessageEmbed()
.setAuthor("embed")
.setDescription("a sample descrption")
).then(message => {
msgId.push(message.id);
message.react("");
})
}
})
client.on('messageReactionAdd', (reaction, user) => {
if(user.id !== bot.user.id) {
if(reaction.message.id === msgId[0]) {
user.send("the message sent to the person that had react");
}
}
});
该代码的目标是在发送特定消息时发送消息嵌入,然后机器人将新的嵌入 id 推送到数组并向此嵌入添加反应。
那么当有人对此反应做出反应时,如果此反应的作者不是机器人,那么机器人会向对嵌入的消息做出反应的人发送消息。
此外,您应该使用以下方法将 discord.js 库更新为 discord.js v12npm update discord.js
推荐阅读
- express - 如果在 Express 中不满足条件,如何重定向除两条以外的所有路径
- regex - 谷歌分析 - 使用正则表达式匹配内容钻取中唯一编号的 URL?
- javascript - 我是 gettig 无法使用 GradleUserHomeServices.createVirtualFileSystem() 创建 WatchingAwareVirtualFileSystem 类型的服务这个错误
- xss - 这是一个有效的 XSS 过滤器吗?
- python - 如何在 BoxLayout 中显示简单列表和带有字典的列表
- python - 如何调度实时循环任务?
- wordpress - 在 .htaccess 中禁用后浏览器缓存未禁用
- git - Visual Studio 2019 Git 身份验证错误
- r - Bookdown:构建所有格式的书的问题
- python - 如何检测当前进程的结束?