From ccd25403da46b68bae2126bd7d56512107bd4459 Mon Sep 17 00:00:00 2001 From: TymanWasTaken <32660892+tymanwastaken@users.noreply.github.com> Date: Tue, 11 May 2021 11:02:09 -0600 Subject: add role command --- src/commands/moderation/role.ts | 122 ++++++++++++++++++++++++++++++++++++++++ src/lib/extensions/Util.ts | 36 ++++++++++-- 2 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 src/commands/moderation/role.ts diff --git a/src/commands/moderation/role.ts b/src/commands/moderation/role.ts new file mode 100644 index 0000000..8bf8827 --- /dev/null +++ b/src/commands/moderation/role.ts @@ -0,0 +1,122 @@ +/* eslint-disable @typescript-eslint/no-empty-function */ +import { BotCommand } from '../../lib/extensions/BotCommand'; +import AllowedMentions from '../../lib/utils/AllowedMentions'; +import { Message, Role, GuildMember } from 'discord.js'; + +export default class RoleCommand extends BotCommand { + private roleWhitelist: Record = { + 'Partner': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'Suggester': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator', 'Helper', 'Trial Helper', 'Contributor'], + 'Level Locked': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'No Files': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'No Reactions': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'No Links': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'No Bots': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'No VC': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'No Giveaways': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator', 'Helper'], + 'No Support': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'Giveaway Donor': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'Giveaway (200m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'Giveaway (100m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'Giveaway (50m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'Giveaway (25m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'Giveaway (10m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'Giveaway (5m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'], + 'Giveaway (1m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'] + }; + constructor() { + super('role', { + aliases: ['role', 'addrole', 'removerole'], + category: "Moulberry's Bush", + description: { + content: "Manages users' roles.", + usage: 'role ', + examples: ['role add tyman adminperms'] + }, + clientPermissions: ['MANAGE_ROLES', 'EMBED_LINKS', 'SEND_MESSAGES'], + channel: 'guild', + typing: true + }); + } + *args(): unknown { + const action: 'add' | 'remove' = yield { + id: 'action', + type: [['add'], ['remove']], + prompt: { + start: 'Would you like to `add` or `remove` a role?', + retry: '<:error:837123021016924261> Choose whether you would you like to `add` or `remove` a role.' + } + }; + let actionWord: string; + if (action === 'add') actionWord = 'to'; + else if (action === 'remove') actionWord = 'from'; + else return; + const user = yield { + id: 'user', + type: 'member', + prompt: { + start: `What user do you want to ${action} the role ${actionWord}?`, + retry: `<:error:837123021016924261> Choose a valid user to ${action} the role ${actionWord}.` + } + }; + const role = yield { + id: 'role', + type: 'role', + match: 'restContent', + prompt: { + start: `What role do you want to ${action}?`, + retry: `<:error:837123021016924261> Choose a valid role to ${action}.` + } + }; + return { action, user, role }; + } + + // eslint-disable-next-line require-await + public async exec(message: Message, { action, user, role }: { action: 'add' | 'remove'; user: GuildMember; role: Role }): Promise { + if (!message.member.permissions.has('MANAGE_ROLES') && !this.client.ownerID.includes(message.author.id)) { + const mappedRole = this.client.util.moulberryBushRoleMap.find(m => m.id === role.id); + if (!mappedRole || !this.roleWhitelist[mappedRole.name]) { + return message.util.reply(`<:error:837123021016924261> <@&${role.id}> is not whitelisted, and you do not have manage roles permission.`, { + allowedMentions: AllowedMentions.none() + }); + } + const allowedRoles = this.roleWhitelist[mappedRole.name].map(r => { + return this.client.util.moulberryBushRoleMap.find(m => m.name === r).id; + }); + if (!message.member.roles.cache.some(role => allowedRoles.includes(role.id))) { + return message.util.reply(`<:error:837123021016924261> <@&${role.id}> is whitelisted, but you do not have any of the roles required to manage it.`, { + allowedMentions: AllowedMentions.none() + }); + } + } + if (!this.client.ownerID.includes(message.author.id)) { + if (role.comparePositionTo(message.member.roles.highest) >= 0) { + return message.util.reply(`<:error:837123021016924261> <@&${role.id}> is higher or equal to your highest role.`, { + allowedMentions: AllowedMentions.none() + }); + } + if (role.comparePositionTo(message.guild.me.roles.highest) >= 0) { + return message.util.reply(`<:error:837123021016924261> <@&${role.id}> is higher or equal to my highest role.`, { + allowedMentions: AllowedMentions.none() + }); + } + if (role.managed) { + await message.util.reply(`<:error:837123021016924261> <@&${role.id}> is managed by an integration and cannot be managed.`, { + allowedMentions: AllowedMentions.none() + }); + } + } + // no checks if the user has MANAGE_ROLES + if (action == 'remove') { + const success = await user.roles.remove(role.id).catch(() => { }); + if (success) + return message.util.reply(`<:checkmark:837109864101707807> Successfully removed <@&${role.id}> from <@${user.id}>!`, { allowedMentions: AllowedMentions.none() }); + else return message.util.reply(`<:error:837123021016924261> Could not remove <@&${role.id}> from <@${user.id}>.`, { allowedMentions: AllowedMentions.none() }); + } else if (action == 'add') { + const success = await user.roles.add(role.id).catch(() => { }); + if (success) { + return message.util.reply(`<:checkmark:837109864101707807> Successfully added <@&${role.id}> to <@${user.id}>!`, { allowedMentions: AllowedMentions.none() }); + } else return message.util.reply(`<:error:837123021016924261> Could not add <@&${role.id}> to <@${user.id}>.`, { allowedMentions: AllowedMentions.none() }); + } + } +} \ No newline at end of file diff --git a/src/lib/extensions/Util.ts b/src/lib/extensions/Util.ts index 771876b..f4e1315 100644 --- a/src/lib/extensions/Util.ts +++ b/src/lib/extensions/Util.ts @@ -16,10 +16,10 @@ export interface uuidRes { uuid: string; username: string; username_history?: - | { - username: string; - }[] - | null; + | { + username: string; + }[] + | null; textures: { custom: boolean; slim: boolean; @@ -271,4 +271,32 @@ export class Util extends ClientUtil { .json()) as uuidRes; return apiRes.uuid; } + + public moulberryBushRoleMap = [ + { name: '*', id: '792453550768390194' }, + { name: 'Admin Perms', id: '746541309853958186' }, + { name: 'Sr. Moderator', id: '782803470205190164' }, + { name: 'Moderator', id: '737308259823910992' }, + { name: 'Helper', id: '737440116230062091' }, + { name: 'Trial Helper', id: '783537091946479636' }, + { name: 'Contributor', id: '694431057532944425' }, + { name: 'Giveaway Donor', id: '784212110263451649' }, + { name: 'Giveaway (200m)', id: '810267756426690601' }, + { name: 'Giveaway (100m)', id: '801444430522613802' }, + { name: 'Giveaway (50m)', id: '787497512981757982' }, + { name: 'Giveaway (25m)', id: '787497515771232267' }, + { name: 'Giveaway (10m)', id: '787497518241153025' }, + { name: 'Giveaway (5m)', id: '787497519768403989' }, + { name: 'Giveaway (1m)', id: '787497521084891166' }, + { name: 'Suggester', id: '811922322767609877' }, + { name: 'Partner', id: '767324547312779274' }, + { name: 'Level Locked', id: '784248899044769792' }, + { name: 'No Files', id: '786421005039173633' }, + { name: 'No Reactions', id: '786421270924361789' }, + { name: 'No Links', id: '786421269356740658' }, + { name: 'No Bots', id: '786804858765312030' }, + { name: 'No VC', id: '788850482554208267' }, + { name: 'No Giveaways', id: '808265422334984203' }, + { name: 'No Support', id: '790247359824396319' } + ]; } -- cgit