Update commands to new system

This commit is contained in:
Ethan Lane 2021-12-21 18:37:11 +00:00
parent 8ca1800da5
commit 3b9231644a
Signed by: Vylpes
GPG key ID: EED233CC06D12504
8 changed files with 137 additions and 87 deletions

37
src/Register.ts Normal file
View file

@ -0,0 +1,37 @@
import { CoreClient } from "./client/client";
import About from "./commands/about";
import Ban from "./commands/ban";
import Clear from "./commands/clear";
import Evaluate from "./commands/eval";
import Help from "./commands/help";
import Kick from "./commands/kick";
import Mute from "./commands/mute";
import Poll from "./commands/poll";
import Role from "./commands/role";
import Rules from "./commands/rules";
import Unmute from "./commands/unmute";
import Warn from "./commands/warn";
import MemberEvents from "./events/MemberEvents";
import MessageEvents from "./events/MessageEvents";
export default class Register {
public static RegisterCommands(client: CoreClient) {
client.RegisterCommand("about", new About());
client.RegisterCommand("ban", new Ban());
client.RegisterCommand("clear", new Clear());
client.RegisterCommand("eval", new Evaluate());
client.RegisterCommand("help", new Help());
client.RegisterCommand("kick", new Kick());
client.RegisterCommand("mute", new Mute());
client.RegisterCommand("poll", new Poll());
client.RegisterCommand("role", new Role());
client.RegisterCommand("rules", new Rules());
client.RegisterCommand("unmute", new Unmute());
client.RegisterCommand("warn", new Warn());
}
public static RegisterEvents(client: CoreClient) {
client.RegisterEvent(new MemberEvents());
client.RegisterEvent(new MessageEvents());
}
}

View file

@ -1,10 +1,17 @@
import { Client } from "discord.js"; import { Client } from "discord.js";
import * as dotenv from "dotenv"; import * as dotenv from "dotenv";
import ICommandItem from "../contracts/ICommandItem";
import IEventItem from "../contracts/IEventItem";
import { Command } from "../type/command";
import { Event } from "../type/event";
import { Events } from "./events"; import { Events } from "./events";
import { Util } from "./util"; import { Util } from "./util";
export class CoreClient extends Client { export class CoreClient extends Client {
private _commandItems: ICommandItem[];
private _eventItems: IEventItem[];
private _events: Events; private _events: Events;
private _util: Util; private _util: Util;
@ -12,6 +19,9 @@ export class CoreClient extends Client {
super(); super();
dotenv.config(); dotenv.config();
this._commandItems = [];
this._eventItems = [];
this._events = new Events(); this._events = new Events();
this._util = new Util(); this._util = new Util();
} }
@ -22,11 +32,26 @@ export class CoreClient extends Client {
if (!process.env.FOLDERS_COMMANDS) throw "FOLDERS_COMMANDS is not defined in .env"; if (!process.env.FOLDERS_COMMANDS) throw "FOLDERS_COMMANDS is not defined in .env";
if (!process.env.FOLDERS_EVENTS) throw "FOLDERS_EVENTS is not defined in .env"; if (!process.env.FOLDERS_EVENTS) throw "FOLDERS_EVENTS is not defined in .env";
super.on("message", this._events.onMessage); super.on("message", (message) => this._events.onMessage(message, this._commandItems));
super.on("ready", this._events.onReady); super.on("ready", this._events.onReady);
super.login(process.env.BOT_TOKEN); super.login(process.env.BOT_TOKEN);
this._util.loadEvents(this); this._util.loadEvents(this, this._eventItems);
}
public RegisterCommand(name: string, command: Command) {
const item: ICommandItem = {
Name: name,
Command: command,
};
this._commandItems.push(item);
}
public RegisterEvent(event: Event) {
const item: IEventItem = {
Event: event,
};
} }
} }

View file

@ -1,5 +1,6 @@
import { Message } from "discord.js"; import { Message } from "discord.js";
import { IBaseResponse } from "../contracts/IBaseResponse"; import { IBaseResponse } from "../contracts/IBaseResponse";
import ICommandItem from "../contracts/ICommandItem";
import { Util } from "./util"; import { Util } from "./util";
export interface IEventResponse extends IBaseResponse { export interface IEventResponse extends IBaseResponse {
@ -20,7 +21,7 @@ export class Events {
// Emit when a message is sent // Emit when a message is sent
// Used to check for commands // Used to check for commands
public onMessage(message: Message): IEventResponse { public onMessage(message: Message, commands: ICommandItem[]): IEventResponse {
if (!message.guild) return { if (!message.guild) return {
valid: false, valid: false,
message: "Message was not sent in a guild, ignoring.", message: "Message was not sent in a guild, ignoring.",
@ -42,7 +43,7 @@ export class Events {
message: "Command name was not found", message: "Command name was not found",
}; };
const res = this._util.loadCommand(name, args, message); const res = this._util.loadCommand(name, args, message, commands);
if (!res.valid) { if (!res.valid) {
return { return {

View file

@ -5,6 +5,8 @@ import { IBaseResponse } from "../contracts/IBaseResponse";
import { Command } from "../type/command"; import { Command } from "../type/command";
import { Event } from "../type/event"; import { Event } from "../type/event";
import { ICommandContext } from "../contracts/ICommandContext"; import { ICommandContext } from "../contracts/ICommandContext";
import ICommandItem from "../contracts/ICommandItem";
import IEventItem from "../contracts/IEventItem";
export interface IUtilResponse extends IBaseResponse { export interface IUtilResponse extends IBaseResponse {
context?: { context?: {
@ -16,8 +18,7 @@ export interface IUtilResponse extends IBaseResponse {
// Util Class // Util Class
export class Util { export class Util {
public loadCommand(name: string, args: string[], message: Message, commands: ICommandItem[]): IUtilResponse {
public loadCommand(name: string, args: string[], message: Message): IUtilResponse {
if (!message.member) return { if (!message.member) return {
valid: false, valid: false,
message: "Member is not part of message", message: "Member is not part of message",
@ -36,97 +37,66 @@ export class Util {
const folder = process.env.FOLDERS_COMMANDS; const folder = process.env.FOLDERS_COMMANDS;
if (existsSync(`${process.cwd()}/${folder}/`)) { const item = commands.find(x => x.Name == name);
if (existsSync(`${process.cwd()}/${folder}/${name}.ts`)) {
const commandFile = require(`${process.cwd()}/${folder}/${name}.ts`).default;
const command = new commandFile() as Command;
const requiredRoles = command._roles; if (!item) {
message.reply('Command not found');
if (!command._category) command._category = "none";
for (const i in requiredRoles) {
if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) {
message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`);
return {
valid: false,
message: `You require the \`${requiredRoles[i]}\` role to run this command`,
};
}
}
const context: ICommandContext = {
name: name,
args: args,
message: message,
}
// Run the command and pass the command context with it
command.execute(context);
return {
valid: true,
context: {
name: name,
args: args,
message: message,
}
}
} else {
return {
valid: false,
message: "File does not exist",
}
}
} else {
return { return {
valid: false, valid: false,
message: "Command folder does not exist", message: "Command not found"
};
}
const requiredRoles = item.Command._roles;
for (const i in requiredRoles) {
if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) {
message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`);
return {
valid: false,
message: `You require the \`${requiredRoles[i]}\` role to run this command`
};
} }
} }
const context: ICommandContext = {
name: name,
args: args,
message: message
};
item.Command.execute(context);
return {
valid: true,
context: context
}
} }
// Load the events // Load the events
loadEvents(client: Client): IUtilResponse { loadEvents(client: Client, events: IEventItem[]): IUtilResponse {
const folder = process.env.FOLDERS_EVENTS; const folder = process.env.FOLDERS_EVENTS;
if (existsSync(`${process.cwd()}/${folder}/`)) {
const eventFiles = readdirSync(`${process.cwd()}/${folder}/`);
for (let i = 0; i < eventFiles.length; i++) {
if (eventFiles[i].includes('.ts')) {
const eventName = eventFiles[i].split('.')[0];
const file = require(`${process.cwd()}/${folder}/${eventName}.ts`);
const event = new file[eventName]() as Event;
// Load events
client.on('channelCreate', event.channelCreate);
client.on('channelDelete', event.channelDelete);
client.on('channelUpdate', event.channelUpdate);
client.on('guildBanAdd', event.guildBanAdd);
client.on('guildBanRemove', event.guildBanRemove);
client.on('guildCreate', event.guildCreate);
client.on('guildMemberAdd', event.guildMemberAdd);
client.on('guildMemberRemove', event.guildMemberRemove);
client.on('guildMemberUpdate', event.guildMemberUpdate);
client.on('message', event.message);
client.on('messageDelete', event.messageDelete);
client.on('messageUpdate', event.messageUpdate);
client.on('ready', event.ready);
}
}
return { events.forEach((e) => {
valid: true, client.on('channelCreate', e.Event.channelCreate);
} client.on('channelDelete', e.Event.channelDelete);
} else { client.on('channelUpdate', e.Event.channelUpdate);
return { client.on('guildBanAdd', e.Event.guildBanAdd);
valid: false, client.on('guildBanRemove', e.Event.guildBanRemove);
message: "Event folder does not exist", client.on('guildCreate', e.Event.guildCreate);
} client.on('guildMemberAdd', e.Event.guildMemberAdd);
client.on('guildMemberRemove', e.Event.guildMemberRemove);
client.on('guildMemberUpdate', e.Event.guildMemberUpdate);
client.on('message', e.Event.message);
client.on('messageDelete', e.Event.messageDelete);
client.on('messageUpdate', e.Event.messageUpdate);
client.on('ready', e.Event.ready);
});
return {
valid: true
} }
} }
} }

View file

@ -5,7 +5,7 @@ import PublicEmbed from "../helpers/embeds/PublicEmbed";
import { Command } from "../type/command"; import { Command } from "../type/command";
import { ICommandContext } from "../contracts/ICommandContext"; import { ICommandContext } from "../contracts/ICommandContext";
export default class Bane extends Command { export default class Ban extends Command {
constructor() { constructor() {
super(); super();

View file

@ -0,0 +1,6 @@
import { Command } from "../type/command";
export default interface ICommandItem {
Name: string,
Command: Command,
}

View file

@ -0,0 +1,6 @@
import { Event } from "../type/event";
export default interface IEventItem {
Event: Event,
}

View file

@ -1,5 +1,6 @@
import { CoreClient } from "./client/client"; import { CoreClient } from "./client/client";
import * as dotenv from "dotenv"; import * as dotenv from "dotenv";
import Register from "./Register";
dotenv.config(); dotenv.config();
@ -22,4 +23,8 @@ requiredConfigs.forEach(config => {
}); });
const client = new CoreClient(); const client = new CoreClient();
Register.RegisterCommands(client);
Register.RegisterEvents(client);
client.start(); client.start();