Add ability for server exclusive commands
This commit is contained in:
parent
6a00c49ef3
commit
759ca653a9
5 changed files with 34 additions and 126 deletions
|
@ -57,10 +57,11 @@ export class CoreClient extends Client {
|
||||||
this._util.loadEvents(this, this._eventItems);
|
this._util.loadEvents(this, this._eventItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RegisterCommand(name: string, command: Command) {
|
public RegisterCommand(name: string, command: Command, serverId?: string) {
|
||||||
const item: ICommandItem = {
|
const item: ICommandItem = {
|
||||||
Name: name,
|
Name: name,
|
||||||
Command: command,
|
Command: command,
|
||||||
|
ServerId: serverId,
|
||||||
};
|
};
|
||||||
|
|
||||||
this._commandItems.push(item);
|
this._commandItems.push(item);
|
||||||
|
|
|
@ -1,18 +1,8 @@
|
||||||
import { Message } from "discord.js";
|
import { Message } from "discord.js";
|
||||||
import { IBaseResponse } from "../contracts/IBaseResponse";
|
|
||||||
import ICommandItem from "../contracts/ICommandItem";
|
import ICommandItem from "../contracts/ICommandItem";
|
||||||
import SettingsHelper from "../helpers/SettingsHelper";
|
import SettingsHelper from "../helpers/SettingsHelper";
|
||||||
import { Util } from "./util";
|
import { Util } from "./util";
|
||||||
|
|
||||||
export interface IEventResponse extends IBaseResponse {
|
|
||||||
context?: {
|
|
||||||
prefix: string;
|
|
||||||
name: string;
|
|
||||||
args: string[];
|
|
||||||
message: Message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Events {
|
export class Events {
|
||||||
private _util: Util;
|
private _util: Util;
|
||||||
|
|
||||||
|
@ -22,58 +12,21 @@ 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[]): Promise<IEventResponse> {
|
public async onMessage(message: Message, commands: ICommandItem[]) {
|
||||||
if (!message.guild) return {
|
if (!message.guild) return;
|
||||||
valid: false,
|
if (message.author.bot) return;
|
||||||
message: "Message was not sent in a guild, ignoring.",
|
|
||||||
};
|
|
||||||
|
|
||||||
if (message.author.bot) return {
|
|
||||||
valid: false,
|
|
||||||
message: "Message was sent by a bot, ignoring.",
|
|
||||||
};
|
|
||||||
|
|
||||||
const prefix = await SettingsHelper.GetSetting("bot.prefix", message.guild.id);
|
const prefix = await SettingsHelper.GetSetting("bot.prefix", message.guild.id);
|
||||||
|
|
||||||
if (!prefix) {
|
if (!prefix) return;
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: "Prefix not found",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.content.substring(0, prefix.length).toLowerCase() == prefix.toLowerCase()) {
|
if (message.content.substring(0, prefix.length).toLowerCase() == prefix.toLowerCase()) {
|
||||||
const args = message.content.substring(prefix.length).split(" ");
|
const args = message.content.substring(prefix.length).split(" ");
|
||||||
const name = args.shift();
|
const name = args.shift();
|
||||||
|
|
||||||
if (!name) return {
|
if (!name) return;
|
||||||
valid: false,
|
|
||||||
message: "Command name was not found",
|
|
||||||
};
|
|
||||||
|
|
||||||
const res = await this._util.loadCommand(name, args, message, commands);
|
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.",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
// Required Components
|
// Required Components
|
||||||
import { Client, Message } from "discord.js";
|
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 { ICommandContext } from "../contracts/ICommandContext";
|
||||||
import ICommandItem from "../contracts/ICommandItem";
|
import ICommandItem from "../contracts/ICommandItem";
|
||||||
import IEventItem from "../contracts/IEventItem";
|
import IEventItem from "../contracts/IEventItem";
|
||||||
|
@ -12,53 +8,37 @@ import StringTools from "../helpers/StringTools";
|
||||||
import { CommandResponse } from "../constants/CommandResponse";
|
import { CommandResponse } from "../constants/CommandResponse";
|
||||||
import ErrorMessages from "../constants/ErrorMessages";
|
import ErrorMessages from "../constants/ErrorMessages";
|
||||||
|
|
||||||
export interface IUtilResponse extends IBaseResponse {
|
|
||||||
context?: {
|
|
||||||
name: string;
|
|
||||||
args: string[];
|
|
||||||
message: Message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Util Class
|
// Util Class
|
||||||
export class Util {
|
export class Util {
|
||||||
public async loadCommand(name: string, args: string[], message: Message, commands: ICommandItem[]): Promise<IUtilResponse> {
|
public async loadCommand(name: string, args: string[], message: Message, commands: ICommandItem[]) {
|
||||||
if (!message.member) return {
|
if (!message.member) return;
|
||||||
valid: false,
|
if (!message.guild) return;
|
||||||
message: "Member is not part of message",
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!message.guild) return {
|
|
||||||
valid: false,
|
|
||||||
message: "Message is not part of a guild",
|
|
||||||
};
|
|
||||||
|
|
||||||
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", message.guild?.id);
|
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", message.guild?.id);
|
||||||
const disabledCommands = disabledCommandsString?.split(",");
|
const disabledCommands = disabledCommandsString?.split(",");
|
||||||
|
|
||||||
if (disabledCommands?.find(x => x == name)) {
|
if (disabledCommands?.find(x => x == name)) {
|
||||||
message.reply(process.env.COMMANDS_DISABLED_MESSAGE || "This command is disabled.");
|
message.reply(process.env.COMMANDS_DISABLED_MESSAGE || "This command is disabled.");
|
||||||
|
return;
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: "Command is disabled",
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 (!item) {
|
if (!itemForServer) {
|
||||||
message.reply('Command not found');
|
if (!item) {
|
||||||
|
message.reply('Command not found');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
itemToUse = item;
|
||||||
valid: false,
|
} else {
|
||||||
message: "Command not found"
|
itemToUse = itemForServer;
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const requiredRoles = item.Command._roles;
|
const requiredRoles = itemToUse.Command._roles;
|
||||||
|
|
||||||
for (const i in requiredRoles) {
|
for (const i in requiredRoles) {
|
||||||
if (message.guild) {
|
if (message.guild) {
|
||||||
|
@ -66,20 +46,12 @@ export class Util {
|
||||||
|
|
||||||
if (!setting) {
|
if (!setting) {
|
||||||
message.reply("Unable to verify if you have this role, please contact your bot administrator");
|
message.reply("Unable to verify if you have this role, please contact your bot administrator");
|
||||||
|
return;
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: "Unable to verify if you have this role, please contact your bot administrator"
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!message.member.roles.cache.find(role => role.name == setting)) {
|
if (!message.member.roles.cache.find(role => role.name == setting)) {
|
||||||
message.reply(`You require the \`${StringTools.Capitalise(setting)}\` role to run this command`);
|
message.reply(`You require the \`${StringTools.Capitalise(setting)}\` role to run this command`);
|
||||||
|
return;
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `You require the \`${StringTools.Capitalise(setting)}\` role to run this command`
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,39 +62,24 @@ export class Util {
|
||||||
message: message
|
message: message
|
||||||
};
|
};
|
||||||
|
|
||||||
const precheckResponse = item.Command.precheck(context);
|
const precheckResponse = itemToUse.Command.precheck(context);
|
||||||
const precheckAsyncResponse = await item.Command.precheckAsync(context);
|
const precheckAsyncResponse = await itemToUse.Command.precheckAsync(context);
|
||||||
|
|
||||||
if (precheckResponse != CommandResponse.Ok) {
|
if (precheckResponse != CommandResponse.Ok) {
|
||||||
message.reply(ErrorMessages.GetErrorMessage(precheckResponse));
|
message.reply(ErrorMessages.GetErrorMessage(precheckResponse));
|
||||||
|
return;
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: ErrorMessages.GetErrorMessage(precheckResponse)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (precheckAsyncResponse != CommandResponse.Ok) {
|
if (precheckAsyncResponse != CommandResponse.Ok) {
|
||||||
message.reply(ErrorMessages.GetErrorMessage(precheckAsyncResponse));
|
message.reply(ErrorMessages.GetErrorMessage(precheckAsyncResponse));
|
||||||
|
return;
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: ErrorMessages.GetErrorMessage(precheckAsyncResponse)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
item.Command.execute(context);
|
itemToUse.Command.execute(context);
|
||||||
|
|
||||||
return {
|
|
||||||
valid: true,
|
|
||||||
context: context
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the events
|
// Load the events
|
||||||
loadEvents(client: Client, events: IEventItem[]): IUtilResponse {
|
loadEvents(client: Client, events: IEventItem[]) {
|
||||||
const folder = process.env.FOLDERS_EVENTS;
|
|
||||||
|
|
||||||
events.forEach((e) => {
|
events.forEach((e) => {
|
||||||
client.on('channelCreate', e.Event.channelCreate);
|
client.on('channelCreate', e.Event.channelCreate);
|
||||||
client.on('channelDelete', e.Event.channelDelete);
|
client.on('channelDelete', e.Event.channelDelete);
|
||||||
|
@ -138,9 +95,5 @@ export class Util {
|
||||||
client.on('messageUpdate', e.Event.messageUpdate);
|
client.on('messageUpdate', e.Event.messageUpdate);
|
||||||
client.on('ready', e.Event.ready);
|
client.on('ready', e.Event.ready);
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
|
||||||
valid: true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,4 +3,5 @@ import { Command } from "../type/command";
|
||||||
export default interface ICommandItem {
|
export default interface ICommandItem {
|
||||||
Name: string,
|
Name: string,
|
||||||
Command: Command,
|
Command: Command,
|
||||||
|
ServerId?: string,
|
||||||
}
|
}
|
|
@ -35,7 +35,7 @@ export default class Registry {
|
||||||
client.RegisterCommand("setup", new Setup());
|
client.RegisterCommand("setup", new Setup());
|
||||||
client.RegisterCommand("config", new Config());
|
client.RegisterCommand("config", new Config());
|
||||||
client.RegisterCommand("code", new Code());
|
client.RegisterCommand("code", new Code());
|
||||||
client.RegisterCommand("disable", new Disable())
|
client.RegisterCommand("disable", new Disable());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RegisterEvents(client: CoreClient) {
|
public static RegisterEvents(client: CoreClient) {
|
||||||
|
|
Loading…
Reference in a new issue