Restructure bot

This commit is contained in:
Dominic Ferrando
2026-09-26 21:06:06 -04:00
parent db206bafa3
commit 7f791babd4
5 changed files with 51 additions and 48 deletions
@@ -1,6 +1,6 @@
import { EmbedBuilder, type Message } from "discord.js"; import { EmbedBuilder, type Message } from "discord.js";
import type { Command } from "../service"; import type { Command } from "../../models";
import { api } from "../../../util"; import { api } from "../../util";
const ATTRIBUTE_EMOJI: Record<string, string> = { const ATTRIBUTE_EMOJI: Record<string, string> = {
Strength: "💪", Strength: "💪",
@@ -12,8 +12,11 @@ const ATTRIBUTE_EMOJI: Record<string, string> = {
export default { export default {
name: "stats", name: "stats",
description: "View your fitness statistics!", description: "View your fitness statistics!",
usage: ".stats",
execute: async (message: Message) => { execute: async (message: Message) => {
const res = await api.accounts({ id: `@${message.author.id}` }).stats.get(); const res = await api.accounts({ id: `@${message.author.id}` }).stats.get();
if (res.error) return;
if (!res.data) { if (!res.data) {
await message.reply("No stats found yet — submit an assessment first!"); await message.reply("No stats found yet — submit an assessment first!");
return; return;
@@ -38,4 +41,4 @@ export default {
await message.reply({ embeds: [embed] }); await message.reply({ embeds: [embed] });
} }
} as Command; } satisfies Command;
+10 -12
View File
@@ -1,20 +1,13 @@
import { Client, Events, GatewayIntentBits } from 'discord.js'; import { Client, Events, GatewayIntentBits } from 'discord.js';
import { CommandService } from './services/cmd/service'; import CommandService from './services/command';
const client = new Client({ const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
}); });
// SERVICES
// -----------------------
const s = (() => {
const Commands = new CommandService(client);
return { Commands };
})();
client.once(Events.ClientReady, async (readyClient) => { client.once(Events.ClientReady, async (readyClient) => {
// Initialize // Initialize services
await s.Commands.init(); await CommandService.init();
console.log(`Ready! Logged in as ${readyClient.user.tag}`); console.log(`Ready! Logged in as ${readyClient.user.tag}`);
}); });
@@ -25,8 +18,13 @@ client.on("guildMemberAdd", async (guildMember) => {
client.on("messageCreate", async (message) => { client.on("messageCreate", async (message) => {
if (message.author.bot) return; if (message.author.bot) return;
const command = s.Commands.parse(message); try {
if (command) command.execute(message); const command = CommandService.parse(message);
if (command) await command.execute(message);
}
catch (err) {
console.error(err);
}
}); });
client.login(Bun.env.BOT_TOKEN); client.login(Bun.env.BOT_TOKEN);
+10
View File
@@ -0,0 +1,10 @@
import type { Message } from "discord.js";
// COMMANDS
export interface Command {
name: string,
description: string,
usage: string,
execute: (message: Message) => void | Promise<void>
}
-33
View File
@@ -1,33 +0,0 @@
import type { Client, Message } from "discord.js";
import { readdir } from "fs/promises";
const CMD_PREFIX = ".";
export interface Command {
name: string,
description: string,
execute: (message: Message) => void | Promise<void>
}
export class CommandService {
private client: Client
private registry: Record<string, Command> = {}
constructor(client: Client) {
this.client = client;
}
async init() {
const commands = await Promise.all(
(await readdir(`${import.meta.dir}/registry`)).map(async file => (await import(`./registry/${file}`)).default as Command)
);
for (const command of commands)
this.registry[command.name] = command;
};
parse(message: Message): Command | undefined {
if (!message.content.startsWith(CMD_PREFIX)) return;
const name = message.content.slice(CMD_PREFIX.length).split(/\s+/)[0];
return this.registry[name ?? ""];
}
}
+25
View File
@@ -0,0 +1,25 @@
import type { Message } from "discord.js";
import { readdir } from "fs/promises";
import { dirname } from "path";
import type { Command } from "../models";
const CMD_PREFIX = ".";
const CMD_REGISTRY_DIR = `${dirname(Bun.main)}/cmd/registry`;
export default abstract class CommandService {
private static registry: Record<string, Command> = {}
static async init() {
const commands = await Promise.all(
(await readdir(CMD_REGISTRY_DIR)).map(async file => (await import(`${CMD_REGISTRY_DIR}/${file}`)).default satisfies Command)
);
for (const command of commands)
CommandService.registry[command.name] = command;
};
static parse(message: Message): Command | undefined {
if (!message.content.startsWith(CMD_PREFIX)) return;
const name = message.content.slice(CMD_PREFIX.length).split(/\s+/)[0];
return this.registry[name ?? ""];
}
}