如何给所有成员一个角色? discord.js

时间:2018-05-12 12:33:59

标签: discord.js

我想让我的Discord中的所有用户都成为“会员”角色,我已经这样做了,所以每个加入的新成员都会收到一个成员角色。但是,仍然有大约90人仍然没有这个角色。

代码用于映射所有用户的代码(我认为它是映射......或者可能是一个集合,idk:s)并给它们一个角色?

命令应该像'!giveallrole(role)'

2 个答案:

答案 0 :(得分:1)

let role = message.guild.roles.find(r => r.name == 'Community')

if (!role) return message.channel.send(`**${message.author.username}**, role not found`)

message.guild.members.filter(m => !m.user.bot).forEach(member => member.addRole(role))
message.channel.send(`**${message.author.username}**, role **${role.name}** was added to all members`)

答案 1 :(得分:0)

这对我有用:

命令编码

// Command coding
module.exports = {
    name: 'giveallrole',
    description: "giveallrole cmd!",
    async execute(message, args) {
        message.channel.bulkDelete(1);
        //Does the person have moderator rank? - Just change the ID.
        if(message.member.roles.cache.has('854013128928919552')){
            // What role is it?
            let role = message.mentions.roles.first() 
            // Do the command action include the role. (Change the message if you don't want it to be danish.)
            if (!role) return message.channel.send(`**${message.author.username}**, rollen blev ikke funddet`)
            // Giv the role.
            message.guild.members.cache.filter(m => !m.user.bot).forEach(member => member.roles.add(role))
            // Return message - just change the message, it's right now in danish.
            message.channel.send(`**${message.author.username}**, rollen **${role.name}** blev tilføjet til alle medlemmer!`)
        }else{
            // Return message if the person doesn't have moderator.
            message.reply("Du har ikke tilladelse til dette.");
        }
    } }

index.js / main.js 编码

// Index.js / main.js coding
client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;
 
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
 
    if(command == 'giveallrole'){
        client.commands.get('giveallrole').execute(message, args);
    } 
});