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", });