如何将特定角色更改的公告发送到特定频道?

时间:2019-03-29 22:26:25

标签: discord.js

当某人的角色发生变化时,我想通知我们的主要聊天频道,虽然是特定角色-我该怎么做?

1 个答案:

答案 0 :(得分:1)

希望我能很好地理解你的问题。如果触发了事件,则必须使用guildMemberUpdate事件来检查角色是否仍然相同。然后,您必须运行一个简单的for循环,并检查已从guildMember中删除或分配了哪些角色。

这是代码:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('guildMemberupdate', (oldMember, newMember) => {
    const messagechannel = oldMember.guild.channels.find(r => r.name === 'Name of the channel where the announcement should be sent');
    if (!messagechannel) return 'Channel does not exist!';

    if (oldMember.roles.size < newMember.roles.size) {
        const embed = new Discord.RichEmbed()
            .setColor('ORANGE')
            .setTimestamp()
            .setAuthor('Role assigned')
            .addField(` Member:`, `${oldMember.user.tag} (${oldMember.id})`);

        for (const role of newMember.roles.map(x => x.id)) {
            if (!oldMember.roles.has(role)) {
                embed.addField(` Role(s):`, `${oldMember.guild.roles.get(role).name}`);
            }
        }
        messagechannel.send({
            embed
        });
    }

    if (oldMember.roles.size > newMember.roles.size) {
        const embed = new Discord.RichEmbed()
            .setColor('ORANGE')
            .setTimestamp()
            .setAuthor('Role removed')
            .addField(` Member`, `${oldMember.user.tag} (${oldMember.id})`);

        for (const role of oldMember.roles.map(x => x.id)) {
            if (!newMember.roles.has(role)) {
                embed.addField(` Role(s):`, `${oldMember.guild.roles.get(role).name}`);
            }
        }
        messagechannel.send({
            embed
        });
    }
});