Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
835 views
in Technique[技术] by (71.8m points)

discord.js - How can I add a role to a member

So I am trying to make a verify script so when a member join he/she has to write !verify and then he/she will get a rank on the discord so we won't get raided by bot's

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
}

And when I try this code out I get this error And I have tried to research it but I can't find anything on it

This is my error

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:UserslauriDesktopQuebecCityutilityverify.js:12:76)
    at Client.<anonymous> (C:UserslauriDesktopQuebecCityindex.js:257:33)
    at Client.emit (node:events:376:20)
    at MessageCreateAction.handle (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcclientactionsMessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcclientwebsockethandlersMESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:301:10)
    at WebSocket.onMessage (C:UserslauriDesktopQuebecCity
ode_moduleswslibevent-target.js:132:16)
    at WebSocket.emit (node:events:376:20)
question from:https://stackoverflow.com/questions/65901388/how-can-i-add-a-role-to-a-member

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

roles is a property of the GuildMember object. You are unable to add roles to a User object (AKA message.author that returns the user object of the author of a message). From that instance, we would simply want to instead use the member property of the message object, resulting in message.member.

Final Code

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...