I'm trying to add a role to a user, but I’m getting no errors. I’m using a slash command.
let role = interaction.member.guild.roles.cache.find(r => r.name == "Amazing")
interaction.member.roles.add(role)
// interaction is from the interactionCreate event
In discord.js v13 interaction.member does not have a function to add roles.
I'd fetch the guildMember object then add the roles.
Example:
let guild = await client.guilds.fetch(interaction.guild_id)
let member = guild.members.cache.get(interaction.member.user.id);
let role = guild.roles.cache.find(r => r.name == "Amazing");
if (!role)
return console.log("the role doesn't exist");
member.roles.add(role);
Full example:
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("interactionCreate", async (interaction) => {
...
let guild = await client.guilds.fetch(interaction.guild_id)
let member = guild.members.cache.get(interaction.member.user.id);
let role = guild.roles.cache.find(r => r.name == "Amazing");
if (!role)
return console.log("the role doesn't exist");
member.roles.add(role);
});
client.login('Your-token');