all repos — stealth-developers @ adf2a1f4e94a5e420bad1b1b4b48ffdae274a4f9

apps/bot/src/preconditions/moderator.ts (view raw)

 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 type { ChatInputCommandInteraction, ContextMenuCommandInteraction } from "discord.js";
import { container, Precondition } from "@sapphire/framework";
import { getActorByDiscordIdAndGuildDiscordId } from "@stealth-developers/db";

export class ModeratorCheckPrecondition extends Precondition {
	public override async chatInputRun(interaction: ChatInputCommandInteraction) {
		if (!interaction.inGuild())
			return this.error({ message: "This command can only be used in a server." });

		const actor = await getActorByDiscordIdAndGuildDiscordId(
			interaction.user.id,
			interaction.guildId,
		);
		if (actor?.moderator) return this.ok();

		return this.error({
			message: "This command can only be usedby moderators.",
			identifier: "MODERATOR_ONLY",
		});
	}

	public override async contextMenuRun(interaction: ContextMenuCommandInteraction) {
		if (!interaction.inGuild())
			return this.error({ message: "This command can only be used in a server." });

		const actor = await getActorByDiscordIdAndGuildDiscordId(
			interaction.user.id,
			interaction.guildId,
		);

		if (actor?.moderator) return this.ok();

		return this.error({
			message: "This option can only be usedby moderators.",
			identifier: "MODERATOR_ONLY",
		});
	}
}

declare module "@sapphire/framework" {
	interface Preconditions {
		ModeratorCheck: never;
	}
}

container.stores.loadPiece({
	name: "ModeratorCheck",
	piece: ModeratorCheckPrecondition,
	store: "preconditions",
});