aboutsummaryrefslogtreecommitdiff
path: root/src/lib/common/DeleteButton.ts
blob: 4874f785dcc1a50572351a27c982b17e5621c7fe (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
import { PaginateEmojis, type BushMessage, type BushSlashMessage } from '#lib';
import { CommandUtil } from 'discord-akairo';
import {
	ActionRowBuilder,
	ButtonBuilder,
	ButtonStyle,
	MessageComponentInteraction,
	MessageEditOptions,
	MessagePayload,
	type MessageOptions
} from 'discord.js';

/**
 * Sends a message with a button for the user to delete it.
 */
export class DeleteButton {
	/**
	 * Options for sending the message
	 */
	protected messageOptions: MessageOptions;

	/**
	 * The message that triggered the command
	 */
	protected message: BushMessage | BushSlashMessage;

	/**
	 * @param message The message to respond to
	 * @param options The send message options
	 */
	protected constructor(message: BushMessage | BushSlashMessage, options: MessageOptions) {
		this.message = message;
		this.messageOptions = options;
	}

	/**
	 * Sends a message with a button for the user to delete it.
	 */
	protected async send() {
		this.updateComponents();

		const msg = (await this.message.util.reply(this.messageOptions)) as BushMessage;

		const collector = msg.createMessageComponentCollector({
			filter: (interaction) => interaction.customId == 'paginate__stop' && interaction.message?.id == msg.id,
			time: 300000
		});

		collector.on('collect', async (interaction: MessageComponentInteraction) => {
			await interaction.deferUpdate().catch(() => undefined);
			if (interaction.user.id == this.message.author.id || client.config.owners.includes(interaction.user.id)) {
				if (msg.deletable && !CommandUtil.deletedMessages.has(msg.id)) await msg.delete();
			}
		});

		collector.on('end', async () => {
			this.updateComponents(true, true);
			await msg.edit(<string | MessagePayload | MessageEditOptions>this.messageOptions).catch(() => undefined);
		});
	}

	/**
	 * Generates the components for the message
	 * @param edit Whether or not the message is being edited
	 * @param disable Whether or not to disable the buttons
	 */
	protected updateComponents(edit = false, disable = false): void {
		this.messageOptions.components = [
			new ActionRowBuilder<ButtonBuilder>().addComponents(
				new ButtonBuilder({
					style: ButtonStyle.Primary,
					customId: 'paginate__stop',
					emoji: PaginateEmojis.STOP,
					disabled: disable
				})
			)
		];
		if (edit) {
			this.messageOptions.reply = undefined;
		}
	}

	/**
	 * Sends a message with a button for the user to delete it.
	 * @param message The message to respond to
	 * @param options The send message options
	 */
	public static async send(message: BushMessage | BushSlashMessage, options: Omit<MessageOptions, 'components'>) {
		return new DeleteButton(message, options).send();
	}
}