It seems you've never defined arguments
(which wouldn't be a wise choice for a variable name anyway, as there is an arguments object in normal functions).
Instead of getting custom arguments from the message and searching the role by its name, you could mention the role(s) and get them by using message.mentions.roles
. This way you don't have to chop and clean up the message.content
either.
client.on('message', async (message) => {
if (message.content.startsWith(`${prefix}giverole`)) {
if (!message.member.hasPermission('ADMINISTRATOR')) {
return message.channel.send(
"Vous n'avez pas la permission d'utiliser cette commande.",
);
}
const user = message.mentions.users.first();
const member = message.guild.member(user);
const role = message.mentions.roles.first();
if (!member) {
return message.reply('Please mention someone to give a role to.');
}
if (!role) {
return message.reply(`Please mention a role to add to ${member}.`);
}
try {
await member.roles.add(role);
message.reply(`${member} now has the "${role}" role`);
} catch (error) {
console.log(error);
message.reply(`Role "${role}" is not added to ${member}.
_${error}_`);
}
}
});
PS: Make sure to drag your bot's role above all other roles in your server settings and give it the Manage Roles
permission to avoid DiscordAPIError: Missing Permissions
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…