提供角色的Discord.js命令

问题描述 投票:0回答:1

因此,我正在尝试创建一个命令,在其中键入$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
});

请让我知道如何轻松地执行此操作。

javascript command bots discord discord.js
1个回答
0
投票
的属性'addRole'

在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)
© www.soinside.com 2019 - 2024. All rights reserved.