110 commandshelp about command errors which causes command to not run (#126)

* Change onMessage to onMessageCreate

* Fix help command
This commit is contained in:
Vylpes 2022-04-15 16:12:46 +01:00 committed by GitHub
parent 39c06fbc8e
commit 18e3f3d800
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 102 additions and 202 deletions

View file

@ -11,17 +11,17 @@ 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 static _commandItems: ICommandItem[];
private _eventItems: IEventItem[]; private static _eventItems: IEventItem[];
private _events: Events; private _events: Events;
private _util: Util; private _util: Util;
public get commandItems(): ICommandItem[] { public static get commandItems(): ICommandItem[] {
return this._commandItems; return this._commandItems;
} }
public get eventItems(): IEventItem[] { public static get eventItems(): IEventItem[] {
return this._eventItems; return this._eventItems;
} }
@ -31,8 +31,8 @@ export class CoreClient extends Client {
DefaultValues.useDevPrefix = devmode; DefaultValues.useDevPrefix = devmode;
this._commandItems = []; CoreClient._commandItems = [];
this._eventItems = []; CoreClient._eventItems = [];
this._events = new Events(); this._events = new Events();
this._util = new Util(); this._util = new Util();
@ -49,31 +49,31 @@ export class CoreClient extends Client {
return; return;
}); });
super.on("message", (message) => { super.on("messageCreate", (message) => {
this._events.onMessage(message, this._commandItems) this._events.onMessageCreate(message, CoreClient._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._eventItems); this._util.loadEvents(this, CoreClient._eventItems);
} }
public RegisterCommand(name: string, command: Command, serverId?: string) { public static RegisterCommand(name: string, command: Command, serverId?: string) {
const item: ICommandItem = { const item: ICommandItem = {
Name: name, Name: name,
Command: command, Command: command,
ServerId: serverId, ServerId: serverId,
}; };
this._commandItems.push(item); CoreClient._commandItems.push(item);
} }
public RegisterEvent(event: Event) { public static RegisterEvent(event: Event) {
const item: IEventItem = { const item: IEventItem = {
Event: event, Event: event,
}; };
this._eventItems.push(item); CoreClient._eventItems.push(item);
} }
} }

View file

@ -12,7 +12,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 async onMessage(message: Message, commands: ICommandItem[]) { public async onMessageCreate(message: Message, commands: ICommandItem[]) {
if (!message.guild) return; if (!message.guild) return;
if (message.author.bot) return; if (message.author.bot) return;

View file

@ -38,7 +38,7 @@ export class Util {
itemToUse = itemForServer; itemToUse = itemForServer;
} }
const requiredRoles = itemToUse.Command._roles; const requiredRoles = itemToUse.Command.Roles;
for (const i in requiredRoles) { for (const i in requiredRoles) {
if (message.guild) { if (message.guild) {
@ -90,7 +90,7 @@ export class Util {
client.on('guildMemberAdd', e.Event.guildMemberAdd); client.on('guildMemberAdd', e.Event.guildMemberAdd);
client.on('guildMemberRemove', e.Event.guildMemberRemove); client.on('guildMemberRemove', e.Event.guildMemberRemove);
client.on('guildMemberUpdate', e.Event.guildMemberUpdate); client.on('guildMemberUpdate', e.Event.guildMemberUpdate);
client.on('message', e.Event.message); client.on('messageCreate', e.Event.messageCreate);
client.on('messageDelete', e.Event.messageDelete); client.on('messageDelete', e.Event.messageDelete);
client.on('messageUpdate', e.Event.messageUpdate); client.on('messageUpdate', e.Event.messageUpdate);
client.on('ready', e.Event.ready); client.on('ready', e.Event.ready);

View file

@ -7,8 +7,8 @@ export default class Entry extends Command {
constructor() { constructor() {
super(); super();
super._category = "Moderation"; super.Category = "Moderation";
super._roles = [ super.Roles = [
"moderator" "moderator"
]; ];
} }

View file

@ -12,7 +12,7 @@ export default class Lobby extends Command {
constructor() { constructor() {
super(); super();
super._category = "General"; super.Category = "General";
} }
public override async execute(context: ICommandContext) { public override async execute(context: ICommandContext) {

View file

@ -6,7 +6,7 @@ import { Command } from "../type/command";
export default class About extends Command { export default class About extends Command {
constructor() { constructor() {
super(); super();
super._category = "General"; super.Category = "General";
} }
public override execute(context: ICommandContext): ICommandReturnContext { public override execute(context: ICommandContext): ICommandReturnContext {

View file

@ -10,8 +10,8 @@ export default class Ban extends Command {
constructor() { constructor() {
super(); super();
super._category = "Moderation"; super.Category = "Moderation";
super._roles = [ super.Roles = [
"moderator" "moderator"
]; ];
} }

View file

@ -9,8 +9,8 @@ export default class Clear extends Command {
constructor() { constructor() {
super(); super();
super._category = "Moderation"; super.Category = "Moderation";
super._roles = [ super.Roles = [
"moderator" "moderator"
]; ];
} }

View file

@ -10,8 +10,8 @@ export default class Code extends Command {
constructor() { constructor() {
super(); super();
super._category = "Moderation"; super.Category = "Moderation";
super._roles = [ super.Roles = [
"moderator" "moderator"
]; ];
} }

View file

@ -13,8 +13,8 @@ import { Command } from "../type/command";
export default class Config extends Command { export default class Config extends Command {
constructor() { constructor() {
super(); super();
super._category = "Administration"; super.Category = "Administration";
super._roles = [ super.Roles = [
"administrator" "administrator"
] ]
} }

View file

@ -7,8 +7,8 @@ export default class Disable extends Command {
constructor() { constructor() {
super(); super();
super._category = "Moderation"; super.Category = "Moderation";
super._roles = [ super.Roles = [
"moderator" "moderator"
]; ];
} }

View file

@ -1,47 +0,0 @@
import { ICommandContext } from "../contracts/ICommandContext";
import ICommandReturnContext from "../contracts/ICommandReturnContext";
import ErrorEmbed from "../helpers/embeds/ErrorEmbed";
import PublicEmbed from "../helpers/embeds/PublicEmbed";
import { Command } from "../type/command";
export default class Evaluate extends Command {
constructor() {
super();
super._category = "Owner";
}
public override execute(context: ICommandContext): ICommandReturnContext {
if (context.message.author.id != process.env.BOT_OWNERID) {
return {
commandContext: context,
embeds: []
};
}
const stmt = context.args;
console.log(`Eval Statement: ${stmt.join(" ")}`);
try {
const result = eval(stmt.join(" "));
const embed = new PublicEmbed(context, "", result);
embed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [embed]
};
}
catch (err: any) {
const errorEmbed = new ErrorEmbed(context, err);
errorEmbed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [errorEmbed]
};
}
}
}

View file

@ -1,9 +1,9 @@
import { existsSync, readdirSync } from "fs"; import { existsSync, readdirSync } from "fs";
import { CoreClient } from "../client/client";
import { ICommandContext } from "../contracts/ICommandContext"; import { ICommandContext } from "../contracts/ICommandContext";
import ErrorEmbed from "../helpers/embeds/ErrorEmbed"; import ErrorEmbed from "../helpers/embeds/ErrorEmbed";
import PublicEmbed from "../helpers/embeds/PublicEmbed"; import PublicEmbed from "../helpers/embeds/PublicEmbed";
import StringTools from "../helpers/StringTools"; import StringTools from "../helpers/StringTools";
import ICommandReturnContext from "../contracts/ICommandReturnContext";
import { Command } from "../type/command"; import { Command } from "../type/command";
export interface ICommandData { export interface ICommandData {
@ -17,106 +17,46 @@ export default class Help extends Command {
constructor() { constructor() {
super(); super();
super._category = "General"; super.Category = "General";
} }
public override execute(context: ICommandContext): ICommandReturnContext { public override execute(context: ICommandContext) {
if (context.args.length == 0) { if (context.args.length == 0) {
return this.SendAll(context); this.SendAll(context);
} else { } else {
return this.SendSingle(context); this.SendSingle(context);
} }
} }
public SendAll(context: ICommandContext): ICommandReturnContext { public SendAll(context: ICommandContext) {
const allCommands = this.GetAllCommandData(); const allCommands = CoreClient.commandItems;
const cateogries = [...new Set(allCommands.map(x => x.Category!))];; const cateogries = [...new Set(allCommands.map(x => x.Command.Category))];;
const embed = new PublicEmbed(context, "Commands", ""); const embed = new PublicEmbed(context, "Commands", "");
cateogries.forEach(category => { cateogries.forEach(category => {
let filtered = allCommands.filter(x => x.Category == category); let filtered = allCommands.filter(x => x.Command.Category == category);
embed.addField(StringTools.Capitalise(category), filtered.flatMap(x => x.Name).join(", ")); embed.addField(StringTools.Capitalise(category || "Uncategorised"), StringTools.CapitaliseArray(filtered.flatMap(x => x.Name)).join(", "));
}); });
embed.SendToCurrentChannel(); embed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [ embed ]
};
} }
public SendSingle(context: ICommandContext): ICommandReturnContext { public SendSingle(context: ICommandContext) {
const command = this.GetCommandData(context.args[0]); const command = CoreClient.commandItems.find(x => x.Name == context.args[0]);
if (!command.Exists) { if (!command) {
const errorEmbed = new ErrorEmbed(context, "Command does not exist"); const errorEmbed = new ErrorEmbed(context, "Command does not exist");
errorEmbed.SendToCurrentChannel(); errorEmbed.SendToCurrentChannel();
return { return;
commandContext: context,
embeds: [ errorEmbed ]
};
} }
const embed = new PublicEmbed(context, StringTools.Capitalise(command.Name!), ""); const embed = new PublicEmbed(context, StringTools.Capitalise(command.Name), "");
embed.addField("Category", StringTools.Capitalise(command.Category!)); embed.addField("Category", StringTools.Capitalise(command.Command.Category || "Uncategorised"));
embed.addField("Required Roles", StringTools.Capitalise(command.Roles!.join(", ")) || "*none*"); embed.addField("Required Roles", StringTools.Capitalise(command.Command.Roles.join(", ")) || "Everyone");
embed.SendToCurrentChannel(); embed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [ embed ]
};
}
public GetAllCommandData(): ICommandData[] {
const result: ICommandData[] = [];
const folder = process.env.FOLDERS_COMMANDS!;
const contents = readdirSync(`${process.cwd()}/${folder}`);
contents.forEach(name => {
const file = require(`${process.cwd()}/${folder}/${name}`).default;
const command = new file() as Command;
const data: ICommandData = {
Exists: true,
Name: name.replace(".ts", ""),
Category: command._category || "none",
Roles: command._roles,
};
result.push(data);
});
return result;
}
public GetCommandData(name: string): ICommandData {
const folder = process.env.FOLDERS_COMMANDS!;
const path = `${process.cwd()}/${folder}/${name}.ts`;
if (!existsSync(path)) {
return {
Exists: false
};
}
const file = require(path).default;
const command = new file() as Command;
const data: ICommandData = {
Exists: true,
Name: name,
Category: command._category || "none",
Roles: command._roles
};
return data;
} }
} }

View file

@ -10,8 +10,8 @@ export default class Kick extends Command {
constructor() { constructor() {
super(); super();
super._category = "Moderation"; super.Category = "Moderation";
super._roles = [ super.Roles = [
"moderator" "moderator"
]; ];
} }

View file

@ -10,8 +10,8 @@ export default class Mute extends Command {
constructor() { constructor() {
super(); super();
super._category = "Moderation"; super.Category = "Moderation";
super._roles = [ super.Roles = [
"moderator" "moderator"
]; ];
} }

View file

@ -8,7 +8,7 @@ export default class Poll extends Command {
constructor() { constructor() {
super(); super();
super._category = "General"; super.Category = "General";
} }
public override async execute(context: ICommandContext): Promise<ICommandReturnContext> { public override async execute(context: ICommandContext): Promise<ICommandReturnContext> {

View file

@ -9,7 +9,7 @@ export default class Role extends Command {
constructor() { constructor() {
super(); super();
super._category = "General"; super.Category = "General";
} }
public override async execute(context: ICommandContext) { public override async execute(context: ICommandContext) {

View file

@ -16,8 +16,8 @@ export default class Rules extends Command {
constructor() { constructor() {
super(); super();
super._category = "Admin"; super.Category = "Admin";
super._roles = [ super.Roles = [
"administrator" "administrator"
]; ];
} }

View file

@ -7,8 +7,8 @@ import { Command } from "../type/command";
export default class Setup extends Command { export default class Setup extends Command {
constructor() { constructor() {
super(); super();
super._category = "Administration"; super.Category = "Administration";
super._roles = [ super.Roles = [
"moderator" "moderator"
] ]
} }

View file

@ -10,8 +10,8 @@ export default class Unmute extends Command {
constructor() { constructor() {
super(); super();
super._category = "Moderation"; super.Category = "Moderation";
super._roles = [ super.Roles = [
"moderator" "moderator"
]; ];
} }

View file

@ -9,8 +9,8 @@ export default class Warn extends Command {
constructor() { constructor() {
super(); super();
super._category = "Moderation"; super.Category = "Moderation";
super._roles = [ super.Roles = [
"moderator" "moderator"
]; ];
} }

View file

@ -71,7 +71,7 @@ export default class MessageEvents extends Event {
}; };
} }
public override async message(message: Message) { public override async messageCreate(message: Message) {
if (!message.guild) return; if (!message.guild) return;
if (message.author.bot) return; if (message.author.bot) return;

View file

@ -13,6 +13,16 @@ export default class StringTools {
return result.join(" "); return result.join(" ");
} }
public static CapitaliseArray(str: string[]): string[] {
const res: string[] = [];
str.forEach(s => {
res.push(StringTools.Capitalise(s));
});
return res;
}
public static RandomString(length: number) { public static RandomString(length: number) {
let result = ""; let result = "";

View file

@ -7,7 +7,6 @@ import Clear from "./commands/clear";
import Code from "./commands/code"; import Code from "./commands/code";
import Config from "./commands/config"; import Config from "./commands/config";
import Disable from "./commands/disable"; import Disable from "./commands/disable";
import Evaluate from "./commands/eval";
import Help from "./commands/help"; import Help from "./commands/help";
import Kick from "./commands/kick"; import Kick from "./commands/kick";
import Mute from "./commands/mute"; import Mute from "./commands/mute";
@ -27,35 +26,34 @@ import MemberEvents from "./events/MemberEvents";
import MessageEvents from "./events/MessageEvents"; import MessageEvents from "./events/MessageEvents";
export default class Registry { export default class Registry {
public static RegisterCommands(client: CoreClient) { public static RegisterCommands() {
client.RegisterCommand("about", new About()); CoreClient.RegisterCommand("about", new About());
client.RegisterCommand("ban", new Ban()); CoreClient.RegisterCommand("ban", new Ban());
client.RegisterCommand("clear", new Clear()); CoreClient.RegisterCommand("clear", new Clear());
client.RegisterCommand("eval", new Evaluate()); CoreClient.RegisterCommand("help", new Help());
client.RegisterCommand("help", new Help()); CoreClient.RegisterCommand("kick", new Kick());
client.RegisterCommand("kick", new Kick()); CoreClient.RegisterCommand("mute", new Mute());
client.RegisterCommand("mute", new Mute()); CoreClient.RegisterCommand("poll", new Poll());
client.RegisterCommand("poll", new Poll()); CoreClient.RegisterCommand("role", new Role());
client.RegisterCommand("role", new Role()); CoreClient.RegisterCommand("rules", new Rules());
client.RegisterCommand("rules", new Rules()); CoreClient.RegisterCommand("unmute", new Unmute());
client.RegisterCommand("unmute", new Unmute()); CoreClient.RegisterCommand("warn", new Warn());
client.RegisterCommand("warn", new Warn()); CoreClient.RegisterCommand("setup", new Setup());
client.RegisterCommand("setup", new Setup()); CoreClient.RegisterCommand("config", new Config());
client.RegisterCommand("config", new Config()); CoreClient.RegisterCommand("code", new Code());
client.RegisterCommand("code", new Code()); CoreClient.RegisterCommand("disable", new Disable());
client.RegisterCommand("disable", new Disable());
// Exclusive Commands: MankBot // Exclusive Commands: MankBot
client.RegisterCommand("lobby", new Lobby(), "501231711271780357"); CoreClient.RegisterCommand("lobby", new Lobby(), "501231711271780357");
client.RegisterCommand("entry", new Entry(), "501231711271780357"); CoreClient.RegisterCommand("entry", new Entry(), "501231711271780357");
// Add Exclusive Commands to Test Server // Add Exclusive Commands to Test Server
client.RegisterCommand("lobby", new Lobby(), "442730357897429002"); CoreClient.RegisterCommand("lobby", new Lobby(), "442730357897429002");
client.RegisterCommand("entry", new Entry(), "442730357897429002"); CoreClient.RegisterCommand("entry", new Entry(), "442730357897429002");
} }
public static RegisterEvents(client: CoreClient) { public static RegisterEvents() {
client.RegisterEvent(new MemberEvents()); CoreClient.RegisterEvent(new MemberEvents());
client.RegisterEvent(new MessageEvents()); CoreClient.RegisterEvent(new MessageEvents());
} }
} }

View file

@ -2,12 +2,11 @@ import { CommandResponse } from "../constants/CommandResponse";
import { ICommandContext } from "../contracts/ICommandContext"; import { ICommandContext } from "../contracts/ICommandContext";
export class Command { export class Command {
public _roles: string[]; public Roles: string[];
public Category?: string;
public _category?: string;
constructor() { constructor() {
this._roles = []; this.Roles = [];
} }
public precheck(context: ICommandContext): CommandResponse { public precheck(context: ICommandContext): CommandResponse {

View file

@ -37,7 +37,7 @@ export class Event {
} }
public message(message: Message) { public messageCreate(message: Message) {
} }

View file

@ -27,7 +27,7 @@ const client = new CoreClient([
Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_MEMBERS,
], devmode); ], devmode);
registry.RegisterCommands(client); registry.RegisterCommands();
registry.RegisterEvents(client); registry.RegisterEvents();
client.start(); client.start();