赋予角色的 Discord.js 命令
Posted
技术标签:
【中文标题】赋予角色的 Discord.js 命令【英文标题】:Discord.js command that gives a role 【发布时间】:2020-10-07 02:42:08 【问题描述】:所以我正在尝试创建一个命令,您可以在其中键入$test
,它会为您提供例如“测试”角色。这是我当前的代码,但我不断收到错误消息:“无法读取未定义的属性 'addRole'”
const Discord = require("discord.js");
const send = require("process");
const clear = require("console");
const client = new Discord.Client();
var prefix = "$";
client.login("token");
//TEST COMMAND
client.on("message", message =>
if (message.content.startsWith(prefix + "test"))
message.channel.send("You have been given `Need to be tested` role! You will be tested shortly!")
client.channels.get("701547440310059059").send(` please test $message.author!`)
const member = message.mentions.members.first();
let testRole = message.guild.roles.find(role => role.id == "609021049375293460")
member.addRole(testRole)
)
client.on('ready',()=>
console.log(`[READY] Logged in as $client.user.tag! ID: $client.user.id`);
let statuses = [
" status "
]
setInterval(function()
let status = statuses[Math.floor(Math.random() * statuses.length)];
client.user.setActivity(status, type:"WATCHING")
, 3000) //Seconds to Random
);
请让我知道如何轻松做到这一点。
【问题讨论】:
【参考方案1】:在discord.js v12中,GuildMember没有.addRole
的功能,你需要使用GuildMemberRoleManager's.add
,另外你需要在从服务器获取角色时添加.cache
,像这样:
const member = message.mentions.members.first();
let testRole = message.guild.roles.cache.find(role => role.id == "609021049375293460")
member.roles.add(testRole)
【讨论】:
嘿,这是我更新的代码,但我收到一个错误:无法读取未定义的属性“查找”。client.on("message", message => if (message.content.startsWith(prefix + "test")) message.channel.send("You have been given `Need to be tested` role! You will be tested shortly!") client.channels.get("701547440310059059").send(` please test $message.author!`) const member = message.mentions.members.first(); let testRole = message.guild.roles.cache.find(role => role.id == "609021049375293460") member.roles.add(testRole) )
嘿,通过检查您的 package.json 文件确保您正在运行 discord.js v12。如果你不是,你也许应该尝试切换到它,因为 v11 将在今年年底停止。如果您在切换到 v12 时需要帮助,可以查看this guide
我现在设法更新它,现在我得到一个“无法读取未定义的属性'角色'”client.on("message", message => if (message.content.startsWith(prefix + "test")) message.channel.send("You have been given `Need to be tested` role! You will be tested shortly!") client.channels.cache.get("701547440310059059").send(` please test $message.author!`) const member = message.mentions.members.first(); let testRole = message.guild.roles.cache.find(role => role.id == "609021049375293460") member.roles.add(testRole) )
您是否在服务器中发送此命令?因为它对我有用
奇怪,是的,我在测试服务器上,命令是$test
【参考方案2】:
好的,你收到了错误Cannot read property 'addRole' of undefined
。
这意味着成员变量是未定义的,这可能是由于您没有提及成员。
在这一行中,您输入了const member = message.mentions.members.first();
,这意味着当您运行命令时,您必须提及要添加角色的人。
希望这会有所帮助。
【讨论】:
嘿,谢谢,但是如果您检查上一个答案,我已经解决了问题,但是谢谢:D :祝你有美好的一天【参考方案3】:在最新版本的 discord.js 中,您可以尝试以下代码:
// Where '799617378904178698' is your role id
message.guild.roles.fetch('799617378904178698')
.then(role =>
console.log(`The role color is: $role.color`);
console.log(`The role name is: $role.name`);
let member = message.mentions.members.first();
member.roles.add(role).catch(console.error);
)
.catch(console.error);
适用于您尝试添加某些角色的情况。你应该这样称呼它:
(prefix)commandName @member
【讨论】:
【参考方案4】:如果您想为任何用户(机器人除外)赋予任何角色,找到了另一种解决方案
main.js(或 index.js 随便你):
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '+';
const fs = require("fs");
client.commands = new Discord.Collection()
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.once('ready', () =>
console.log('Bot is online');
);
client.on('message', message =>
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
console.log(args);
const command = args.shift().toLowerCase();
if (command == 'giveany')
const role = args[1].slice(3, args[1].length - 1);
client.commands.get('giveany').execute(message, args, role);
);
client.login('token');
giveany.js:
module.exports =
name: 'giveany',
description: 'adds any role to a member',
execute(message, args, role)
console.log(role);
message.guild.roles.fetch(role)
.then(r =>
console.log(`The role color is: $r.color`);
console.log(`The role name is: $r.name`);
let member = message.mentions.members.first();
if (member != undefined)
console.log('member=' + member);
member.roles.add(r).catch(console.error);
else
message.channel.send('You cannot give a role to a user that is either bot or undefined');
).catch( (error) =>
console.error(error);
message.channel.send('Could not find given role: ' + args[1]);
);
来电:+giveany @Username @Role
【讨论】:
以上是关于赋予角色的 Discord.js 命令的主要内容,如果未能解决你的问题,请参考以下文章