javascript - Discord.js 反应角色机器人不分配角色,但嵌入工作
问题描述
我的反应角色机器人发送带有反应的嵌入消息,但是当您选择反应时,角色不是属性
我有这个代码 index.js :
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"]}, {partials: ["MESSAGE", "CHANNEL", "REACTION", "MANAGE_ROLES"] })
require("dotenv").config();
const prefix = "!";
const fs = require("fs");
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync("./commands/").filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on("ready", () => {
console.log("bot online");
});
client.on("messageCreate", message => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === "reactionrole") {
client.commands.get("reactionrole").execute(message, args, Discord, client);
}
});
client.login(process.env.BOT_TOKEN);
反应角色.js:
module.exports = {
name: 'reactionrole',
description: "Sets up a reaction role message",
async execute(message, args, Discord, client) {
const channel = "889233386781151232";
const maths = message.guild.roles.cache.find(role => role.name === "Maths");
const mathsExpertes = message.guild.roles.cache.find(role => role.name === "Maths expertes");
const nsi = message.guild.roles.cache.find(role => role.name === "NSI");
const physique = message.guild.roles.cache.find(role => role.name === "Physique");
const svt = message.guild.roles.cache.find(role => role.name === "SVT");
const artsPlastique = message.guild.roles.cache.find(role => role.name === "Arts Plastiques");
const mathsEmoji = '';
const mathsExpertesEmoji = '';
const nsiEmoji = '';
const physiqueEmoji = '';
const svtEmoji = '';
const artsPlastiquesEmoji = '';
let embed = new Discord.MessageEmbed()
.setColor("#e42643")
.setTitle("Selection des matière")
.setDescription("Choisis tes matières en cliquant sur la reaction qui correspond\n\n"
+ `${mathsEmoji} : Maths\n`
+ `${mathsExpertesEmoji} : Maths Expertes\n`
+ `${nsiEmoji} : NSI\n`
+ `${physiqueEmoji} : Physique\n`
+ `${svtEmoji} : SVT\n`
+ `${artsPlastiquesEmoji} : Arts Plastiques\n`);
let messageEmbed = await message.channel.send({embeds: [embed]});
messageEmbed.react(mathsEmoji);
messageEmbed.react(mathsExpertesEmoji);
messageEmbed.react(nsiEmoji);
messageEmbed.react(physiqueEmoji);
messageEmbed.react(svtEmoji);
messageEmbed.react(artsPlastiquesEmoji);
client.on('messageReactionAdd', async(reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
if (reaction.emoji.name === mathsEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.add(maths);
}
if (reaction.emoji.name === mathsExpertesEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.add(mathsExpertes);
}
if (reaction.emoji.name === nsiEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.add(nsi);
}
if (reaction.emoji.name === physiqueEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.add(physique);
}
if (reaction.emoji.name === svtEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.add(svt);
}
if (reaction.emoji.name === artsPlastiquesEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.add(artsPlastique);
}
} else {
return;
}
});
client.on("messageReactionRemove", async(reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
if (reaction.emoji.name === mathsEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.remove(maths);
}
if (reaction.emoji.name === mathsExpertesEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.remove(mathsExpertes);
}
if (reaction.emoji.name === nsiEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.remove(nsi);
}
if (reaction.emoji.name === physiqueEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.remove(physique);
}
if (reaction.emoji.name === svtEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.remove(svt);
}
if (reaction.emoji.name === artsPlastiquesEmoji){
await reaction.message.guild.members.cache.get(user.id).roles.remove(artsPlastique);
}
} else {
return;
}
});
}
}
并且在启动命令时我没有错误
你能帮我吗
我知道这个问题已经被问过了,但我不明白答案或不清楚
解决方案
推荐阅读
- huawei-mobile-services - 华为账号登录agc失败代码203817988出现错误
- vue.js - 从 vuex 操作访问 $root 实例
- c - 根据 C 标准将函数强制转换为指向各种函数的指针是否合法?
- python - 使用与所有其他命令相同的命令向选项菜单添加选项
- python - 如何将一个数据框拆分为两个数据框
- java - 在 spring mvc 应用程序中使用本地线程是否安全?
- javascript - React-Router-Dom v5.2,无法从父级收到的道具将道具传递给子组件
- flutter - 如何从父级中的子小部件显示 CircularProgressIndicator()?
- sonarqube - 未针对构建中的所有作业分析 Azure Devops 中 PR 的 Sonarcloud 质量策略
- php - 从表单 html 接收到的 PHP 传递变量到两个 php 文件