aboutsummaryrefslogtreecommitdiff
path: root/src/lib/extensions/BotMessage.ts
blob: 70d4478fb01704bfca9af9a80d396ede903bbcf8 (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
import {
	TextChannel,
	NewsChannel,
	DMChannel,
	Message,
	Structures
} from 'discord.js';
import { BotClient } from './BotClient';
import { Guild as GuildModel } from '../models';
import { BotGuild } from './BotGuild';

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

export class BotMessage extends Message {
	constructor(
		client: BotClient,
		data: Record<string, unknown>,
		channel: TextChannel | DMChannel | NewsChannel
	) {
		super(client, data, channel);
	}
	public guild: BotGuild;
	public client: BotClient;
	static install(): void {
		Structures.extend('Message', () => BotMessage);
	}
	public settings = new GuildSettings(this);
}