Skip to content

Commit

Permalink
[Enhancement] addrole command now can take role mention, name or id…
Browse files Browse the repository at this point in the history
…! deleted useless code from the interaction function
  • Loading branch information
naseif committed Nov 25, 2021
1 parent 2d41190 commit 756c16e
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions commands/Admin/addrole.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { Permissions } = require("discord.js");
const { embedMessage } = require("../../modules/embedSimple");
const { getGuildUserFromMention } = require("../../modules/getUserFromMention");
const {
getGuildUserFromMention,
getRoleFromMention,
} = require("../../modules/getUserFromMention");

module.exports = {
name: "addrole",
args: true,
description: "Adds a role for a user",
usage: "addrole <user> <role name>",
usage: "addrole <user> <role name || mention>",
async run(message, args, client) {
const guildUser = getGuildUserFromMention(args[0], message);
const roleName = args[1];
const roleToGive = message.guild.roles.cache.find(
(role) => role.name === `${roleName}`
);
const roleToGive =
message.guild.roles.cache.find(
(role) => role.name === `${roleName}` || role.id === roleName
) || (await getRoleFromMention(args[1], message));

if (!args[0])
return await message.channel.send({
Expand Down Expand Up @@ -92,18 +96,17 @@ module.exports = {
.addUserOption((option) =>
option.setName("user").setDescription("Select a user").setRequired(true)
)
.addStringOption((option) =>
option.setName("role").setDescription("role name").setRequired(true)
.addRoleOption((option) =>
option
.setName("role")
.setDescription("role you wish to add to the user")
.setRequired(true)
),
async execute(interaction, client) {
await interaction.deferReply();
const user = interaction.options.getMember("user");
const roleName = interaction.options.getString("role");
const roleToGive = interaction.guild.roles.cache.find(
(role) => role.name === `${roleName}`
);
const newRole = interaction.options.getRole("role");

if (!roleToGive) return await interaction.followUp({embeds: [embedMessage("RED", `❌ | Role does not exist!`)]});
if (!interaction.member.permissions.has([Permissions.FLAGS.ADMINISTRATOR]))
return await interaction.followUp({
embeds: [
Expand All @@ -115,12 +118,12 @@ module.exports = {
});

try {
await user.roles.add(roleToGive);
await user.roles.add(newRole);
await interaction.followUp({
embeds: [
embedMessage(
"#9dcc37",
`✅ | ${user} has been given ${roleToGive} Role!`
`✅ | ${user} has been granted ${newRole} Role!`
),
],
});
Expand Down

0 comments on commit 756c16e

Please sign in to comment.