Skip to content

Commit

Permalink
Feat: Auto-Reload Code (#19)
Browse files Browse the repository at this point in the history
* Merge master into auto-reload-code (#18)

* Feat: Add join command (#15)

* Feat(Commands): Added join command

* Fix(Client): Add missing client.settings

* Feat: Removed startNoBuild from scripts (#16)

* Fix(Invite): Added applications.commands scope (#17)

* Feat: remove watch.ts

* Feat: Added custom command/event reloading
  • Loading branch information
JaronZ committed Aug 29, 2021
1 parent a87243c commit 9323009
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 1 addition & 5 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
21 changes: 20 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,6 +23,25 @@ class CustomClient<Ready extends boolean = boolean> extends Client<Ready> {
* The settings of the client per guild
*/
public readonly settings: Collection<string, GuildSettings> = new Collection();

private readonly events: Collection<string, {
name: string,
listener: (...args: any[]) => any
}> = new Collection();

public onCustom<K extends keyof ClientEvents>(file: string, event: K, listener: (...args: ClientEvents[K]) => Awaited<void>, 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);
}
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ActivityOptions } from "discord.js";
import { watch } from "fs";
import { ClientEvent, CustomClient } from "../client";
import { CustomConsole } from "../console";

Expand All @@ -8,6 +9,24 @@ module.exports = class extends ClientEvent {
}

async run(client: CustomClient<true>){
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.");

Expand Down
27 changes: 0 additions & 27 deletions src/watch.ts

This file was deleted.

0 comments on commit 9323009

Please sign in to comment.