Update commands to new system
This commit is contained in:
parent
8ca1800da5
commit
3b9231644a
8 changed files with 137 additions and 87 deletions
37
src/Register.ts
Normal file
37
src/Register.ts
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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,14 +37,18 @@ 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";
|
return {
|
||||||
|
valid: false,
|
||||||
|
message: "Command not found"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const requiredRoles = item.Command._roles;
|
||||||
|
|
||||||
for (const i in requiredRoles) {
|
for (const i in requiredRoles) {
|
||||||
if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) {
|
if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) {
|
||||||
|
@ -51,7 +56,7 @@ export class Util {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: `You require the \`${requiredRoles[i]}\` role to run this command`,
|
message: `You require the \`${requiredRoles[i]}\` role to run this command`
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,74 +64,39 @@ export class Util {
|
||||||
const context: ICommandContext = {
|
const context: ICommandContext = {
|
||||||
name: name,
|
name: name,
|
||||||
args: args,
|
args: args,
|
||||||
message: message,
|
message: message
|
||||||
}
|
};
|
||||||
|
|
||||||
// Run the command and pass the command context with it
|
item.Command.execute(context);
|
||||||
command.execute(context);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: true,
|
valid: true,
|
||||||
context: {
|
context: context
|
||||||
name: name,
|
|
||||||
args: args,
|
|
||||||
message: message,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: "File does not exist",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: "Command folder does not exist",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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}/`)) {
|
events.forEach((e) => {
|
||||||
const eventFiles = readdirSync(`${process.cwd()}/${folder}/`);
|
client.on('channelCreate', e.Event.channelCreate);
|
||||||
|
client.on('channelDelete', e.Event.channelDelete);
|
||||||
for (let i = 0; i < eventFiles.length; i++) {
|
client.on('channelUpdate', e.Event.channelUpdate);
|
||||||
if (eventFiles[i].includes('.ts')) {
|
client.on('guildBanAdd', e.Event.guildBanAdd);
|
||||||
const eventName = eventFiles[i].split('.')[0];
|
client.on('guildBanRemove', e.Event.guildBanRemove);
|
||||||
|
client.on('guildCreate', e.Event.guildCreate);
|
||||||
const file = require(`${process.cwd()}/${folder}/${eventName}.ts`);
|
client.on('guildMemberAdd', e.Event.guildMemberAdd);
|
||||||
|
client.on('guildMemberRemove', e.Event.guildMemberRemove);
|
||||||
const event = new file[eventName]() as Event;
|
client.on('guildMemberUpdate', e.Event.guildMemberUpdate);
|
||||||
|
client.on('message', e.Event.message);
|
||||||
// Load events
|
client.on('messageDelete', e.Event.messageDelete);
|
||||||
client.on('channelCreate', event.channelCreate);
|
client.on('messageUpdate', e.Event.messageUpdate);
|
||||||
client.on('channelDelete', event.channelDelete);
|
client.on('ready', e.Event.ready);
|
||||||
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 {
|
return {
|
||||||
valid: true,
|
valid: true
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: "Event folder does not exist",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
|
6
src/contracts/ICommandItem.ts
Normal file
6
src/contracts/ICommandItem.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { Command } from "../type/command";
|
||||||
|
|
||||||
|
export default interface ICommandItem {
|
||||||
|
Name: string,
|
||||||
|
Command: Command,
|
||||||
|
}
|
6
src/contracts/IEventItem.ts
Normal file
6
src/contracts/IEventItem.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
import { Event } from "../type/event";
|
||||||
|
|
||||||
|
export default interface IEventItem {
|
||||||
|
Event: Event,
|
||||||
|
}
|
|
@ -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();
|
Loading…
Reference in a new issue