aboutsummaryrefslogtreecommitdiff
path: root/src/lib/extensions/BotGuild.ts
blob: 22d783481d9845ae2e6f378fdffdd87e3af79038 (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
import { Guild, Structures } from 'discord.js';
import { BotClient } from './BotClient';
import { Guild as GuildModel } from '../types/Models';

export class GuildSettings {
	private guild: BotGuild;
	constructor(guild: BotGuild) {
		this.guild = guild;
	}
	public async getPrefix(): Promise<string> {
		return await GuildModel.findByPk(this.guild.id).then(
			(gm) => gm?.prefix || this.guild.client.config.prefix
		);
	}
	public async setPrefix(value: string): Promise<void> {
		let entry = await GuildModel.findByPk(this.guild.id);
		if (!entry) {
			entry = GuildModel.build({
				id: this.guild.id,
				prefix: value
			});
		} else {
			entry.prefix = value;
		}
		await entry.save();
	}
}

export class BotGuild extends Guild {
	constructor(client: BotClient, data: Record<string, unknown>) {
		super(client, data);
	}
	static install(): void {
		Structures.extend('Guild', () => BotGuild);
	}
	public settings = new GuildSettings(this);
	public client: BotClient;
}