Discord Bot阅读对已设置消息的反应

时间:2020-03-31 16:45:49

标签: discord.js

我让我的机器人读取所有渠道中的所有消息,然后根据仅存在?表情符号而没有其他内容来分配角色。

构造代码之上

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侦听消息响应。一旦我知道了,希望我能弄清楚其余的事情。

2 个答案:

答案 0 :(得分: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)
    }
});

这应该记录反应和用户,只要有人对此作出反应。

答案 1 :(得分:1)

这是使用discord.js v12

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();
        }
    }
});```