From c889e8be8ac8ad340670cc1c6dcc97de1d48e337 Mon Sep 17 00:00:00 2001 From: xHyroM Date: Thu, 24 Aug 2023 07:21:01 +0200 Subject: [PATCH] feat: message command context, message cmd handling --- src/commands/version.ts | 4 +- src/constants.ts | 4 ++ src/listeners/index.ts | 1 + src/listeners/message_create.ts | 32 +++++++++++++++ src/structs/context/CommandContext.ts | 59 ++++++++++++++++++++++++++- 5 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 src/listeners/message_create.ts diff --git a/src/commands/version.ts b/src/commands/version.ts index 84e0ee7..fdd1109 100644 --- a/src/commands/version.ts +++ b/src/commands/version.ts @@ -1,5 +1,5 @@ import { defineCommand } from "../loaders/commands.ts"; -import { COMMIT_HASH } from "../constants.ts"; +import { COMMIT_HASH, PRODUCTION } from "../constants.ts"; import { CommandContext } from "../structs/context/CommandContext.ts"; export default defineCommand({ @@ -8,7 +8,7 @@ export default defineCommand({ run: (context: CommandContext) => { context.interaction.reply({ content: [ - `[git-bun-discord-bot-${COMMIT_HASH}]()`, + `[git-bun-discord-bot-${COMMIT_HASH}]() ${!PRODUCTION ? "(dev)" : ""}`, `[v${Bun.version}](https://github.com/oven-sh/bun/releases/tag/bun-v${Bun.version})` ].join("\n"), ephemeral: true, diff --git a/src/constants.ts b/src/constants.ts index e9c8b60..6811a5d 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -3,3 +3,7 @@ import { spawnSync } from "bun"; export const COMMIT_HASH = spawnSync({ cmd: [ "git", "log", "--pretty=format:%h", "-n", "1" ] }).stdout.toString(); + +export const PRODUCTION = process.env.NODE_ENV === "production"; + +export const MESSAGE_PREFIX = PRODUCTION ? "b" : "<>"; diff --git a/src/listeners/index.ts b/src/listeners/index.ts index 70fde22..b3df2dd 100644 --- a/src/listeners/index.ts +++ b/src/listeners/index.ts @@ -1,2 +1,3 @@ import "./ready.ts"; import "./interaction_create.ts"; +import "./message_create.ts"; diff --git a/src/listeners/message_create.ts b/src/listeners/message_create.ts new file mode 100644 index 0000000..449637f --- /dev/null +++ b/src/listeners/message_create.ts @@ -0,0 +1,32 @@ +import { Events, Message } from "discord.js"; +import { defineListener } from "../loaders/listeners.ts"; +import { MESSAGE_PREFIX } from "../constants.ts"; +import { COMMANDS } from "../loaders/commands.ts"; +import { MessageCommandContext } from "../structs/context/CommandContext.ts"; + +defineListener({ + event: Events.MessageCreate, + run: async(message: Message) => { + if (!message.content.toLowerCase().startsWith(MESSAGE_PREFIX)) + return; + + const [commandName, ...args] = message.content + .slice(MESSAGE_PREFIX.length) + .trim() + .split(/ +/g); + + if (commandName.length === 0) return; + + const command = COMMANDS.get(commandName); + if (!command) return; + + const context = new MessageCommandContext(command, message, args); + + await Promise.resolve(command.run(context)) + .catch(async error => { + context.reply({ + content: `Something went wrong... ${error.message} (${error.code})` + }); + }); + } +}); diff --git a/src/structs/context/CommandContext.ts b/src/structs/context/CommandContext.ts index 221767c..c4b1c49 100644 --- a/src/structs/context/CommandContext.ts +++ b/src/structs/context/CommandContext.ts @@ -1,7 +1,18 @@ -import { ChatInputCommandInteraction } from "discord.js"; +import { ChatInputCommandInteraction, InteractionReplyOptions, Message, MessageCreateOptions, MessagePayload, InteractionResponse, Channel, User, GuildMember, APIInteractionGuildMember } from "discord.js"; import type { Command } from "../Command.ts"; -export class CommandContext { +export interface CommandContext { + command: Command; + isInteraction: T; + + user: User; + member: GuildMember | APIInteractionGuildMember; + channel: Channel; + + reply(options: string | MessagePayload | (T extends true ? InteractionReplyOptions : MessageCreateOptions)): (T extends true ? Promise : Promise); +} + +export class InteractionCommandContext implements CommandContext { public command: Command; public interaction: ChatInputCommandInteraction; @@ -10,6 +21,10 @@ export class CommandContext { this.interaction = interaction; } + get isInteraction(): true { + return true; + } + get user() { return this.interaction.user; } @@ -18,7 +33,47 @@ export class CommandContext { return this.interaction.member; } + get channel() { + return this.interaction.channel; + } + get resolved() { return this.interaction.options.resolved; } + + public reply(options: string | MessagePayload | InteractionReplyOptions): Promise { + return this.interaction.reply(options); + } +} + +export class MessageCommandContext implements CommandContext { + public command: Command; + public message: Message; + + public constructor(command: Command, message: Message, args: string[]) { + this.command = command; + this.message = message; + + // change args structure to application commands like + } + + get isInteraction(): false { + return false; + } + + get user() { + return this.message.author; + } + + get member() { + return this.message.member; + } + + get channel() { + return this.message.channel; + } + + public reply(options: string | MessagePayload | MessageCreateOptions): Promise> { + return this.channel.send(options); + } }