对反应角色不和谐机器人有问题

Posted

技术标签:

【中文标题】对反应角色不和谐机器人有问题【英文标题】:Having a problem with reactionRole discord bot 【发布时间】:2021-05-22 14:46:55 【问题描述】:

所以我最近开始制作一个不和谐的机器人,我是编码新手,但我确实有一些基本知识! 所以我主要是编写代码,它的工作率为 90%。唯一的问题是在添加或删除卷时。因此,当用户点击表情以获得角色时,机器人会对点击做出反应,但无法定义“公会”,因此它不会赋予角色。

module.exports = 
    name: 'reactionrole',
    description: "Best thing!!!",
    async execute(message, args, Discord, bot) 

        const channel = 'the id channel';
        const modRole = message.guild.roles.cache.find(role => role.name === "Mod");
        const defaultRole = message.guild.roles.cache.find(role => role.name === "El Gars");

        const modEmoji = '????';
        const defaultEmoji = '????';

//========== My embed message with the info ========//
        let embed = new Discord.MessageEmbed()
            .setColor('#e42643')
            .setTitle(`just thing`)
            .setDescription(`just thing\n\n`
                + `$modEmoji bla bla bla\n`
                + `$defaultEmoji bla bla bla`);

        let messageEmbed = await message.channel.send(embed);
//========== Letting my bot add the reaction ======//
        messageEmbed.react(modEmoji);
        messageEmbed.react(defaultEmoji);

//========== Here to add the role =================//
        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 === modEmoji) 
                    await reaction.message, guild, members.cache.get(user.id).roles.add(modRole);
                
                if (reaction.emoji.name === defaultEmoji) 
                    await reaction.message, guild, members.cache.get(user.id).roles.add(defaultRole);
                
             else 
                return;
            
        );

//========= Here to remove the role ============//
        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 === modEmoji) 
                    await reaction.message, guild, members.cache.get(user.id).roles.remove(modRole);
                
                if (reaction.emoji.name === defaultEmoji) 
                    await reaction.message, guild, members.cache.get(user.id).roles.remove(defaultRole);
                
             else 
                return;
            
        );

    


//========== In my main.js =========//


const Discord = require("discord.js");
const config = require("./config.json");
const fs = require('fs');
const bot = new Discord.Client( partials: ["MESSAGE", "CHANNEL", "REACTION"] );

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("ready", () => 
  console.log('Bot has started.');
);

bot.on('guildMemberAdd', guildMember => 
  let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'El Gars');

  guildMember.roles.add(welcomeRole);
  guildMember.guild.channels.cache.get('id channel').send(`Yo <@$guildMember.user.id> kes tu criss icit!`)
);

bot.on('message', message => 
  if (!message.content.startsWith(config.prefix) || message.author.bot) return;

  const args = message.content.slice(config.prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();

  if (command === 'command') 
    bot.commands.get('command').execute(message, args, Discord);
  

  if (command === 'clear') 
    bot.commands.get('clear').execute(message, args);
   else if (command === 'ban') 
    bot.commands.get('ban').execute(message, args);
   else if (command === 'kick') 
    bot.commands.get('kick').execute(message, args);
   else if (command === 'mute') 
    bot.commands.get('mute').execute(message, args);
   else if (command === 'unmute') 
    bot.commands.get('unmute').execute(message, args);
   else if (command === 'reactionrole') 
    bot.commands.get('reactionrole').execute(message, args, Discord, bot);
  


);



bot.login(config.token);

这里的错误: This is a peek at my error! :D

【问题讨论】:

【参考方案1】:

我刚刚发现我的错误! 我用逗号代替点

if (reaction.emoji.name === modEmoji) 
  await reaction.message, guild, members.cache.get(user.id).roles.add(modRole);

if (reaction.emoji.name === defaultEmoji) 
  await reaction.message, guild, members.cache.get(user.id).roles.add(defaultRole);


if (reaction.emoji.name === modEmoji) 
  await reaction.message.guild.members.cache.get(user.id).roles.add(modRole);

if (reaction.emoji.name === defaultEmoji) 
  await reaction.message.guild.members.cache.get(user.id).roles.add(defaultRole);

删除部分也是如此! ^^'

【讨论】:

以上是关于对反应角色不和谐机器人有问题的主要内容,如果未能解决你的问题,请参考以下文章

角色反应 Discord Bot

如何修剪没有特定角色的不和谐用户?

为 discord.js 设置嵌入的反应角色时遇到问题

试图让不和谐机器人对某个频道内的所有消息做出反应

不和谐机器人可以返回并对所有旧消息做出反应吗

(discord.py) 向不和谐机器人发布的嵌入添加反应