aboutsummaryrefslogtreecommitdiff
path: root/src/commands/config/blacklist.ts
blob: ef0c94e847fbd71c19cf6b845373ff5f99c864ba (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { AllowedMentions, BushCommand, BushMessage, BushSlashMessage, Global } from '@lib';
import { Argument } from 'discord-akairo';
import { Channel, User } from 'discord.js';

export default class BlacklistCommand extends BushCommand {
	public constructor() {
		super('blacklist', {
			aliases: ['blacklist', 'unblacklist'],
			category: 'config',
			description: {
				content: 'A command to blacklist users and channels.',
				usage: 'blacklist|unblacklist <user|channel>',
				examples: ['blacklist @user', 'unblacklist #channel']
			},
			args: [
				{
					id: 'target',
					customType: Argument.union('channel', 'user'),
					prompt: {
						start: 'What channel or user that you would like to blacklist/unblacklist?',
						retry: '{error} Pick a valid command.',
						optional: false
					}
				},
				{
					id: 'global',
					match: 'flag',
					flag: '--global'
				}
			],
			slash: true,
			slashOptions: [
				{
					name: 'action',
					description: 'Would you like to add or remove someone or something from/to the blacklist?',
					type: 'STRING',
					choices: [
						{ name: 'blacklist', value: 'blacklist' },
						{ name: 'unblacklist', value: 'unblacklist' }
					],
					required: true
				},
				{
					name: 'target',
					description: 'What channel or user that you would like to blacklist/unblacklist?',
					type: 'STRING',
					required: true
				}
			],
			channel: 'guild',
			clientPermissions: (m) => util.clientSendAndPermCheck(m),
			userPermissions: ['MANAGE_GUILD']
		});
	}

	public override async exec(
		message: BushMessage | BushSlashMessage,
		args: { action: 'blacklist' | 'unblacklist'; target: Channel | User | string; global: boolean }
	): Promise<unknown> {
		let action: 'blacklist' | 'unblacklist' | 'toggle' =
			args.action ?? (message?.util?.parsed?.alias as 'blacklist' | 'unblacklist') ?? 'toggle';
		const global = args.global && message.author.isOwner();
		const target =
			typeof args.target === 'string'
				? (await util.arg.cast('channel', message, args.target)) ?? (await util.arg.cast('user', message, args.target))
				: args.target;
		if (!target) return await message.util.reply(`${util.emojis.error} Choose a valid channel or user.`);
		const targetID = target.id;

		if (global) {
			if ((action as 'blacklist' | 'unblacklist' | 'toggle') === 'toggle') {
				const globalDB =
					(await Global.findByPk(client.config.environment)) ??
					(await Global.create({ environment: client.config.environment }));
				const blacklistedUsers = globalDB.blacklistedUsers;
				const blacklistedChannels = globalDB.blacklistedChannels;
				action = blacklistedUsers.includes(targetID) || blacklistedChannels.includes(targetID) ? 'unblacklist' : 'blacklist';
			}
			const success = await util
				.insertOrRemoveFromGlobal(
					action === 'blacklist' ? 'add' : 'remove',
					target instanceof User ? 'blacklistedUsers' : 'blacklistedChannels',
					targetID
				)
				.catch(() => false);
			if (!success)
				return await message.util.reply({
					content: `${util.emojis.error} There was an error globally ${action}ing ${util.format.bold(
						target?.tag ?? target.name
					)}.`,
					allowedMentions: AllowedMentions.none()
				});
			else
				return await message.util.reply({
					content: `${util.emojis.success} Successfully **${action}ed** ${util.format.bold(
						target?.tag ?? target.name
					)} globally.`,
					allowedMentions: AllowedMentions.none()
				});
			// guild disable
		} else {
			if (!message.guild)
				return await message.util.reply(`${util.emojis.error} You have to be in a guild to disable commands.`);
			const blacklistedChannels = (await message.guild.getSetting('blacklistedChannels')) ?? [];
			const blacklistedUsers = (await message.guild.getSetting('blacklistedUsers')) ?? [];
			if ((action as 'blacklist' | 'unblacklist' | 'toggle') === 'toggle') {
				action = blacklistedChannels.includes(targetID) ?? blacklistedUsers.includes(targetID) ? 'unblacklist' : 'blacklist';
			}
			const newValue = util.addOrRemoveFromArray(
				action === 'blacklist' ? 'add' : 'remove',
				target instanceof User ? blacklistedUsers : blacklistedChannels,
				targetID
			);
			const success = await message.guild
				.setSetting(target instanceof User ? 'blacklistedUsers' : 'blacklistedChannels', newValue, message.member!)
				.catch(() => false);
			if (!success)
				return await message.util.reply({
					content: `${util.emojis.error} There was an error ${action}ing ${util.format.bold(target?.tag ?? target.name)}.`,
					allowedMentions: AllowedMentions.none()
				});
			else
				return await message.util.reply({
					content: `${util.emojis.success} Successfully ${action}ed ${util.format.bold(target?.tag ?? target.name)}.`,
					allowedMentions: AllowedMentions.none()
				});
		}
	}
}