当有人对消息做出反应时,我如何让我的 discord.js 机器人添加角色?
Posted
技术标签:
【中文标题】当有人对消息做出反应时,我如何让我的 discord.js 机器人添加角色?【英文标题】:How would I get my discord.js bot to add a role when someone reacts to a message? 【发布时间】:2020-11-21 11:18:20 【问题描述】:现在,我正在尝试制作一个反应角色机器人,但我不断收到错误消息。这是我的代码。
const Discord = require('discord.js');
const keepAlive = require('./server');
const client = new Discord.Client();
client.on('message', message =>
if (message.author !== null && message.author.bot) return;
if (message.toString().toLowerCase().includes('start reaction roles'))
message.delete()
const embed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('React to get notified')
.setDescription(`Hello! Please react to this message to get a role. Here are the roles that you can get by reacting:\n> ???? | React with this emoji to get <@&727665971443269635> notifications.\n> ⚠️ | React with this emoji to get <@&727633811709362187>.\nYou can chose one of the emojis, or both of them.`)
message.channel.send(embed).then(sentMessage =>
sentMessage.react("????")
.then(() => sentMessage.react("⚠️"))
.catch(() => console.error('One of the emojis failed to react.'));
)
const collector = sentMessage.createReactionCollector();
collector.on('collect', (reaction, user) =>
if (reaction.emoji.name === '????')
role = message.guild.roles.find(role => role.name === "ne1 here");
message.member.addRole(role);
else if (reaction.emoji.name === '⚠️')
role = message.guild.roles.find(role => role.name === "HR notifications");
message.member.addRole(role);
)
);
keepAlive();
client.login(process.env.TOKEN);
上面写着const collector = sentMessage.createReactionCollector();
的部分不起作用。如果有人可以帮我解决这个问题,那就太好了。
【问题讨论】:
【参考方案1】:我刚刚想出了如何让它运行。如果您想使用它,这是代码(这是我的全部代码。如果需要,请删除一些部分)。
const Discord = require('discord.js');
const keepAlive = require('./server');
const client = new Discord.Client();
client.on('message', message =>
if (message.author !== null && message.author.bot) return;
if (message.toString().toLowerCase().includes('start reaction roles'))
message.delete()
const embed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('React to get notified')
.setDescription(`Hello! Please react to this message to get a role. Here are the roles that you can get by reacting:\n> ? | React with this emoji to get <@&727665971443269635> notifications.\n> ⚠️ | React with this emoji to get <@&727633811709362187>.\nYou can chose one of the emojis, or both of them.\n\n**NOTE:**\nCurrently, this command does not support removing your roles. If you want to remove your role, click on the emoji. Then, click on your profile, and click the X next to the role you want to remove. You can always get it back by reacting.`)
message.channel.send(embed).then(sentMessage =>
sentMessage.react("?").then(() => sentMessage.react("⚠️")).then(() =>
const filter = (reaction, user) =>
return true;
const collector = sentMessage.createReactionCollector(filter);
collector.on('collect', (reaction, user) =>
if (reaction.emoji.name === '?')
role = message.guild.roles.cache.find(role => role.name === "ne1 here");
message.member.roles.add(role);
else if (reaction.emoji.name === '⚠️')
role = message.guild.roles.cache.find(role => role.name === "HR notifications");
message.member.roles.add(role);
)
)
.catch(() => console.error('One of the emojis failed to react.'));
)
);
keepAlive();
client.login(process.env.TOKEN);
我希望这是有用的。
【讨论】:
以上是关于当有人对消息做出反应时,我如何让我的 discord.js 机器人添加角色?的主要内容,如果未能解决你的问题,请参考以下文章
当用户对消息做出反应时,机器人会在 discord.js 中发送另一条消息
discord.py :: 我怎样才能让我的机器人对它自己的消息做出反应?