diff options
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/moderation/ban.ts | 90 | ||||
-rw-r--r-- | src/commands/moderation/kick.ts | 72 | ||||
-rw-r--r-- | src/commands/moderation/role.ts | 109 |
3 files changed, 156 insertions, 115 deletions
diff --git a/src/commands/moderation/ban.ts b/src/commands/moderation/ban.ts index 107de9d..556dd6b 100644 --- a/src/commands/moderation/ban.ts +++ b/src/commands/moderation/ban.ts @@ -5,8 +5,8 @@ import { Ban, Modlog, ModlogType } from '../../lib/models'; import moment from 'moment'; import { Message } from 'discord.js'; import { CommandInteraction } from 'discord.js'; -// import { SlashCommandOption } from '../../lib/extensions/Util'; -// import { ApplicationCommandOptionType } from 'discord-api-types'; +import { SlashCommandOption } from '../../lib/extensions/Util'; +import { ApplicationCommandOptionType } from 'discord-api-types'; const durationAliases: Record<string, string[]> = { weeks: ['w', 'weeks', 'week', 'wk', 'wks'], @@ -51,28 +51,28 @@ export default class PrefixCommand extends BotCommand { 'ban @Tyman being cool', 'ban @Tyman being cool --time 7days' ] - } - // slashCommandOptions: [ - // { - // type: ApplicationCommandOptionType.USER, - // name: 'user', - // description: 'The user to ban', - // required: true - // }, - // { - // type: ApplicationCommandOptionType.STRING, - // name: 'reason', - // description: 'The reason to show in modlogs and audit log', - // required: false - // }, - // { - // type: ApplicationCommandOptionType.STRING, - // name: 'time', - // description: - // 'The time the user should be banned for (default permanent)', - // required: false - // } - // ] + }, + slashCommandOptions: [ + { + type: ApplicationCommandOptionType.USER, + name: 'user', + description: 'The user to ban', + required: true + }, + { + type: ApplicationCommandOptionType.STRING, + name: 'reason', + description: 'The reason to show in modlogs and audit log', + required: false + }, + { + type: ApplicationCommandOptionType.STRING, + name: 'time', + description: + 'The time the user should be banned for (default permanent)', + required: false + } + ] }); } async *genResponses( @@ -199,25 +199,25 @@ export default class PrefixCommand extends BotCommand { } } - // async execSlash( - // message: CommandInteraction, - // { - // user, - // reason, - // time - // }: { - // user: SlashCommandOption<undefined>; - // reason: SlashCommandOption<string>; - // time: SlashCommandOption<string>; - // } - // ): Promise<void> { - // for await (const response of this.genResponses( - // message, - // user.user, - // reason?.value, - // time?.value - // )) { - // await message.reply(response); - // } - // } + async execSlash( + message: CommandInteraction, + { + user, + reason, + time + }: { + user: SlashCommandOption<undefined>; + reason: SlashCommandOption<string>; + time: SlashCommandOption<string>; + } + ): Promise<void> { + for await (const response of this.genResponses( + message, + user.user, + reason?.value, + time?.value + )) { + await message.reply(response); + } + } } diff --git a/src/commands/moderation/kick.ts b/src/commands/moderation/kick.ts index 7b04d5a..a16e4b0 100644 --- a/src/commands/moderation/kick.ts +++ b/src/commands/moderation/kick.ts @@ -1,8 +1,10 @@ import { BotCommand } from '../../lib/extensions/BotCommand'; import { Guild, Modlog, ModlogType } from '../../lib/models'; import { GuildMember, Message } from 'discord.js'; +import { ApplicationCommandOptionType } from 'discord-api-types'; +import { CommandInteraction } from 'discord.js'; -export default class PrefixCommand extends BotCommand { +export default class KickCommand extends BotCommand { constructor() { super('kick', { aliases: ['kick'], @@ -25,13 +27,29 @@ export default class PrefixCommand extends BotCommand { content: 'Kick a member and log it in modlogs', usage: 'kick <member> <reason>', examples: ['kick @Tyman being cool'] - } + }, + slashCommandOptions: [ + { + type: ApplicationCommandOptionType.USER, + name: 'user', + description: 'The user to kick', + required: true + }, + { + type: ApplicationCommandOptionType.STRING, + name: 'reason', + description: 'The reason to show in modlogs and audit log', + required: false + } + ] }); } - async exec( - message: Message, - { user, reason }: { user: GuildMember; reason?: string } - ): Promise<void> { + + private async *genResponses( + message: Message | CommandInteraction, + user: GuildMember, + reason?: string + ): AsyncIterable<string> { let modlogEnry: Modlog; // Create guild entry so postgres doesn't get mad when I try and add a modlog entry await Guild.findOrCreate({ @@ -46,16 +64,14 @@ export default class PrefixCommand extends BotCommand { modlogEnry = Modlog.build({ user: user.id, guild: message.guild.id, - moderator: message.author.id, + moderator: message instanceof Message ? message.author.id : message.user.id, type: ModlogType.KICK, reason }); await modlogEnry.save(); } catch (e) { console.error(e); - await message.util.send( - 'Error saving to database. Please report this to a developer.' - ); + yield 'Error saving to database. Please report this to a developer.' return; } try { @@ -65,21 +81,45 @@ export default class PrefixCommand extends BotCommand { }\`` ); } catch (e) { - await message.channel.send('Error sending message to user'); + yield 'Error sending message to user'; } try { await user.kick( - `Kicked by ${message.author.tag} with ${ + `Kicked by ${message instanceof Message ? message.author.tag : message.user.tag} with ${ reason ? `reason ${reason}` : 'no reason' }` ); } catch { - await message.util.send('Error kicking :/'); + yield 'Error kicking :/'; await modlogEnry.destroy(); return; } - await message.util.send( - `Kicked <@!${user.id}> with reason \`${reason || 'No reason given'}\`` - ); + yield `Kicked <@!${user.id}> with reason \`${reason || 'No reason given'}\`` + } + + async exec( + message: Message, + { user, reason }: { user: GuildMember; reason?: string } + ): Promise<void> { + for await (const response of this.genResponses( + message, + user, + reason + )) { + await message.util.send(response); + } + } + + async execSlash( + message: CommandInteraction, + { user, reason }: { user: GuildMember; reason?: string } + ): Promise<void> { + for await (const response of this.genResponses( + message, + user, + reason + )) { + await message.reply(response); + } } } diff --git a/src/commands/moderation/role.ts b/src/commands/moderation/role.ts index e799698..daa575d 100644 --- a/src/commands/moderation/role.ts +++ b/src/commands/moderation/role.ts @@ -2,6 +2,7 @@ import { BotCommand } from '../../lib/extensions/BotCommand'; import AllowedMentions from '../../lib/utils/AllowedMentions'; import { Message, Role, GuildMember } from 'discord.js'; +import { ApplicationCommandOptionType } from 'discord-api-types'; export default class RoleCommand extends BotCommand { private roleWhitelist: Record<string, string[]> = { @@ -49,51 +50,49 @@ export default class RoleCommand extends BotCommand { }, clientPermissions: ['MANAGE_ROLES', 'EMBED_LINKS', 'SEND_MESSAGES'], channel: 'guild', - typing: true + typing: true, + args: [ + { + id: 'user', + type: 'member', + prompt: { + start: `What user do you want to add/remove the role on?`, + retry: `<:error:837123021016924261> Choose a valid user to add/remove the role on.` + } + }, + { + id: 'role', + type: 'role', + match: 'restContent', + prompt: { + start: `What role do you want to add/remove?`, + retry: `<:error:837123021016924261> Choose a valid role to add/remove.` + } + } + ], + slashCommandOptions: [ + { + type: ApplicationCommandOptionType.USER, + name: 'user', + description: 'The user to add/remove the role on', + required: true + }, + { + type: ApplicationCommandOptionType.ROLE, + name: 'role', + description: 'The role to add/remove', + required: 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 } + }: { user: GuildMember; role: Role } ): Promise<unknown> { if ( !message.member.permissions.has('MANAGE_ROLES') && @@ -153,31 +152,33 @@ export default class RoleCommand extends BotCommand { ); } } - // no checks if the user has MANAGE_ROLES - if (action == 'remove') { - const success = await user.roles.remove(role.id).catch(() => undefined); - if (success) - return message.util.reply( - `<:checkmark:837109864101707807> Successfully removed <@&${role.id}> from <@${user.id}>!`, - { allowedMentions: AllowedMentions.none() } - ); - else + // No checks if the user has MANAGE_ROLES + if (user.roles.cache.has(role.id)) { + try { + await user.roles.remove(role.id); + } catch { 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(() => undefined); - if (success) { - return message.util.reply( - `<:checkmark:837109864101707807> Successfully added <@&${role.id}> to <@${user.id}>!`, - { allowedMentions: AllowedMentions.none() } - ); - } else + } + return message.util.reply( + `<:checkmark:837109864101707807> Successfully removed <@&${role.id}> from <@${user.id}>!`, + { allowedMentions: AllowedMentions.none() } + ); + } else { + try { + await user.roles.add(role.id); + } catch { return message.util.reply( `<:error:837123021016924261> Could not add <@&${role.id}> to <@${user.id}>.`, { allowedMentions: AllowedMentions.none() } ); + } + return message.util.reply( + `<:checkmark:837109864101707807> Successfully added <@&${role.id}> to <@${user.id}>!`, + { allowedMentions: AllowedMentions.none() } + ); } } } |