Initialize apps/bot and other cleanup
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { Client, Events, GatewayIntentBits } from 'discord.js';
|
||||
import { CommandService } from './services/cmd/service';
|
||||
|
||||
const client = new Client({ 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) => {
|
||||
// Initialize
|
||||
await s.Commands.init();
|
||||
|
||||
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
|
||||
});
|
||||
|
||||
client.on("guildMemberAdd", async (guildMember) => {
|
||||
});
|
||||
|
||||
client.on("messageCreate", async (message) => {
|
||||
if (message.author.bot) return;
|
||||
|
||||
const command = s.Commands.parse(message);
|
||||
if (command) command.execute(message);
|
||||
});
|
||||
|
||||
client.login(Bun.env.BOT_TOKEN);
|
||||
@@ -0,0 +1,10 @@
|
||||
import type { Message } from "discord.js";
|
||||
import type { Command } from "../service";
|
||||
|
||||
export default {
|
||||
name: "stats",
|
||||
description: "View your fitness statistics!",
|
||||
execute: async (message: Message) => {
|
||||
console.log("Stats executed");
|
||||
}
|
||||
} as Command;
|
||||
@@ -0,0 +1,33 @@
|
||||
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 ?? ""];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user