英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论