aboutsummaryrefslogtreecommitdiff
path: root/src/commands/config/prefix.ts
blob: 3bb717bd01bdef6123e275cdfe6cc71684a6f3b3 (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
import { Guild as DiscordGuild, Message } from 'discord.js';
import { SlashCommandOption } from '../../lib/extensions/BushClientUtil';
import { BushCommand } from '../../lib/extensions/BushCommand';
import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage';
import { Guild } from '../../lib/models';

export default class PrefixCommand extends BushCommand {
	constructor() {
		super('prefix', {
			aliases: ['prefix'],
			category: 'config',
			args: [
				{
					id: 'prefix'
				}
			],
			userPermissions: ['MANAGE_GUILD'],
			description: {
				content: 'Set the prefix of the current server (resets to default if prefix is not given)',
				usage: 'prefix [prefix]',
				examples: ['prefix', 'prefix +']
			},
			slashOptions: [
				{
					type: 'STRING',
					name: 'prefix',
					description: 'The prefix to set for this server',
					required: false
				}
			],
			slash: true
		});
	}

	async changePrefix(guild: DiscordGuild, prefix?: string): Promise<void> {
		let row = await Guild.findByPk(guild.id);
		if (!row) {
			row = Guild.build({
				id: guild.id
			});
		}
		// this.client.console.debug(row);
		if (prefix) {
			row.prefix = prefix;
			await row.save();
		} else {
			const row = await Guild.findByPk(guild.id);
			row.prefix = this.client.config.prefix;
			await row.save();
		}
	}

	async exec(message: Message, { prefix }: { prefix?: string }): Promise<void> {
		await this.changePrefix(message.guild, prefix);
		if (prefix) {
			await message.util.send(`Sucessfully set prefix to \`${prefix}\``);
		} else {
			await message.util.send(`Sucessfully reset prefix to \`${this.client.config.prefix}\``);
		}
	}

	async execSlash(message: BushInteractionMessage, { prefix }: { prefix?: SlashCommandOption<string> }): Promise<void> {
		await this.changePrefix(message.guild, prefix?.value);
		if (prefix) {
			await message.reply(`Sucessfully set prefix to \`${prefix.value}\``);
		} else {
			await message.reply(`Sucessfully reset prefix to \`${this.client.config.prefix}\``);
		}
	}
}