aboutsummaryrefslogtreecommitdiff
path: root/src/listeners/interaction/interactionCreate.ts
blob: ced359cb75b577b05cb16e1cba594f2182d9885b (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
import {
	BotListener,
	Emitter,
	emojis,
	format,
	formatList,
	handleAutomodInteraction,
	surroundEach,
	type BotClientEvents
} from '#lib';
import { Events, InteractionType } from 'discord.js';

export default class InteractionCreateListener extends BotListener {
	public constructor() {
		super('interactionCreate', {
			emitter: Emitter.Client,
			event: Events.InteractionCreate
		});
	}

	public async exec(...[interaction]: BotClientEvents[Events.InteractionCreate]) {
		if (!interaction) return;
		if ('customId' in interaction && (interaction as any)['customId'].startsWith('test')) return;
		void this.client.console.verbose(
			'interactionVerbose',
			`An interaction of type <<${InteractionType[interaction.type]}>> was received from <<${interaction.user.tag}>>.`
		);
		if (interaction.type === InteractionType.ApplicationCommand) {
			return;
		} else if (interaction.isButton()) {
			const id = interaction.customId;
			if (['paginate_', 'command_', 'confirmationPrompt_', 'appeal'].some((s) => id.startsWith(s))) return;
			else if (id.startsWith('automod;')) void handleAutomodInteraction(interaction);
			else if (id.startsWith('button-role;') && interaction.inCachedGuild()) {
				const [, roleId] = id.split(';');
				const role = interaction.guild.roles.cache.get(roleId);
				if (!role) return interaction.reply({ content: `${emojis.error} That role does not exist.`, ephemeral: true });
				const has = interaction.member.roles.cache.has(roleId);
				await interaction.deferReply({ ephemeral: true });
				if (has) {
					const success = await interaction.member.roles.remove(roleId).catch(() => false);
					if (success)
						return interaction.editReply({
							content: `${emojis.success} Removed the ${role} role from you.`,
							allowedMentions: {}
						});
					else
						return interaction.editReply({
							content: `${emojis.error} Failed to remove ${role} from you.`,
							allowedMentions: {}
						});
				} else {
					const success = await interaction.member.roles.add(roleId).catch(() => false);
					if (success)
						return interaction.editReply({
							content: `${emojis.success} Added the ${role} role to you.`,
							allowedMentions: {}
						});
					else
						return interaction.editReply({
							content: `${emojis.error} Failed to add ${role} to you.`,
							allowedMentions: {}
						});
				}
			} else return await interaction.reply({ content: 'Buttons go brrr', ephemeral: true });
		} else if (interaction.isSelectMenu()) {
			if (interaction.customId.startsWith('command_')) return;
			return await interaction.reply({
				content: `You selected ${
					Array.isArray(interaction.values)
						? formatList(surroundEach(interaction.values, '`'), 'and')
						: format.input(interaction.values)
				}.`,
				ephemeral: true
			});
		}
	}
}