aboutsummaryrefslogtreecommitdiff
path: root/src/commands/config/settings.ts
blob: a8070e28a03c165cbf7f43d68afd354fb4b074f1 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { BushCommand, BushMessage, BushSlashMessage, guildSettingsObj, settingsArr } from '@lib';
import {
	Message,
	MessageActionRow,
	MessageButton,
	MessageComponentInteraction,
	MessageEmbed,
	MessageOptions,
	MessageSelectMenu
} from 'discord.js';

export default class SettingsCommand extends BushCommand {
	public constructor() {
		super('settings', {
			aliases: ['settings', 'setting', 'configure', 'config'],
			category: 'config',
			description: {
				content: 'Configure server options.',
				usage: 'settings',
				examples: ['settings']
			},
			slash: true,
			slashOptions: settingsArr.map((setting) => {
				return {
					name: util.camelToSnakeCase(setting),
					description: `Manage the server's ${guildSettingsObj[setting].name.toLowerCase()}`,
					type: 'SUB_COMMAND_GROUP',
					options: guildSettingsObj[setting].type.includes('-array')
						? [
								{
									name: 'view',
									description: `View the server's ${guildSettingsObj[setting].name.toLowerCase()}.`,
									type: 'SUB_COMMAND'
								},
								{
									name: 'add',
									description: `Add a value to the server's ${guildSettingsObj[setting].name.toLowerCase()}.`,
									type: 'SUB_COMMAND',
									options: [
										{
											name: 'value',
											description: `What would you like to add to the server's ${guildSettingsObj[
												setting
											].name.toLowerCase()}?'`,
											type: guildSettingsObj[setting].type.replace('-array', '').toUpperCase() as 'ROLE' | 'STRING' | 'CHANNEL',
											required: true
										}
									]
								},
								{
									name: 'remove',
									description: `Remove a value from the server's ${guildSettingsObj[setting].name.toLowerCase()}.`,
									type: 'SUB_COMMAND',
									options: [
										{
											name: 'value',
											description: `What would you like to remove from the server's ${guildSettingsObj[
												setting
											].name.toLowerCase()}?'`,
											type: guildSettingsObj[setting].type.replace('-array', '').toUpperCase() as 'ROLE' | 'STRING' | 'CHANNEL',
											required: true
										}
									]
								}
						  ]
						: [
								{
									name: 'view',
									description: `View the server's ${guildSettingsObj[setting].name.toLowerCase()}.`,
									type: 'SUB_COMMAND'
								},
								{
									name: 'set',
									description: `Set the server's ${guildSettingsObj[setting].name.toLowerCase()}.`,
									type: 'SUB_COMMAND',
									options: [
										{
											name: 'value',
											description: `What would you like to set the server's ${guildSettingsObj[
												setting
											].name.toLowerCase()} to?'`,
											type: guildSettingsObj[setting].type.toUpperCase() as 'ROLE' | 'STRING' | 'CHANNEL',
											required: true
										}
									]
								}
						  ]
				};
			}),
			slashGuilds: ['516977525906341928', '812400566235430912'],
			channel: 'guild',
			clientPermissions: ['SEND_MESSAGES'],
			userPermissions: ['SEND_MESSAGES', 'MANAGE_GUILD'],
			ownerOnly: true
		});
	}

	// *args(): any {}

	public override async exec(message: BushMessage | BushSlashMessage, args: unknown): Promise<unknown> {
		client.console.debugRaw(message.interaction);
		client.console.debugRaw(args);
		if (!message.guild) return await message.util.reply(`${util.emojis.error} This command can only be used in servers.`);
		const messageOptions = await this.generateMessageOptions(message);
		const msg = (await message.util.reply(messageOptions)) as Message;
		const collector = msg.createMessageComponentCollector({
			channel: message.channel ?? undefined,
			guild: message.guild,
			message: message as Message,
			time: 300_000
		});

		collector.on('collect', async (interaction: MessageComponentInteraction) => {
			if (interaction.user.id === message.author.id || client.config.owners.includes(interaction.user.id)) {
				if (!message.guild) throw new Error('message.guild is null');
				switch (interaction.customId) {
					case 'command_settingsSel': {
						if (!interaction.isSelectMenu()) return;

						return interaction.update(
							await this.generateMessageOptions(message, interaction.values[0] as keyof typeof guildSettingsObj)
						);
					}
				}
			} else {
				return await interaction?.deferUpdate().catch(() => undefined);
			}
		});
	}

	public async generateMessageOptions(
		message: BushMessage | BushSlashMessage,
		feature?: keyof typeof guildSettingsObj
	): Promise<MessageOptions> {
		if (!message.guild) throw new Error('message.guild is null');
		const settingsEmbed = new MessageEmbed().setTitle(`${message.guild!.name}'s Settings`).setColor(util.colors.default);
		if (!feature) {
			const desc = settingsArr.map((s) => `**${guildSettingsObj[s].name}**`).join('\n');
			settingsEmbed.setDescription(desc);

			const selMenu = new MessageActionRow().addComponents(
				new MessageSelectMenu()
					.addOptions(
						...settingsArr.map((s) => ({
							label: guildSettingsObj[s].name,
							value: s,
							description: guildSettingsObj[s].description
						}))
					)
					.setPlaceholder('Select A Setting to View')
					.setMaxValues(1)
					.setMinValues(1)
					.setCustomId('command_settingsSel')
			);
			return { embeds: [settingsEmbed], components: [selMenu] };
		} else {
			const generateCurrentValue = async (
				type: 'string' | 'channel' | 'channel-array' | 'role' | 'role-array'
			): Promise<string> => {
				const feat = await message.guild!.getSetting(feature);
				switch (type.replace('-array', '') as 'string' | 'channel' | 'role') {
					case 'string': {
						return Array.isArray(feat)
							? feat.map((feat) => util.discord.escapeInlineCode(util.inspectAndRedact(feat))).join('\n')
							: util.discord.escapeInlineCode(util.inspectAndRedact(feat));
					}
					case 'channel': {
						return Array.isArray(feat) ? feat.map((feat) => `<#${feat}>`).join('\n') : `<#${feat}>`;
					}
					case 'role': {
						return Array.isArray(feat) ? feat.map((feat) => `<@&${feat}>`).join('\n') : `<@&${feat}>`;
					}
				}
			};
			const components = new MessageActionRow().addComponents(
				new MessageButton().setStyle('PRIMARY').setCustomId('command_settingsBack').setLabel('Back')
			);
			settingsEmbed.setDescription(guildSettingsObj[feature].description);

			settingsEmbed.setFooter(
				`Run "${message.util.isSlash ? '/' : await message.guild.getSetting('prefix')}settings ${feature} ${
					guildSettingsObj[feature].type.includes('-array') ? 'add/remove' : 'set'
				} <value>" to set this setting.`
			);
			settingsEmbed.addField(
				guildSettingsObj[feature].name,
				await generateCurrentValue(feature as 'string' | 'channel' | 'channel-array' | 'role' | 'role-array')
			);
			return { embeds: [settingsEmbed], components: [components] };
		}
	}
}