A powerful and extendable Discord bot, with it's own module system :3 thevoid.cafe/projects/voidy

✨ Add FeatureHandler for larger systems

Jo 70a0b1f6 5ddb29f3

+52
+4
src/features/chatbot/feature.json
··· 1 + { 2 + "name": "chatbot", 3 + "version": "0.1.0" 4 + }
+31
src/handlers/FeatureHandler.ts
··· 1 + import { walk } from "https://deno.land/std@0.170.0/fs/walk.ts"; 2 + import { VoidyClient } from "../utils/classes/VoidyClient.ts"; 3 + import { Feature, FeatureOptions } from "../utils/classes/Feature.ts"; 4 + import { CommandHandler } from "./CommandHandler.ts"; 5 + import { EventHandler } from "./EventHandler.ts"; 6 + 7 + export class FeatureHandler { 8 + public static async loadAll(client: VoidyClient, paths: string[] = []) { 9 + for (const path of paths) { 10 + await this.handleDirectory(client, path); 11 + } 12 + 13 + console.log(); 14 + } 15 + 16 + protected static async handleDirectory(client: VoidyClient, path: string) { 17 + console.log(`[Voidy] Loading Features from: ${path}`); 18 + console.log(); 19 + 20 + for await (const walkEntry of walk(path)) { 21 + if (!walkEntry.isFile || walkEntry.name !== "feature.json") continue; 22 + 23 + const featureDirectory = walkEntry.path.split("/feature.json")[0]; 24 + const feature = (await import(`file://${Deno.cwd()}/${walkEntry.path}`, { with: { type: "json" } })).default as FeatureOptions; 25 + client.features.set(feature.name, new Feature(feature)); 26 + 27 + await CommandHandler.loadCommands(client, [`${featureDirectory}/commands`]); 28 + await EventHandler.loadEvents(client, [`${featureDirectory}/events`]); 29 + } 30 + } 31 + }
+14
src/utils/classes/Feature.ts
··· 1 + export class Feature { 2 + constructor(options: FeatureOptions) { 3 + this.name = options.name; 4 + this.version = options.version; 5 + } 6 + 7 + public name: string; 8 + public version: string; 9 + } 10 + 11 + export interface FeatureOptions { 12 + name: string, 13 + version: string, 14 + }
+3
src/utils/classes/VoidyClient.ts
··· 1 1 import { Client, ClientOptions, Collection } from "discord.js"; 2 2 import { Command } from "./Command.ts"; 3 + import { Feature } from "./Feature.ts"; 3 4 4 5 export class VoidyClient extends Client { 5 6 constructor(options: ClientOptions) { ··· 7 8 8 9 this.commands = new Collection<string, Command>(); 9 10 this.events = new Collection<string, string>(); 11 + this.features = new Collection<string, Feature>(); 10 12 } 11 13 12 14 public commands: Collection<string, Command>; 13 15 public events: Collection<string, string>; 16 + public features: Collection<string, Feature> 14 17 }