diff --git a/package.json b/package.json index bdd18e7a..8946c2ac 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,9 @@ "main": "dist/sharder.js", "scripts": { "start": "node dist/sharder.js", - "build": "tsc", - "startNoBuild": "node dist/sharder.js", "register": "node dist/deploy.js", - "watch": "node dist/watch.js" + "build": "tsc", + "watch": "tsc -w" }, "repository": { "type": "git", diff --git a/src/bot.ts b/src/bot.ts index 26aad234..83b07fec 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -22,11 +22,7 @@ for(const file of readdirSync(join(__dirname, "events")).filter(file => file.end let event = require(`./events/${file}`); // make a new event if a class is being used try { event = new event(); } catch (error) {} - if(event.once){ - client.once(event.name, (...args) => event.run(...args)); - continue; - } - client.on(event.name, (...args) => event.run(...args)); + client.onCustom(file, event.name, (...args) => event.run(...args), event.once); } client.login(process.env.TOKEN); \ No newline at end of file diff --git a/src/client.ts b/src/client.ts index d37d4225..3e482004 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,5 +1,5 @@ import { VoiceConnection } from "@discordjs/voice"; -import { Client, ClientEvents, Collection } from "discord.js"; +import { Awaited, Client, ClientEvents, Collection } from "discord.js"; import { SlashCommand } from "./command"; interface GuildSettings { connection: VoiceConnection | null; @@ -23,6 +23,25 @@ class CustomClient extends Client { * The settings of the client per guild */ public readonly settings: Collection = new Collection(); + + private readonly events: Collection any + }> = new Collection(); + + public onCustom(file: string, event: K, listener: (...args: ClientEvents[K]) => Awaited, once: boolean = false): this { + this.events.set(file, { + name: event, + listener + }); + return once ? this.once(event, listener) : this.on(event, listener); + } + + public offCustom(file: string): this { + const oldEvent = this.events.get(file); + this.events.delete(file); + return this.off(oldEvent.name, oldEvent.listener); + } } /** diff --git a/src/events/ready.ts b/src/events/ready.ts index b46140d3..4e0ae282 100644 --- a/src/events/ready.ts +++ b/src/events/ready.ts @@ -1,4 +1,5 @@ import { ActivityOptions } from "discord.js"; +import { watch } from "fs"; import { ClientEvent, CustomClient } from "../client"; import { CustomConsole } from "../console"; @@ -8,6 +9,24 @@ module.exports = class extends ClientEvent { } async run(client: CustomClient){ + watch("dist/commands", "utf8", (eventType, file) => { + if(eventType != "change") return; + const path = `../commands/${file}`; + delete require.cache[require.resolve(path)]; + let command = require(path); + try { command = new command(); } catch (error) {} + client.commands.set(command.data.name, command); + }); + watch("dist/events", "utf8", (eventType, file) => { + if(eventType != "change") return; + client.offCustom(file); + const path = `../events/${file}`; + delete require.cache[require.resolve(path)]; + let event = require(path); + try { event = new event(); } catch (error) {} + client.onCustom(file, event.name, (...args) => event.run(...args)); + }); + try { CustomConsole.log("Setting default permission for guild (/) commands."); diff --git a/src/watch.ts b/src/watch.ts deleted file mode 100644 index fd2d5a18..00000000 --- a/src/watch.ts +++ /dev/null @@ -1,27 +0,0 @@ -import Collection from "@discordjs/collection"; -import { exec } from "child_process"; -import { existsSync, readFileSync, unlinkSync, watch } from "fs"; - -const cooldowns: Collection = new Collection(); - -const watchChanges = (dir?: string) => { - watch(`src${dir ? `/${dir}` : ""}`, {encoding: "utf8"}, async (event, fileName) => { - let path = `src${dir ? `/${dir}` : ""}`; - if(!(fileName.endsWith(".ts") || fileName.endsWith(".js"))) return; - path += `/${fileName}`; - if(cooldowns.get(path)) return; - cooldowns.set(path, true); - setTimeout(() => cooldowns.set(path, false), 5000); - if(!existsSync(path) || !readFileSync(path, "utf8")){ - unlinkSync(`dist/${dir ? `${dir}/` : ""}${fileName.substring(0, fileName.length - 2)}js`); - return; - } - if(event == "change") exec("npm run build"); - }); -} - -watchChanges(); -watchChanges("commands"); -watchChanges("events"); - -console.log("now watching all files in src"); \ No newline at end of file