简体   繁体   English

我的 Discord 机器人如何为新用户分配角色?

[英]How can my Discord Bot assign a Role to new Users?

First im new to Coding, that means i mostly Copie and Paste things.首先我是编码新手,这意味着我主要是复制和粘贴东西。

To my problem: i do exactly things like in Videos, switch on "SERVER MEMBERS INTENT" in the Discord Dev portal and so on.对于我的问题:我做的事情就像在视频中一样,在 Discord 开发门户中打开“服务器成员意图”等等。 But my Bot won't assign a Role after someone Joins my DC.但是在有人加入我的 DC 后,我的 Bot 不会分配角色。

My code looks like a mess BUT!我的代码看起来很乱但是! the function i wanted first that the Bot reply "pong" after i type !ping worked finaly after many hours of re-coding stuff . function 我首先想在我输入 !ping 后的 Bot 回复“pong”经过数小时的重新编码后终于工作

here my code:这是我的代码:

global.Discord = require('discord.js')
const { Client, Intents } = require('discord.js');
const { on } = require('events');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = "!";
const fs = require('fs');

const token = "my token";

client.commands = new Discord.Collection();
client.events = new Discord.Collection();

client.on('guildMemberAdd', guildMember =>{
    let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'anfänger');     

    guildMember.roles.add(welcomeRole);
    guildMember.guild.channels.cache.get('930264184510361670').send(`Welcome <${guildMember.user.id}> to out Server!`)
});

client.once("ready", () => {
    console.log(`Ready! Logged in as ${client.user.tag}! Im on ${client.guilds.cache.size} guild(s)!`)
    client.user.setActivity({type: "PLAYING", name: "Learning Code"})
});

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);
 
    client.commands.set(command.name, command);
}

client.on('messageCreate', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.split(' ').slice(1);
    const command = message.content.split(' ')[0].slice(prefix.length).toLowerCase();

    if(command === 'is'){
        client.commands.get('is').execute(message, args, Discord);
    }
});
 
client.login(token);

Aha!啊哈! there it is you spelled guildMember as guildmemberguildMember拼写为guildmember

client.on('guildMemberAdd', async guildMember => {
    var i = "930263943597928508";
    let role = guildMember.guild.roles.cache.find(r => r.id === i);
    guildmember.roles.add(role); //here replace guildmember with guildMember
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM