宣布成员角色添加到特定频道中的特定角色

Announce a members role ADDED to a specific role in a specific channel

我想让我的机器人在我们名为 family-talk 的特定频道中宣布,我也有频道 ID,但不确定放在哪里,但我希望这只会发生当一个角色被添加到一个成员时,我下面的代码是正确的还是错误的?我没有很多好的方法来测试这个,所以我希望能在这里得到一些很大的帮助。我还想知道放置代码的最佳位置。谢谢!

   if(!oldMember.roles.has('539208166563643407') && newMember.roles.has('561773668439687179'))
   client.channels.get("550197572178935809").send("This member got the special role!");

您的代码应该可以工作,但是您在 if 中有 2 个不同的 ID,因此为了使代码更简洁,只需执行以下操作:

const roleID = '539208166563643407';
const channelID = '550197572178935809';

client.on('guildMemberUpdate', (oldMember, newMember) => {
    if(!oldMember.roles.has(roleID) && newMember.roles.has(roleID)) {
        client.channels.get(channelID).send(newMember.displayName + ' got the special role!');
    }
});