aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moderation/unban.ts
blob: 3a6221a891e3d77ee1363c77a30dba52c14643d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import {
	AllowedMentions,
	Arg,
	BushCommand,
	emojis,
	format,
	unbanResponse,
	type ArgType,
	type CommandMessage,
	type OptArgType,
	type SlashMessage,
	type UnbanResponse
} from '#lib';
import assert from 'assert/strict';
import { ApplicationCommandOptionType, PermissionFlagsBits, type User } from 'discord.js';

export default class UnbanCommand extends BushCommand {
	public constructor() {
		super('unban', {
			aliases: ['unban'],
			category: 'moderation',
			description: 'Unban a member from the server.',
			usage: ['unban <member> <reason>'],
			examples: ['unban 322862723090219008 I changed my mind, commands are allowed in #general'],
			args: [
				{
					id: 'user',
					description: 'The user to unban.',
					type: Arg.union('user', 'globalUser'),
					prompt: 'What user would you like to unban?',
					retry: '{error} Choose a valid user to unban.',
					slashType: ApplicationCommandOptionType.User
				},
				{
					id: 'reason',
					description: 'The reason for the unban',
					type: 'string',
					match: 'restContent',
					prompt: 'Why should this user be unbanned?',
					retry: '{error} Choose a valid unban reason.',
					optional: true,
					slashType: ApplicationCommandOptionType.String
				}
			],
			slash: true,
			channel: 'guild',
			clientPermissions: [PermissionFlagsBits.BanMembers],
			userPermissions: [PermissionFlagsBits.BanMembers]
		});
	}

	public override async exec(
		message: CommandMessage | SlashMessage,
		{ user, reason }: { user: ArgType<'user' | 'globalUser'>; reason: OptArgType<'string'> }
	) {
		assert(message.inGuild());

		const responseCode = await message.guild.bushUnban({
			user,
			moderator: message.author,
			reason
		});

		return await message.util.reply({
			content: UnbanCommand.formatCode(user, responseCode),
			allowedMentions: AllowedMentions.none()
		});
	}

	public static formatCode(user: User, code: UnbanResponse): string {
		const victim = format.input(user.tag);
		switch (code) {
			case unbanResponse.MISSING_PERMISSIONS:
				return `${emojis.error} Could not unban ${victim} because I am missing the **Ban Members** permission.`;
			case unbanResponse.ACTION_ERROR:
				return `${emojis.error} An error occurred while trying to unban ${victim}.`;
			case unbanResponse.PUNISHMENT_ENTRY_REMOVE_ERROR:
				return `${emojis.error} While unbanning ${victim}, there was an error removing their ban entry, please report this to my developers.`;
			case unbanResponse.MODLOG_ERROR:
				return `${emojis.error} While unbanning ${victim}, there was an error creating a modlog entry, please report this to my developers.`;
			case unbanResponse.NOT_BANNED:
				return `${emojis.warn} ${victim} is not banned but I tried to unban them anyways.`;
			case unbanResponse.DM_ERROR:
			case unbanResponse.SUCCESS:
				return `${emojis.success} Successfully unbanned ${victim}.`;
			default:
				return `${emojis.error} An error occurred: ${format.input(code)}}`;
		}
	}
}