【问题标题】:How to give all members a role? discord.js如何给所有成员一个角色?不和谐.js
【发布时间】:2018-10-22 16:44:00
【问题描述】:

我想给我的 Discord 中的所有用户一个"Member" 角色,我已经做到了,所以每个加入的新成员都会收到一个成员角色。但是,仍有大约 90 人没有担任该角色。

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

命令应该是这样的: '!giveallrole (role)'

【问题讨论】:

    标签: discord.js


    【解决方案1】:
    // find the role with the name "Community"
    let role = message.guild.roles.find(r => r.name == 'Community')
    
    // if role doesn't exist, notify the author of command that the role couldn't be found
    if (!role) return message.channel.send(`**${message.author.username}**, role not found`)
    
    // find all guild members that aren't bots, and add the "Community" role to each
    message.guild.members.filter(m => !m.user.bot).forEach(member => member.addRole(role))
    
    // notify the author of the command that the role was successfully added to all members
    message.channel.send(`**${message.author.username}**, role **${role.name}** was added to all members`)
    

    【讨论】:

    • 代码至少应该有 cmets,以便 OP 和未来的读者可以从中受益和学习
    【解决方案2】:

    这对我有用:

    命令编码

    // 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);
        } 
    });
    

    【讨论】:

    • 请考虑添加有关您的代码的简短说明,以及它与其他答案的不同/更好之处。
    猜你喜欢
    • 2020-10-24
    • 2021-02-16
    • 2021-06-16
    • 2020-12-05
    • 2020-11-22
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    相关资源
    最近更新 更多