I’m having trouble with my Discord bot. The bot logs in fine and shows the login message in console, but it won’t respond to commands at all. I want it to give a specific role to users when they get mentioned in a message.
client.once('ready', () => {
console.log(`Bot is online as: ${client.user.tag}!`)
})
client.on('messageCreate', (msg) => {
if (msg.content.startsWith(COMMAND_PREFIX)) {
const parameters = msg.content.slice(COMMAND_PREFIX.length).trim().split(/ +/);
const cmd = parameters.shift().toLowerCase();
if (cmd === 'giverole') {
if (msg.mentions.users.size === 1) {
const targetUser = msg.mentions.users.first();
const targetRole = msg.guild.roles.cache.find((role) => role.name === 'Team A');
if (targetRole) {
const guildMember = msg.guild.members.cache.get(targetUser.id);
guildMember.roles.add(targetRole)
.then(() => {
msg.reply(`Successfully gave ${targetRole.name} role to ${targetUser.tag}`);
})
.catch((err) => {
console.error(err);
msg.reply('Failed to assign the role.');
});
} else {
msg.reply('Cannot find that role.');
}
} else {
msg.reply('You need to mention exactly one user.');
}
}
}
});
client.login(BOT_TOKEN);
The goal is to automatically assign the specified role to any user that gets mentioned in the command. What could be wrong with my setup?