【问题标题】:Discord Bot Reads Reaction to a Set MessageDiscord Bot 读取对设置消息的反应
【发布时间】:2020-07-12 07:17:54
【问题描述】:

我让我的机器人读取所有渠道中的所有消息,然后根据只有 ????表情符号,仅此而已。

Const 高于所有代码

const Discord = require('discord.js');
const bot = new Discord.Client();
const Yard = '694204522528243752';

代码用于阅读消息

bot.on('message', (message) => {
    if (message.content == '????') {
        message.member.roles.add(Yard);
    }
});

我正在尝试让机器人在某个频道中收听单个消息,然后根据反应将角色/权限组分配给放置反应的用户。

尝试让机器人读取反应

bot.on('Reaction Assign Role', async (reaction, user) => {
    const filter = (reaction) => reaction.emoji.name === '????';
    const rules = message.id('694538155336138752')

    await rules.message.id === filter;
        message.member.roles.add(Yard);
});

我不确定这应该如何工作。我认为机器人正在监听目标消息的反应。如果消息得到反应并且反应等于过滤器表情符号,则将角色添加到目标用户。

不知道如何让机器人通过 ID 监听消息的反应。一旦我得到它,我希望我能弄清楚其余的。

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    这比听起来要复杂。您需要创建一个“原始”侦听器,它基本上跟踪所有通道中的所有更改。然后,您可以专注于对特定消息的反应。

    我就是这样做的:

    const events = {
        MESSAGE_REACTION_ADD: 'messageReactionAdd',
    };
    
    //you dont need to modify any of this:
    bot.on('raw', async event => {
        if (!events.hasOwnProperty(event.t)) return;
    
        const { d: data } = event;
        const user = bot.users.get(data.user_id);
        const channel = bot.channels.get(data.channel_id) || await user.createDM();
    
        if (channel.messages.has(data.message_id)) return;
    
        const message = await channel.fetchMessage(data.message_id);
        const emojiKey = (data.emoji.id) ? `${data.emoji.name}:${data.emoji.id}` : data.emoji.name;
        const reaction = message.reactions.get(emojiKey);
        bot.emit(events[event.t], reaction, user);
    })
    
    //change the chnl variable so it gets the channel you want, the server ID for the correct server and the name of the emoji:
    bot.on('messageReactionAdd', async (reaction, user) => {
        let chnl= bot.channels.get(`289390221789757440`);
        if(reaction.emoji.name === 'NAMEHERE') {
            let msgserver = bot.guilds.get(`SERVERIDHERE`)
            let usr = await msgserver.fetchMember(user)
            console.log(reaction + ` ` + user)
        }
    });
    

    这应该在有人对其做出反应时记录反应和用户。

    【讨论】:

    • 您发布的代码的顶部是一个原始侦听器-侦听器首先会侦听所有消息反应。稍后在代码的后半部分,我将过滤某个消息的反应。如果我想将反应过滤到特定的表情符号,我也会在那里这样做吗?
    • 是的,在我放“NAMEHERE”的部分你只是把表情符号的名字放在那里
    • 尝试使用上面的代码,|bot.on('raw', async event => {| 我在第 5 个字符处出现错误,所以我假设 if 语句是'不好,因为没有消息
    • 这是我得到的错误。 (node:30404) UnhandledPromiseRejectionWarning: ReferenceError: events is not defined
    • 哦,抱歉,我忘记了一些事情。我编辑了我的答案以包含事件的定义。
    【解决方案2】:

    这是使用 discord.js v12

    const discord = require('discord.js');
    const bot = new discord.Client({
        partials: ['MESSAGE','REACTION']
    });
    const TOKEN = require('./config.json');
    const Yard = '<roleID to Set>'
    const MessageNumber = '<messageID to Watch>'
    
    bot.login(TOKEN.token)
    
    bot.on('ready', () => {
        console.log(bot.user.tag + " has logged in.");
    });
    
    bot.on('messageReactionAdd', async (reaction, user) => {
        console.log("Message Reaction Add Top");
    
        let applyRole = async () => {
            let emojiName = reaction.emoji.name;
            let role = reaction.message.guild.roles.cache.find;
            let member = reaction.message.guild.members.cache.find(member => member.id == user.id);
            if (role && member) {
                console.log("Role and Member Found");
                await member.roles.add(Yard);
            }
        }
        if (reaction.message.partial) {
            try {
                let msg = await reaction.message.fetch()
                console.log(msg.id);
                if (msg.id === MessageNumber) {
                    console.log("Cached - Applied");
                    applyRole();
                }
            }
            catch (err) {
                console.log(err);
            }
        }
        else {
            console.log("Not a Partial");
            if (reaction.message.id === MessageNumber) {
                console.log("Not a Partial - applied")
                applyRole();
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 1970-01-01
      • 2018-05-30
      • 2022-10-14
      • 2019-06-07
      • 2022-01-10
      • 2019-07-04
      • 2020-07-15
      • 2021-03-25
      相关资源
      最近更新 更多