Something went wrong. Try again.
A Discord bot designed to entertain your server esmbot.net
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373import type { ServerResponse } from "node:http";import { type AllowedMentions, type AnyTextableChannel, type Attachment, type Client, type CommandInteraction, Constants, type CreateMessageOptions, type Guild, type InteractionContent, type Locale, type Member, type Message, type MessageReference, Permission, type Role, TextableChannel, type Uncached, type User,} from "oceanic.js";import { getString } from "#utils/i18n.js";import { getEnv } from "#utils/runtime.js";import { cleanInteraction, cleanMessage } from "#utils/sanitize.js";import type { CommandFlag, CommandType } from "#utils/types.js";import type { DatabasePlugin } from "../database.ts";
type CommandOptionsClassic = { type: "classic"; cmdName: string; args: string[]; message: Message<Uncached | AnyTextableChannel>; content: string; specialArgs: { [key: string]: string | boolean | number; }; locale?: Locale;};
type CommandOptionsApplication = { type: "application"; interaction: CommandInteraction; res?: ServerResponse;};
export type CommandOptions = CommandOptionsClassic | CommandOptionsApplication;
class Command { client: Client; origOptions: CommandOptions; type: CommandType; success: boolean; args: string[]; locale: Locale; cmdName: string; author: User; permissions: Permission; memberPermissions: Permission;
database?: DatabasePlugin; message?: Message; interaction?: CommandInteraction; channel: AnyTextableChannel | ({ guildID?: string } & Uncached); guild: Guild | null; member?: Member | null; content?: string; reference?: { messageReference: MessageReference; allowedMentions: AllowedMentions }; res?: ServerResponse; private options?: { [key: string]: string | number | boolean } | null;
constructor(client: Client, database: DatabasePlugin | undefined, options: CommandOptions) { this.client = client; this.database = database; this.origOptions = options; this.type = options.type; this.success = true; if (options.type === "classic") { this.message = options.message; this.args = options.args; this.locale = options.locale ?? (getEnv("LOCALE") as Locale) ?? "en-US"; this.cmdName = options.cmdName; this.channel = options.message.channel ?? { id: options.message.channelID, guildID: options.message.guildID ?? undefined, }; this.guild = options.message.guild; this.author = options.message.author; this.member = options.message.member; if (this.channel instanceof TextableChannel && (!this.guild || this.guild.roles.size !== 0)) { this.permissions = this.channel.permissionsOf(client.user.id); } else { this.permissions = new Permission(Constants.AllPermissions); } this.memberPermissions = this.member?.permissions ?? new Permission(Constants.AllPermissions); this.content = options.content; this.options = options.specialArgs; this.reference = { messageReference: { channelID: this.message.channelID, messageID: this.message.id, guildID: this.message.guildID ?? undefined, failIfNotExists: false, }, allowedMentions: { repliedUser: false, }, }; } else { this.res = options.res; this.interaction = options.interaction; this.locale = options.interaction.locale as Locale; this.cmdName = options.interaction.data.options.raw.some((v) => v.type === 1 || v.type === 2) ? `${options.interaction.data.name} ${options.interaction.data.options.getSubCommand(true).join(" ")}` : options.interaction.data.name; this.args = []; this.channel = options.interaction.channel ?? { id: options.interaction.channelID, guildID: options.interaction.guildID ?? undefined, }; if ( !options.interaction.authorizingIntegrationOwners || options.interaction.authorizingIntegrationOwners[0] !== undefined ) { this.guild = options.interaction.guild; } else { this.guild = null; } this.author = options.interaction.user; this.member = options.interaction.member; this.permissions = options.interaction.appPermissions; this.memberPermissions = options.interaction.memberPermissions ?? new Permission(Constants.AllPermissions); } }
/** * The main command function. */ async run(): Promise<string | InteractionContent | CreateMessageOptions | undefined> { this.success = false; return this.getString(`commands.responses.${this.cmdName}.invalid`); }
/** * The function to run after sending the output. */ async finalize(_res?: Message) {}
/** * Replies to the message or interaction that invoked the command. */ async reply( content: InteractionContent | CreateMessageOptions, options: { ephemeral?: boolean; mention?: boolean } = {}, ): Promise<Message> { if (this.interaction) { let res; if (this.interaction.acknowledged) { res = await this.interaction.createFollowup({ flags: options.ephemeral ? 64 : 0, ...(content as InteractionContent), }); } else if (this.res && !this.res.writableEnded && !content.files) { // TODO: figure out a method to send files as an HTTP interaction response this.res.setHeader("Content-Type", "application/json"); this.res.end( JSON.stringify({ type: 4, data: { flags: options.ephemeral ? 64 : 0, ...(content as InteractionContent), components: content.components ? this.client.util.componentsToRaw(content.components) : undefined, embeds: content.embeds ? this.client.util.embedsToRaw(content.embeds) : undefined, }, }), ); this.interaction.acknowledged = true; } else { res = await this.interaction.createMessage({ flags: options.ephemeral ? 64 : 0, ...(content as InteractionContent), }); } return res?.getMessage() ?? this.interaction.getOriginal(); } if (this.message && this.reference) { return this.client.rest.channels.createMessage(this.message.channelID, { ...(content as CreateMessageOptions), ...this.reference, allowedMentions: { repliedUser: options.mention ?? false }, }); } throw Error("Unknown command type"); }
async acknowledge() { if (this.type === "classic" && this.message) { await this.client.rest.channels.sendTyping(this.message.channelID); } else if (this.interaction && !this.interaction.acknowledged) { const flags = (this.constructor as typeof Command).ephemeral || this.interaction.data.options.getBoolean("ephemeral", false) ? 64 : undefined; if (this.res && !this.res.writableEnded) { this.res.setHeader("Content-Type", "application/json"); this.res.end( JSON.stringify({ type: 5, data: { flags, }, }), ); this.interaction.acknowledged = true; } else { await this.interaction.defer(flags); } } }
getString(key: string, params?: { returnNull?: false; params?: { [key: string]: string } }): string; getString(key: string, params: { returnNull: boolean; params?: { [key: string]: string } }): string | undefined; getString(key: string, params?: { returnNull?: boolean; params?: { [key: string]: string } }): string | undefined { return getString(key, { locale: this.locale, returnNull: params?.returnNull ?? false, ...params, }); }
getOption(key: string, type: Constants.ApplicationCommandOptionTypes, defaultArg?: boolean) { switch (type) { case Constants.ApplicationCommandOptionTypes.STRING: return this.getOptionString(key, defaultArg); case Constants.ApplicationCommandOptionTypes.BOOLEAN: return this.getOptionBoolean(key, defaultArg); case Constants.ApplicationCommandOptionTypes.NUMBER: return this.getOptionNumber(key, defaultArg); case Constants.ApplicationCommandOptionTypes.INTEGER: return this.getOptionInteger(key, defaultArg); case Constants.ApplicationCommandOptionTypes.USER: return this.getOptionUser(key, defaultArg); case Constants.ApplicationCommandOptionTypes.ATTACHMENT: return this.getOptionAttachment(key); } }
getOptionString(key: string, defaultArg?: boolean): string | undefined { if (this.type === "classic") { return defaultArg ? this.args.join(" ").trim() : (this.options?.[key] as string); } if (this.type === "application") { return this.interaction?.data.options.getString(key); } throw Error("Unknown command type"); }
getOptionBoolean(key: string, defaultArg?: boolean): boolean | undefined { if (this.type === "classic") { const option = defaultArg ? this.args.join(" ").trim() : this.options?.[key]; if (option) return !!option; else return; } if (this.type === "application") { return this.interaction?.data.options.getBoolean(key); } throw Error("Unknown command type"); }
private clampClassicOption(key: string, value: number) { if (!Number.isFinite(value)) return Number.NaN; const flag = (this.constructor as typeof Command).flags.find((f) => f.name === key); if (flag?.minValue !== undefined) value = Math.max(flag.minValue, value); if (flag?.maxValue !== undefined) value = Math.min(flag.maxValue, value); return value; }
getOptionNumber(key: string, defaultArg?: boolean): number | undefined { if (this.type === "classic") { return this.clampClassicOption( key, Number.parseFloat((defaultArg ? this.args.join(" ").trim() : this.options?.[key]) as string), ); } if (this.type === "application") { return this.interaction?.data.options.getNumber(key); } throw Error("Unknown command type"); }
getOptionInteger(key: string, defaultArg?: boolean): number | undefined { if (this.type === "classic") { return this.clampClassicOption( key, Number.parseInt((defaultArg ? this.args.join(" ").trim() : this.options?.[key]) as string), ); } if (this.type === "application") { return this.interaction?.data.options.getInteger(key); } throw Error("Unknown command type"); }
getOptionUser(key: string, defaultArg?: boolean): User | undefined { if (this.type === "classic") { const id = defaultArg ? this.args.join(" ").trim() : this.options?.[key]; return this.client.users.get(id as string); } if (this.type === "application") { return this.interaction?.data.options.getUser(key); } throw Error("Unknown command type"); }
getOptionMember(key: string, defaultArg?: boolean): Member | undefined { if (this.type === "classic") { const id = defaultArg ? this.args.join(" ").trim() : this.options?.[key]; return this.guild?.members.get(id as string); } if (this.type === "application") { return this.interaction?.data.options.getMember(key); } throw Error("Unknown command type"); }
getOptionRole(key: string, defaultArg?: boolean): Role | undefined { if (this.type === "classic") { const id = defaultArg ? this.args.join(" ").trim() : this.options?.[key]; return this.guild?.roles.get(id as string); } if (this.type === "application") { return this.interaction?.data.options.getRole(key); } throw Error("Unknown command type"); }
// Note: the key is unused in a classic command context. getOptionAttachment(key: string): Attachment | undefined { if (this.type === "classic") { return this.message?.attachments.first(); } if (this.type === "application") { return this.interaction?.data.options.getAttachment(key); } throw Error("Unknown command type"); }
clean(text: string) { if (this.message) { return cleanMessage(this.message, text); } if (this.interaction) { return cleanInteraction(this.interaction, text); } throw Error("Unknown command type"); }
static init() { return this; }
static description = "No description found"; static aliases: string[] = []; static flags: CommandFlag[] = []; static ephemeral = false; static dbRequired = false; static slashAllowed = true; static directAllowed = true; static userAllowed = true; static adminOnly = false;}
export default Command;