Discord Bot 选择角色不会将角色授予用户

时间:2021-01-10 16:48:58

标签: javascript discord.js

这是我最近在开发的 discord bot,在我将所有代码移动到我的 VPS 后,reactionroles 不再起作用。

因此,出于某种原因,每次您单击 emoij 时,它都不会向用户添加角色,我尝试了很多但没有任何效果。看起来它应该可以工作,我错过了什么吗?当我启动机器人时,一切似乎都很好,甚至命令也能正常工作。我没有在 role.id 中犯任何错误,因为我也尝试了 role.name 并在其中输入了名称。这也不起作用,我希望你有足够的信息。

第一部分是 index.js,第二部分是我使用的模块。

//This is the index.js
const Discord = require('discord.js');
const bot = new Discord.Client({partials: ["MESSAGE", "CHANNEL", "REACTION"]});
const fs = require('fs');

const prefix = '!';

bot.once('ready', () => {
    console.log('Commandor is online!')
});

bot.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./Commands/').filter(file => file.endsWith('.js'))
for(const file of commandFiles){
    const command = require (`./Commands/${file}`);

    bot.commands.set(command.name, command)
}

bot.on('message', 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') {
        bot.commands.get('reactionrole').execute(message, args, Discord, bot);
    } else if (command === 'twitchandyoutube') {
        bot.commands.get('twitchandyoutube').execute(message, args, Discord, bot);
    } else if (command === 'games') {
        bot.commands.get('games').execute(message, args, Discord, bot);
    } else if (command === 'clear') {
        bot.commands.get('clear').execute(message, args);
    }
});

bot.login(This is my very secret token :) );



//this is the module i made.
module.exports = {
    name: 'games',
    description: "Select the games",
    async execute(message, args, Discord, bot) {
        if(message.member.roles.cache.has('794900472632836116')) {
        const channel = '795030215361429565';
        const rlRole = message.guild.roles.cache.find(role => role.name === "Rocket League");
        const csgoRole = message.guild.roles.cache.find(role => role.name === "Counter-Strike Global Offensive");

        const rl = ('<:RocketLeague:796401366804856842>');
        const csgo = ('<:CSGO:796402447739912202>');
        const rlEmoij = ('796401366804856842');
        const csgoEmoij = ('796402447739912202');

        let embed = new Discord.MessageEmbed()
            .setColor('#e42643')
            .setTitle('Game selection')
            .setDescription(`What games do you play?
                            Select here which games you like to play and see the text/voice channels from.\n\n`
                + `${rl}   **(**    Rocket League                       **)**\n`
                + `${csgo}   **(**    Counter-Strike Global Offensive     **)**`);
        
        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(rlEmoij);
        messageEmbed.react(csgoEmoij);

        bot.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 === rlEmoij) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(rlRole);
                }
                if (reaction.emoji.name === csgoEmoij) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(csgoRole);
                }
            } else {
                return;
            }
 
        });

        bot.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 === rlRole) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(rlRole);
                }
                if (reaction.emoji.name === csgoEmoij) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(csgoRole);
                }
            } else {
                return;
            }
        });
       } else {
        message.channel.send("You don't have the right permissions to do this.");
 
}
}
} 

1 个答案:

答案 0 :(得分:0)

在两个事件(messageReactionAddmessageReactionRemove)中,您都在检查 reaction.emoji.name 是否为 rlEmoij / csgoEmoij

但属性 reaction.emoji.name 只是表情符号的名称。因此,在您的情况下,它将是 RocketLeagueCSGO。您需要获取 ID:reaction.emoji.id

做这样的事情:

if (reaction.emoji.id === rlEmoij) {
    await reaction.message.guild.members.cache.get(user.id).roles.add(rlRole);
}
if (reaction.emoji.id === csgoEmoij) {
    await reaction.message.guild.members.cache.get(user.id).roles.add(csgoRole);
}