Ad
Discord.js Can't Add A Role To Member When They Join My Server
I want my bot to give a certain role to people when they join the server. But I get some weird error I don't really understand.
This is the code:
const { GuildMember, MessageEmbed } = require("discord.js");
module.exports = {
name: "guildMemberAdd",
/**
* @param {GuildMember} member
*/
async execute(member){
let role = member.guild.roles.cache.some(role => role.name === 'Member')
member.roles.add(role)
member.guild.channels.cache.get(process.env.WELCOME_MESSAGE_CHANNEL_ID).send({
embeds: [
new MessageEmbed()
.setTitle("Welcome! :smiley:")
.setDescription(`${member.toString()} has joined the server!\n
Thanks for joining. Head over to <#${process.env.RULE_CHANNEL_ID}> and verify yourself in <#${process.env.VERIFY_CHANNEL_ID}> to get access to all other channels.`)
.setThumbnail(member.user.displayAvatarURL())
.setColor("GREEN")
]
})
}
}
And when someone joins I get this error message:
TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.
Ad
Answer
The problem is that some()
returns a boolean (either true
or false
) and when you try to add a role to the member you pass this boolean value instead of a role ID. You can use the find()
method instead that returns the first item where the given function returns a truthy value (i.e. where role.name
is equal to "Member"
):
async execute(member) {
let role = member.guild.roles.cache.find((role) => role.name === 'Member');
if (!role)
return console.log('Cannot find the role with the name "Member"');
member.roles.add(role);
member.guild.channels.cache
.get(process.env.WELCOME_MESSAGE_CHANNEL_ID)
.send({
embeds: [
new MessageEmbed()
.setTitle('Welcome! :smiley:')
.setDescription(
`${member.toString()} has joined the server!\n Thanks for joining. Head over to <#${process.env.RULE_CHANNEL_ID}> and verify yourself in <#${process.env.VERIFY_CHANNEL_ID}> to get access to all other channels.`,
)
.setThumbnail(member.user.displayAvatarURL())
.setColor('GREEN'),
],
});
}
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad