Skip to content

Instantly share code, notes, and snippets.

@5factor
Created April 27, 2021 01:19
Show Gist options
  • Save 5factor/53212d43bab7fd2827b144e7ddb2464d to your computer and use it in GitHub Desktop.
Save 5factor/53212d43bab7fd2827b144e7ddb2464d to your computer and use it in GitHub Desktop.
import { Client, Collection } from "discord.js";
import { walk } from "../functions/fswalk";
const ignoreFiles = file => {
const [str] = file.match(/[^/\\]*?.js/) ?? [];
return /^[^_].*?\.js$/.test(str);
};
const commands = walk("./commands/", ignoreFiles);
const events = walk("./events/", ignoreFiles);
export default class ToastClient extends Client {
commands: Collection<any, any>;
config: any;
constructor(options?: any) {
super(options || {});
this.commands = new Collection();
this.config = require("../../config");
}
async connect() {
await super.login(process.env.CLIENT_TOKEN);
await this._loadEvents();
await this._loadCommands();
return this;
}
private async _loadCommands() {
for await (const file of commands) {
const Command = require("./" + file);
const command = new Command(this);
const split = file.split(/[\/\\]/g);
command.setCategory(split[split.length - 2]);
if (this.commands.has(command.help.name)) {
throw new Error("Duplicate command name " + command.help.name);
}
this.commands.set(command.help.name, command);
}
return this;
}
private async _loadEvents() {
for await (const file of events) {
const Event = require("./" + file);
const event = new Event(this);
if (event.conf.once) {
this.once(event.conf.name, (...args) => event.run(...args));
} else {
this.on(event.conf.name, (...args) => event.run(...args));
}
}
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment