如何向成员添加角色

时间:2021-01-26 12:29:06

标签: discord.js

所以我正在尝试制作一个验证脚本,这样当成员加入时,他/她必须写 !verify 然后他/她将在不和谐中获得排名,这样我们就不会被机器人突袭

const Discord = require("discord.js");
const bot = new Discord.Client();

module.exports.run = async (bot, message, args) => {
    
    message.delete()
    
    const member = message.author
    let myRole = message.guild.roles.cache.get("791724979435470889")

    if (!message.channel.name.startsWith(`⚜ʙɪᴇɴ`)) return message.channel.send(`you have already been verified`).then(msg => msg.delete({ timeout: 5000 }));
    message.channel.send(`${member} have been verifyed`).then(member.roles.add(myRole).tehn(msg => msg.delete({ timeout: 5000 })));
}

module.exports.help = {
    name: "verify" //Name of the command
}

当我尝试这段代码时,我得到了这个错误 我试图研究它,但我找不到任何东西

这是我的错误

message.channel.send(`${member} have been verifyed`).then(member.roles.add(myRole).tehn(msg => msg.delete({ timeout: 5000 })));
                                                                       ^

TypeError: Cannot read property 'add' of undefined
    at Object.module.exports.run (C:\Users\lauri\Desktop\QuebecCity\utility\verify.js:12:76)
    at Client.<anonymous> (C:\Users\lauri\Desktop\QuebecCity\index.js:257:33)
    at Client.emit (node:events:376:20)
    at MessageCreateAction.handle (C:\Users\lauri\Desktop\QuebecCity\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\lauri\Desktop\QuebecCity\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\lauri\Desktop\QuebecCity\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\lauri\Desktop\QuebecCity\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\lauri\Desktop\QuebecCity\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\lauri\Desktop\QuebecCity\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (node:events:376:20)

1 个答案:

答案 0 :(得分:0)

roles 是 GuildMember 对象的一个​​属性。您无法将角色添加到用户对象(又名 message.author,它返回消息作者的用户对象)。在该实例中,我们只想改为使用 member 对象的 message 属性,从而得到 message.member

最终代码

const Discord = require("discord.js");
const bot = new Discord.Client();

module.exports.run = async (bot, message, args) => {
    
    message.delete()
    
    const member = message.member
    let myRole = message.guild.roles.cache.get("791724979435470889")

    if (!message.channel.name.startsWith(`⚜ʙɪᴇɴ`)) return message.channel.send(`you have already been verified`).then(msg => msg.delete({ timeout: 5000 }));
    message.channel.send(`${member} have been verifyed`).then(msg => msg.delete({ timeout: 5000 }));
    member.roles.add(myRole)
}

module.exports.help = {
    name: "verify" //Name of the command
}