add command enabling/disabling, only by admins

This commit is contained in:
2025-06-27 15:28:51 +02:00
parent c960ae49bf
commit de492718fe
7 changed files with 73 additions and 6 deletions

View File

@@ -28,4 +28,4 @@ export default new Command('admingive', ['admingive'], [], async msg => {
await sendMessage(`Failed to give ${target.displayName} ${amount} ${item.prettyName + (amount === 1 ? '' : item.plural)}`, msg.messageId);
};
await target.clearLock();
});
}, false);

View File

@@ -0,0 +1,16 @@
import { redis } from "bun";
import commands, { Command, sendMessage } from ".";
import { isAdmin } from "../lib/admins";
import parseCommandArgs from "../lib/parseCommandArgs";
export default new Command('disablecommand', ['disablecommand'], [], async msg => {
if (!await isAdmin(msg.chatterId)) return;
const args = parseCommandArgs(msg.messageText);
if (!args[0]) { await sendMessage('Please specify a command to disable', msg.messageId); return; };
const selection = commands.get(args[0].toLowerCase());
if (!selection) { await sendMessage(`There is no ${args[0]} command`, msg.messageId); return; };
if (!selection.disableable) { await sendMessage(`Cannot disable ${selection.name} as the command is not disableable`, msg.messageId); return; };
const result = await redis.sadd('disabledcommands', selection.name);
if (result === 0) { await sendMessage(`The ${selection.name} command is already disabled`, msg.messageId); return; };
await sendMessage(`Successfully disabled the ${selection.name} command`, msg.messageId);
}, false);

View File

@@ -0,0 +1,15 @@
import { redis } from "bun";
import commands, { Command, sendMessage } from ".";
import { isAdmin } from "../lib/admins";
import parseCommandArgs from "../lib/parseCommandArgs";
export default new Command('enablecommand', ['enablecommand'], [], async msg => {
if (!await isAdmin(msg.chatterId)) return;
const args = parseCommandArgs(msg.messageText);
if (!args[0]) { await sendMessage('Please specify a command to enable', msg.messageId); return; };
const selection = commands.get(args[0].toLowerCase());
if (!selection) { await sendMessage(`There is no ${args[0]} command`, msg.messageId); return; };
const result = await redis.srem('disabledcommands', selection.name);
if (result === 0) { await sendMessage(`The ${selection.name} command isn't disabled`, msg.messageId); return; };
await sendMessage(`Successfully enabled the ${selection.name} command`, msg.messageId);
}, false);

View File

@@ -0,0 +1,23 @@
import { redis } from "bun";
import { basecommands, Command, sendMessage } from ".";
import parseCommandArgs from "../lib/parseCommandArgs";
export default new Command('getcommands', ['getcommands', 'getc'], [], async msg => {
const args = parseCommandArgs(msg.messageText);
if (!args[0]) { await sendMessage(`A list of commands can be found here: https://github.com/qwerinope/qweribot#commands`, msg.messageId); return; };
const disabledcommands = await redis.smembers('disabledcommands');
if (args[0].toLowerCase() === 'enabled') {
const commandnames: string[] = [];
for (const [name, command] of Array.from(basecommands.entries())) {
if (!command.disableable) continue;
if (disabledcommands.includes(name)) continue;
commandnames.push(name);
};
if (commandnames.length === 0) await sendMessage('No commands besides non-disableable commands are enabled', msg.messageId);
else await sendMessage(`Currently enabled commands: ${commandnames.join(', ')}`, msg.messageId);
} else if (args[0].toLowerCase() === 'disabled') {
if (disabledcommands.length === 0) await sendMessage('No commands are disabled', msg.messageId);
else await sendMessage(`Currently disabled commands: ${disabledcommands.join(', ')}`);
}
else await sendMessage('Please specify if you want the enabled or disabled commands', msg.messageId);
}, false);

View File

@@ -6,18 +6,21 @@ export class Command {
public readonly name: string;
public readonly aliases: string[];
public readonly requiredIntents: string[];
public readonly disableable: boolean;
public readonly execute: (message: EventSubChannelChatMessageEvent, sender: User) => Promise<void>;
constructor(name: string, aliases: string[], requiredIntents: string[], execution: (message: EventSubChannelChatMessageEvent, sender: User) => Promise<void>) {
this.name = name;
constructor(name: string, aliases: string[], requiredIntents: string[], execution: (message: EventSubChannelChatMessageEvent, sender: User) => Promise<void>, disableable?: boolean) {
this.name = name.toLowerCase();
this.aliases = aliases;
this.requiredIntents = requiredIntents;
this.execute = execution;
this.disableable = disableable ?? true;
};
};
import { readdir } from 'node:fs/promises';
const commands = new Map<string, Command>;
const commandintents: string[] = [];
const basecommands = new Map<string, Command>;
const files = await readdir(import.meta.dir);
for (const file of files) {
@@ -25,6 +28,7 @@ for (const file of files) {
if (file === import.meta.file) continue;
const command: Command = await import(import.meta.dir + '/' + file.slice(0, -3)).then(a => a.default);
commandintents.push(...command.requiredIntents);
basecommands.set(command.name, command);
for (const alias of command.aliases) {
commands.set(alias, command); // Since it's not a primitive type the map is filled with references to the command, not the actual object
};
@@ -36,7 +40,7 @@ for (const [name, item] of Array.from(items)) {
};
export default commands;
export { commandintents };
export { commandintents, basecommands };
import { singleUserMode, chatterApi, chatterId, streamerId } from "..";