Disabled commands per server

This commit is contained in:
Ethan Lane 2022-03-21 18:38:03 +00:00
parent 4c7208c192
commit d5b3daaec5
8 changed files with 113 additions and 12 deletions

View file

@ -7,11 +7,7 @@
# any secret values. # any secret values.
BOT_TOKEN= BOT_TOKEN=
BOT_PREFIX=v!
BOT_VER=3.0 BOT_VER=3.0
BOT_AUTHOR=Vylpes BOT_AUTHOR=Vylpes
BOT_DATE=28 Nov 2021 BOT_DATE=28 Nov 2021
BOT_OWNERID=147392775707426816 BOT_OWNERID=147392775707426816
FOLDERS_COMMANDS=src/commands
FOLDERS_EVENTS=src/events

View file

@ -3,8 +3,7 @@ USAGE: <key> <set|reset> [value]
===[ KEYS ]=== ===[ KEYS ]===
bot.prefix: The bot prefix for the server (Default: "v!") bot.prefix: The bot prefix for the server (Default: "v!")
commands.disabled: Disabled commands (Default: "") commands.disabled: Disabled commands, separated by commas (Default: "")
commands.disabled.message: The message to show when a disabled command is ran (Default: "This command is disabled.")
role.assignable: List of roles assignable to user (Default: []) role.assignable: List of roles assignable to user (Default: [])
role.moderator: The moderator role name (Default: "Moderator") role.moderator: The moderator role name (Default: "Moderator")

View file

@ -6,7 +6,7 @@
"typings": "./dist", "typings": "./dist",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"start": "node ./dist/vylbot", "start": "tsc && node ./dist/vylbot",
"test": "jest" "test": "jest"
}, },
"repository": { "repository": {

View file

@ -37,9 +37,6 @@ export class CoreClient extends Client {
public async start() { public async start() {
if (!process.env.BOT_TOKEN) throw "BOT_TOKEN is not defined in .env"; if (!process.env.BOT_TOKEN) throw "BOT_TOKEN is not defined in .env";
if (!process.env.BOT_PREFIX) throw "BOT_PREFIX 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";
await createConnection().catch(e => { await createConnection().catch(e => {
throw e; throw e;

View file

@ -28,7 +28,13 @@ export class Util {
message: "Member is not part of message", message: "Member is not part of message",
}; };
const disabledCommands = process.env.COMMANDS_DISABLED?.split(','); 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 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.");

96
src/commands/disable.ts Normal file
View file

@ -0,0 +1,96 @@
import { CommandResponse } from "../constants/CommandResponse";
import { ICommandContext } from "../contracts/ICommandContext";
import ErrorEmbed from "../helpers/embeds/ErrorEmbed";
import PublicEmbed from "../helpers/embeds/PublicEmbed";
import SettingsHelper from "../helpers/SettingsHelper";
import StringTools from "../helpers/StringTools";
import { Command } from "../type/command";
export default class Disable extends Command {
constructor() {
super();
super._category = "Moderation";
super._roles = [
"moderator"
];
}
public override async execute(context: ICommandContext) {
const action = context.args[0];
switch (action) {
case "add":
await this.Add(context);
break;
case "remove":
await this.Remove(context);
break;
default:
await this.SendUsage(context);
}
}
private async SendUsage(context: ICommandContext) {
const description = [
"USAGE: <add|remove> <name>",
"",
"add: Adds the command name to the server's disabled command string",
"remove: Removes the command name from the server's disabled command string",
"name: The name of the command to enable/disable"
].join("\n");
const embed = new PublicEmbed(context, "", description);
embed.SendToCurrentChannel();
}
private async Add(context: ICommandContext) {
if (!context.message.guild) {
return;
}
const commandName = context.args[1];
if (!commandName) {
this.SendUsage(context);
return;
}
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", context.message.guild.id);
const disabledCommands = disabledCommandsString != "" ? disabledCommandsString?.split(",") : [];
disabledCommands?.push(commandName);
await SettingsHelper.SetSetting("commands.disabled", context.message.guild.id, disabledCommands!.join(","));
const embed = new PublicEmbed(context, "", `Disabled command: ${commandName}`);
embed.SendToCurrentChannel();
}
private async Remove(context: ICommandContext) {
if (!context.message.guild) {
return;
}
const commandName = context.args[1];
if (!commandName) {
this.SendUsage(context);
return;
}
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", context.message.guild.id);
const disabledCommands = disabledCommandsString != "" ? disabledCommandsString?.split(",") : [];
const disabledCommandsInstance = disabledCommands?.findIndex(x => x == commandName);
if (disabledCommandsInstance! > -1) {
disabledCommands?.splice(disabledCommandsInstance!, 1);
}
await SettingsHelper.SetSetting("commands.disabled", context.message.guild.id, disabledCommands!.join(","));
const embed = new PublicEmbed(context, "", `Enabled command: ${commandName}`);
embed.SendToCurrentChannel();
}
}

View file

@ -4,6 +4,7 @@ import Ban from "./commands/ban";
import Clear from "./commands/clear"; 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 Evaluate from "./commands/eval"; 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";
@ -34,6 +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())
} }
public static RegisterEvents(client: CoreClient) { public static RegisterEvents(client: CoreClient) {

View file

@ -5,6 +5,11 @@ import registry from "./registry";
dotenv.config(); dotenv.config();
const requiredConfigs: string[] = [ const requiredConfigs: string[] = [
"BOT_TOKEN",
"BOT_VER",
"BOT_AUTHOR",
"BOT_DATE",
"BOT_OWNERID",
]; ];
requiredConfigs.forEach(config => { requiredConfigs.forEach(config => {