DiscordBot会向选择了特定角色的用户发送欢迎私信。

huangapple go评论84阅读模式
英文:

DiscordBot Sends a welcome DM to those who have selected a certain role

问题

我正在尝试编写代码,让我的机器人向选择了广告角色的新成员发送私信,但似乎不起作用,我无法弄清楚原因,我对此还很陌生。

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.GuildMessageReactions,
        // ...
    ]
});


client.on('guildMemberAdd', member => {
    const advertiserRoleId = '1138502484210495648'; 

    // 检查新成员是否拥有'Advertiser'角色
    const hasAdvertiserRole = member.roles.cache.some(role => role.id === advertiserRoleId);

    if (hasAdvertiserRole) {
        // 向新成员发送欢迎私信
        member.send("欢迎加入服务器!感谢您选择'Advertiser'角色。");
    }
});

client.login('TOKEN');

如果有人知道我做错了什么,将不胜感激。

当新成员加入时,他们可以选择一个'Advertiser'角色,我希望向他们发送私信。

(上面的代码中并没有实际发送私信,我还没有编写这部分,我只是用它作为测试,直到我能让它正常工作)。

英文:

I'm trying to code my bot to DM new joiners who have selected an Advertising role but It doesn't seem to be working and can't figure out why, I'm quite new to this

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.GuildMessageReactions,
        // ...
    ]
});


client.on('guildMemberAdd', member => {
    const advertiserRoleId = '1138502484210495648'; 

    // Check if the new member has the 'Advertiser' role
    const hasAdvertiserRole = member.roles.cache.some(role => role.id === advertiserRoleId);

    if (hasAdvertiserRole) {
        // Send a welcome DM to the new member
        member.send("Welcome to the server! Thank you for selecting the 'Advertiser' role.");
    }
});

client.login('TOKEN');

if anyone knows what I'm doing wrong That would be much appreciated

When new members join they are able to select an 'Advertiser' Role, I wanted the DM to send them a private DM message

(It won't be the Dm in the code above, I havn't wrote it yet, I'm just using that as a tester until I can get it to work)

答案1

得分: 1

正如Zsolt已经评论的那样,guildMemberAdd事件在用户加入服务器时触发。

在你的情况下,你可以使用guildMemberUpdate来进行检查,类似于以下代码:

client.on('guildMemberUpdate', (oldUser, newUser) => {
    const advertiserRoleId = '1138502484210495648'; 

    // 检查新成员是否拥有'Advertiser'角色
    const hasAdvertiserRole = newUser.roles.cache.find(role => role.id === advertiserRoleId);
    // 检查用户之前是否拥有该角色,以避免重复发送消息。
    const hadAdvertiserRole = oldUser.roles.cache.find(role => role.id === advertiserRoleId);

    if (hasAdvertiserRole && !hadAdvertiserRole) {
        // 向新成员发送欢迎私信
        member.send({content: "欢迎加入服务器!感谢您选择'Advertiser'角色。"});
    }
});

在上面的代码中,我们还检查用户之前是否拥有该角色,以避免重复发送私信。

你可能需要编辑上述代码以适应你的需求。

英文:

As Zsolt already commented, guildMemberAdd event is fired at the moment user joins the guild.

In your case you would want to check with guildMemberUpdate something like:

client.on('guildMemberUpdate', (oldUser, newUser) => {
    const advertiserRoleId = '1138502484210495648'; 

    // Check if the new member has the 'Advertiser' role
    const hasAdvertiserRole = newUser.roles.cache.find(role => role.id === advertiserRoleId);
    // Check if the user had the role before to avoid duplicate message.
    const hadAdvertiserRole = oldUser.roles.cache.find(role => role.id === advertiserRoleId);

    if (hasAdvertiserRole && !hadAdvertiserRole) {
        // Send a welcome DM to the new member
        member.send({content: "Welcome to the server! Thank you for selecting the 'Advertiser' role."});
    }
});

In above code we also check if the user HAD the role before, to avoid duplicate DMs.

You might need to edit the above code to fit your needs.

huangapple
  • 本文由 发表于 2023年8月9日 01:55:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定