【发布时间】:2018-10-22 16:44:00
【问题描述】:
我想给我的 Discord 中的所有用户一个"Member" 角色,我已经做到了,所以每个加入的新成员都会收到一个成员角色。但是,仍有大约 90 人没有担任该角色。
映射所有用户的代码是什么(我认为它是映射......或者可能是一个集合,idk :s)并赋予他们一个角色?
命令应该是这样的:
'!giveallrole (role)'
【问题讨论】:
标签: discord.js
我想给我的 Discord 中的所有用户一个"Member" 角色,我已经做到了,所以每个加入的新成员都会收到一个成员角色。但是,仍有大约 90 人没有担任该角色。
映射所有用户的代码是什么(我认为它是映射......或者可能是一个集合,idk :s)并赋予他们一个角色?
命令应该是这样的:
'!giveallrole (role)'
【问题讨论】:
标签: discord.js
// 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`)
【讨论】:
这对我有用:
命令编码
// 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);
}
});
【讨论】: