#Help with setting roles
14 messages · Page 1 of 1 (latest)
• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.
const {SlashCommandBuilder} = require('discord.js');
const Account = require('../schemas/AccountSchema.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('linkaccount')
.setDescription('Link Valorant account to Discord account.')
.addStringOption(option =>
option.setName('input')
.setDescription("The username of the player")),
async execute(interaction){
const username = interaction.options.getString("input");
if (!username.includes('#'))
return await interaction.reply('You have entered an invalid Valorant username and tag!');
const account = Account.find({discordId:interaction.user.id});
if (account.length > 0) {
await Account.deleteMany({ discordId: interaction.user.id })
}
try{
await Account.create({
username: interaction.user.username,
discordId: interaction.user.id,
valorantAccount: username
})
const role = interaction.option.guild.getRole('Bronze 1')
const member = interaction.options.getMember('Aronka');
interaction.member.roles.add(role)
await interaction.reply('Account successfully linked');
}catch(error){
console.error(error)
}
},
};
this code doesnt work
You dont have a role or user option so .getMember() and .getRole() won’t work. You only have a string option
Another thing, the options.get…() method takes the option name which is always lowercase
How can i make a command that gets the role and adds it to the userwithout them having to input it themselves?
You can use
await interaction.member.roles.add('ROLEID', 'Adding valorant role');
//...
or
const role = interaction.guild.roles.cache.find((role) => {return role.name == 'ROLENAME'})
if (!role) {
// Handle role not found
}
await interaction.member.roles.add(role, 'Adding valorant role');
//...
Make sure that the interaction is used in the correct guild and not in DMs
thanks a lot!
No problem :D