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

Posted

技术标签:

【中文标题】Discord Bot 选择角色不会将角色授予用户【英文标题】:Discord Bot selection role won't give the role to the user 【发布时间】:2021-04-15 17:51:09 【问题描述】:

这是我最近正在研究的不和谐机器人,在我将所有代码移到我的 VPS 后,reactionroles 不再工作了。

因此,由于某种原因,每次您单击表情符号时,它都不会向用户添加角色,我尝试了很多但没有任何效果。似乎它应该工作,我错过了什么?当我启动机器人时,一切似乎都很好,甚至命令也有效。我没有在 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】:

在这两个事件(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);

【讨论】:

以上是关于Discord Bot 选择角色不会将角色授予用户的主要内容,如果未能解决你的问题,请参考以下文章

角色反应 Discord Bot

为啥 Discord bot 无法识别命令?

Python Discord Bot - 在不同的服务器中获取用户角色

Discord Bot如何为自动进入服务器的新用户赋予角色

Discord bot 忽略所有 webhook?

如何让 Discord Bot 选择频道