Add ability for server exclusive commands

This commit is contained in:
Ethan Lane 2022-03-31 18:32:32 +01:00
parent 6a00c49ef3
commit 759ca653a9
Signed by: Vylpes
GPG key ID: EED233CC06D12504
5 changed files with 34 additions and 126 deletions

View file

@ -57,10 +57,11 @@ export class CoreClient extends Client {
this._util.loadEvents(this, this._eventItems);
}
public RegisterCommand(name: string, command: Command) {
public RegisterCommand(name: string, command: Command, serverId?: string) {
const item: ICommandItem = {
Name: name,
Command: command,
ServerId: serverId,
};
this._commandItems.push(item);

View file

@ -1,18 +1,8 @@
import { Message } from "discord.js";
import { IBaseResponse } from "../contracts/IBaseResponse";
import ICommandItem from "../contracts/ICommandItem";
import SettingsHelper from "../helpers/SettingsHelper";
import { Util } from "./util";
export interface IEventResponse extends IBaseResponse {
context?: {
prefix: string;
name: string;
args: string[];
message: Message;
}
}
export class Events {
private _util: Util;
@ -22,58 +12,21 @@ export class Events {
// Emit when a message is sent
// Used to check for commands
public async onMessage(message: Message, commands: ICommandItem[]): Promise<IEventResponse> {
if (!message.guild) return {
valid: false,
message: "Message was not sent in a guild, ignoring.",
};
if (message.author.bot) return {
valid: false,
message: "Message was sent by a bot, ignoring.",
};
public async onMessage(message: Message, commands: ICommandItem[]) {
if (!message.guild) return;
if (message.author.bot) return;
const prefix = await SettingsHelper.GetSetting("bot.prefix", message.guild.id);
if (!prefix) {
return {
valid: false,
message: "Prefix not found",
};
}
if (!prefix) return;
if (message.content.substring(0, prefix.length).toLowerCase() == prefix.toLowerCase()) {
const args = message.content.substring(prefix.length).split(" ");
const name = args.shift();
if (!name) return {
valid: false,
message: "Command name was not found",
};
if (!name) return;
const res = await this._util.loadCommand(name, args, message, commands);
if (!res.valid) {
return {
valid: false,
message: res.message,
};
}
return {
valid: true,
context: {
prefix: prefix,
name: name,
args: args,
message: message,
},
};
}
return {
valid: false,
message: "Message was not a command, ignoring.",
await this._util.loadCommand(name, args, message, commands);
}
}

View file

@ -1,9 +1,5 @@
// Required Components
import { Client, Message } from "discord.js";
import { readdirSync, existsSync } from "fs";
import { IBaseResponse } from "../contracts/IBaseResponse";
import { Command } from "../type/command";
import { Event } from "../type/event";
import { ICommandContext } from "../contracts/ICommandContext";
import ICommandItem from "../contracts/ICommandItem";
import IEventItem from "../contracts/IEventItem";
@ -12,53 +8,37 @@ import StringTools from "../helpers/StringTools";
import { CommandResponse } from "../constants/CommandResponse";
import ErrorMessages from "../constants/ErrorMessages";
export interface IUtilResponse extends IBaseResponse {
context?: {
name: string;
args: string[];
message: Message;
}
}
// Util Class
export class Util {
public async loadCommand(name: string, args: string[], message: Message, commands: ICommandItem[]): Promise<IUtilResponse> {
if (!message.member) return {
valid: false,
message: "Member is not part of message",
};
if (!message.guild) return {
valid: false,
message: "Message is not part of a guild",
};
public async loadCommand(name: string, args: string[], message: Message, commands: ICommandItem[]) {
if (!message.member) return;
if (!message.guild) return;
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", message.guild?.id);
const disabledCommands = disabledCommandsString?.split(",");
if (disabledCommands?.find(x => x == name)) {
message.reply(process.env.COMMANDS_DISABLED_MESSAGE || "This command is disabled.");
return {
valid: false,
message: "Command is disabled",
};
return;
}
const folder = process.env.FOLDERS_COMMANDS;
const item = commands.find(x => x.Name == name && !x.ServerId);
const itemForServer = commands.find(x => x.Name == name && x.ServerId == message.guild?.id);
const item = commands.find(x => x.Name == name);
let itemToUse: ICommandItem;
if (!itemForServer) {
if (!item) {
message.reply('Command not found');
return {
valid: false,
message: "Command not found"
};
return;
}
const requiredRoles = item.Command._roles;
itemToUse = item;
} else {
itemToUse = itemForServer;
}
const requiredRoles = itemToUse.Command._roles;
for (const i in requiredRoles) {
if (message.guild) {
@ -66,20 +46,12 @@ export class Util {
if (!setting) {
message.reply("Unable to verify if you have this role, please contact your bot administrator");
return {
valid: false,
message: "Unable to verify if you have this role, please contact your bot administrator"
};
return;
}
if (!message.member.roles.cache.find(role => role.name == setting)) {
message.reply(`You require the \`${StringTools.Capitalise(setting)}\` role to run this command`);
return {
valid: false,
message: `You require the \`${StringTools.Capitalise(setting)}\` role to run this command`
};
return;
}
}
}
@ -90,39 +62,24 @@ export class Util {
message: message
};
const precheckResponse = item.Command.precheck(context);
const precheckAsyncResponse = await item.Command.precheckAsync(context);
const precheckResponse = itemToUse.Command.precheck(context);
const precheckAsyncResponse = await itemToUse.Command.precheckAsync(context);
if (precheckResponse != CommandResponse.Ok) {
message.reply(ErrorMessages.GetErrorMessage(precheckResponse));
return {
valid: false,
message: ErrorMessages.GetErrorMessage(precheckResponse)
};
return;
}
if (precheckAsyncResponse != CommandResponse.Ok) {
message.reply(ErrorMessages.GetErrorMessage(precheckAsyncResponse));
return {
valid: false,
message: ErrorMessages.GetErrorMessage(precheckAsyncResponse)
};
return;
}
item.Command.execute(context);
return {
valid: true,
context: context
}
itemToUse.Command.execute(context);
}
// Load the events
loadEvents(client: Client, events: IEventItem[]): IUtilResponse {
const folder = process.env.FOLDERS_EVENTS;
loadEvents(client: Client, events: IEventItem[]) {
events.forEach((e) => {
client.on('channelCreate', e.Event.channelCreate);
client.on('channelDelete', e.Event.channelDelete);
@ -138,9 +95,5 @@ export class Util {
client.on('messageUpdate', e.Event.messageUpdate);
client.on('ready', e.Event.ready);
});
return {
valid: true
}
}
}

View file

@ -3,4 +3,5 @@ import { Command } from "../type/command";
export default interface ICommandItem {
Name: string,
Command: Command,
ServerId?: string,
}

View file

@ -35,7 +35,7 @@ export default class Registry {
client.RegisterCommand("setup", new Setup());
client.RegisterCommand("config", new Config());
client.RegisterCommand("code", new Code());
client.RegisterCommand("disable", new Disable())
client.RegisterCommand("disable", new Disable());
}
public static RegisterEvents(client: CoreClient) {