From 9d8f4aed77234e99e8d528364a85129c413f4d62 Mon Sep 17 00:00:00 2001 From: 3urobeat <35304405+3urobeat@users.noreply.github.com> Date: Thu, 2 Jan 2025 13:24:20 +0100 Subject: [PATCH] feat: Add !manage command which implements manage module #228 --- src/commands/core/system.js | 97 +++++++++++++++++++++++++- src/data/fileStructure.json | 12 ++-- src/data/lang/chinese.json | 8 +++ src/data/lang/english.json | 8 +++ src/data/lang/portuguese.json | 8 +++ src/data/lang/russian.json | 8 +++ src/data/lang/traditional-chinese.json | 8 +++ 7 files changed, 141 insertions(+), 8 deletions(-) diff --git a/src/commands/core/system.js b/src/commands/core/system.js index 8fd76d8b..2fac496b 100644 --- a/src/commands/core/system.js +++ b/src/commands/core/system.js @@ -4,10 +4,10 @@ * Created Date: 2021-07-09 16:26:00 * Author: 3urobeat * - * Last Modified: 2024-05-03 13:08:52 + * Last Modified: 2025-01-02 13:23:12 * Modified By: 3urobeat * - * Copyright (c) 2021 - 2024 3urobeat + * Copyright (c) 2021 - 2025 3urobeat * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @@ -272,3 +272,96 @@ module.exports.eval = { } } }; + + +const availableManageModes = "addAccount, removeAccount, filterAccounts"; + +module.exports.manage = { + names: ["manage"], + description: "Interact with the manage module to administrate the active set of bot accounts", + args: [ + { + name: "mode", + description: `Available modes: '${availableManageModes}'`, + type: "string", + isOptional: false, + ownersOnly: true + }, + { + name: "argument", + description: "Argument(s) the selected mode requires. Run command without this parameter to display help for the selected mode", + type: "string", + isOptional: true, // Optional to display help for selected mode if no argument is provided + ownersOnly: true + } + ], + ownersOnly: true, + + /** + * The manage command + * @param {CommandHandler} commandHandler The commandHandler object + * @param {Array} args Array of arguments that will be passed to the command + * @param {function(object, object, string): void} respondModule Function that will be called to respond to the user's request. Passes context, resInfo and txt as parameters. + * @param {object} context The context (this.) of the object calling this command. Will be passed to respondModule() as first parameter. + * @param {CommandHandler.resInfo} resInfo Object containing additional information your respondModule might need to process the response (for example the userID who executed the command). + */ + run: async (commandHandler, args, respondModule, context, resInfo) => { + const respond = ((txt) => respondModule(context, resInfo, txt)); // Shorten each call + + const cmdusage = `'${resInfo.cmdprefix}manage mode argument'`; // TODO: Can't we dynamically generate this in the future? + + // Prevent error when calling toLowerCase below when user did not provide any mode + if (!args[0]) args[0] = ""; + + switch (args[0].toLowerCase()) { + case "addaccount": { // Block "fixes" eslint no-case-declarations warning + // Check if no or too short (= probably incorrect format) credentials were provided + if (!args[1] || args[1].split(":").length < 2) { + respond(await commandHandler.data.getLang("managecmdaddusage", { "credentialsformat": "username:password:sharedSecret", "optionalparameter": "sharedSecret", "cmdusage": cmdusage }, resInfo.userID)); + } + + // Split credentials into username, password and sharedSecret + const credentials = args[1].split(":"); + + // Run manage function with the provided credentials + commandHandler.controller.addAccount(credentials[0], credentials[1], credentials[2] || ""); + + respond(await commandHandler.data.getLang("managecmdaddsuccess", { "username": credentials[0] }, resInfo.userID)); + break; + } + + case "removeaccount": + // Check if no username was provided + if (!args[1]) return respond(await commandHandler.data.getLang("managecmdremoveusage", { "cmdusage": cmdusage }, resInfo.userID)); + + // Run manage function with the provided username + commandHandler.controller.removeAccount(args[1]); + + respond(await commandHandler.data.getLang("managecmdremovesuccess", { "username": args[1] }, resInfo.userID)); + break; + + case "filteraccounts": { // Block "fixes" eslint no-case-declarations warning + const availableFilters = Object.keys(commandHandler.controller.filters); + const filtersLowercase = availableFilters.map((e) => e.toLowerCase()); + + // Check if no filter or an invalid filter was provided + if (!args[1] || !filtersLowercase.includes(args[1].toLowerCase())) { + respond(await commandHandler.data.getLang("managecmdfilterusage", { "availablefilters": availableFilters.join(", "), "cmdusage": cmdusage }, resInfo.userID)); + return; + } + + // Find the selected filter while ignoring case + const selectedFilter = availableFilters.find((e) => e.toLowerCase() == args[1].toLowerCase()); + + // Run manage function with the selected filter + const filterResult = commandHandler.controller.filterAccounts(commandHandler.controller.filters[selectedFilter]); + + respond(await commandHandler.data.getLang("managecmdfiltersuccess", { "matchamount": filterResult.length, "selectedfilter": selectedFilter, "usernames": filterResult.map((e) => e.accountName).join(", ") })); + break; + } + + default: + respond(await commandHandler.data.getLang("managecmdusage", { "availablemodes": availableManageModes, "cmdusage": cmdusage }, resInfo.userID)); + } + } +}; diff --git a/src/data/fileStructure.json b/src/data/fileStructure.json index 42cb8b0a..879471bb 100644 --- a/src/data/fileStructure.json +++ b/src/data/fileStructure.json @@ -203,7 +203,7 @@ { "path": "src/commands/core/system.js", "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/commands/core/system.js", - "checksum": "b37fd364b9aa9f00d47b3a0d8094c700" + "checksum": "906a73f90236dd974fdf9d8e4cff7025" }, { "path": "src/commands/core/vote.js", @@ -338,27 +338,27 @@ { "path": "src/data/lang/chinese.json", "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/data/lang/chinese.json", - "checksum": "438eec25dd0c247c8b8396a9f2288d0c" + "checksum": "1b703865e31c2a604606b8e4dd092d69" }, { "path": "src/data/lang/english.json", "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/data/lang/english.json", - "checksum": "aa163f0d8a84f49ce00075a23a3f9e4d" + "checksum": "403032426144d8f05b859e183848671d" }, { "path": "src/data/lang/portuguese.json", "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/data/lang/portuguese.json", - "checksum": "2a0266a92ad46689e63ae386b3eaa48e" + "checksum": "7f360b933a4b317a4aeec502dfa8e233" }, { "path": "src/data/lang/russian.json", "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/data/lang/russian.json", - "checksum": "5766fa2429c79523f67405eeef397fbe" + "checksum": "10a3592d60de8d1b9542dd60eaba1c08" }, { "path": "src/data/lang/traditional-chinese.json", "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/data/lang/traditional-chinese.json", - "checksum": "82363a6ac21306e03e638f99bebd64ac" + "checksum": "3fbb763499dc7105b3c908aece6a6f4e" }, { "path": "src/dataManager/dataCheck.js", diff --git a/src/data/lang/chinese.json b/src/data/lang/chinese.json index 125944ec..e36b7ee8 100644 --- a/src/data/lang/chinese.json +++ b/src/data/lang/chinese.json @@ -144,5 +144,13 @@ "evalcmdturnedoff": "eval命令已关闭!", "evalcmdlogininfoblock": "您的代码包含'logininfo'。为了保护密码,这是不允许的。", + "managecmdaddusage": "Provide the credentials of the account to add to the current set of bot accounts as argument in this format:\n'${credentialsformat}' (${optionalparameter} is optional)\n\nCommand usage: ${cmdusage}", + "managecmdaddsuccess": "The bot will attempt to add & login '${username}'. Please check the log for potential errors.", + "managecmdremoveusage": "Provide the username (accountName) of the bot account to remove from the current set of bot accounts as argument.\n\nCommand usage: ${cmdusage}", + "managecmdremovesuccess": "The bot will log off & remove account '${username}' if it exists. Please check the log for potential errors.", + "managecmdfilterusage": "Provide one of these arguments to filter the current list of bot accounts and display their names:\n'${availablefilters}'\n\nCommand usage: ${cmdusage}", + "managecmdfiltersuccess": "These ${matchamount} bot accounts match the selected filter '${selectedfilter}':\n'${usernames}'", + "managecmdusage": "Provide one of these modes: '${availablemodes}'.\nRun command with mode but without argument parameter to display help for that selected mode.\n\nCommand usage: ${cmdusage}", + "childbotmessage": "这是一个运行在机器人群集中的帐户。\n请添加主要的机器人并发送给他一个${cmdprefix}help消息。\n如果您想了解这是什么,请键入:${cmdprefix}about\n这是主要的机器人账户:" } diff --git a/src/data/lang/english.json b/src/data/lang/english.json index b9c6f3ec..d97879f6 100644 --- a/src/data/lang/english.json +++ b/src/data/lang/english.json @@ -144,5 +144,13 @@ "evalcmdturnedoff": "The eval command has been turned off!", "evalcmdlogininfoblock": "Your code includes 'logininfo'. In order to protect passwords this is not allowed.", + "managecmdaddusage": "Provide the credentials of the account to add to the current set of bot accounts as argument in this format:\n'${credentialsformat}' (${optionalparameter} is optional)\n\nCommand usage: ${cmdusage}", + "managecmdaddsuccess": "The bot will attempt to add & login '${username}'. Please check the log for potential errors.", + "managecmdremoveusage": "Provide the username (accountName) of the bot account to remove from the current set of bot accounts as argument.\n\nCommand usage: ${cmdusage}", + "managecmdremovesuccess": "The bot will log off & remove account '${username}' if it exists. Please check the log for potential errors.", + "managecmdfilterusage": "Provide one of these arguments to filter the current list of bot accounts and display their names:\n'${availablefilters}'\n\nCommand usage: ${cmdusage}", + "managecmdfiltersuccess": "These ${matchamount} bot accounts match the selected filter '${selectedfilter}':\n'${usernames}'", + "managecmdusage": "Provide one of these modes: '${availablemodes}'.\nRun command with mode but without argument parameter to display help for that selected mode.\n\nCommand usage: ${cmdusage}", + "childbotmessage": "This is one account running in a bot cluster.\nPlease add the main bot and send him a ${cmdprefix}help message.\nIf you want to check out what this is about, type: ${cmdprefix}about\nThis is the main bot account:" } diff --git a/src/data/lang/portuguese.json b/src/data/lang/portuguese.json index dc4eeb10..85cf8e4d 100644 --- a/src/data/lang/portuguese.json +++ b/src/data/lang/portuguese.json @@ -144,5 +144,13 @@ "evalcmdturnedoff": "O comando eval foi desativado!", "evalcmdlogininfoblock": "Seu código inclui 'logininfo'. Para proteger senhas, isso não é permitido.", + "managecmdaddusage": "Provide the credentials of the account to add to the current set of bot accounts as argument in this format:\n'${credentialsformat}' (${optionalparameter} is optional)\n\nCommand usage: ${cmdusage}", + "managecmdaddsuccess": "The bot will attempt to add & login '${username}'. Please check the log for potential errors.", + "managecmdremoveusage": "Provide the username (accountName) of the bot account to remove from the current set of bot accounts as argument.\n\nCommand usage: ${cmdusage}", + "managecmdremovesuccess": "The bot will log off & remove account '${username}' if it exists. Please check the log for potential errors.", + "managecmdfilterusage": "Provide one of these arguments to filter the current list of bot accounts and display their names:\n'${availablefilters}'\n\nCommand usage: ${cmdusage}", + "managecmdfiltersuccess": "These ${matchamount} bot accounts match the selected filter '${selectedfilter}':\n'${usernames}'", + "managecmdusage": "Provide one of these modes: '${availablemodes}'.\nRun command with mode but without argument parameter to display help for that selected mode.\n\nCommand usage: ${cmdusage}", + "childbotmessage": "Esta é uma conta em execução em um cluster de bots.\nPor favor, adicione o bot principal e envie a ele uma mensagem ${cmdprefix}help.\nSe quiser saber do que se trata, digite: ${cmdprefix}about\nEsta é a conta principal do bot:" } diff --git a/src/data/lang/russian.json b/src/data/lang/russian.json index db3946fa..378490e2 100644 --- a/src/data/lang/russian.json +++ b/src/data/lang/russian.json @@ -144,5 +144,13 @@ "evalcmdturnedoff": "Команда eval была отключена!", "evalcmdlogininfoblock": "Ваш код включает «logininfo». В целях защиты паролей это запрещено.", + "managecmdaddusage": "Provide the credentials of the account to add to the current set of bot accounts as argument in this format:\n'${credentialsformat}' (${optionalparameter} is optional)\n\nCommand usage: ${cmdusage}", + "managecmdaddsuccess": "The bot will attempt to add & login '${username}'. Please check the log for potential errors.", + "managecmdremoveusage": "Provide the username (accountName) of the bot account to remove from the current set of bot accounts as argument.\n\nCommand usage: ${cmdusage}", + "managecmdremovesuccess": "The bot will log off & remove account '${username}' if it exists. Please check the log for potential errors.", + "managecmdfilterusage": "Provide one of these arguments to filter the current list of bot accounts and display their names:\n'${availablefilters}'\n\nCommand usage: ${cmdusage}", + "managecmdfiltersuccess": "These ${matchamount} bot accounts match the selected filter '${selectedfilter}':\n'${usernames}'", + "managecmdusage": "Provide one of these modes: '${availablemodes}'.\nRun command with mode but without argument parameter to display help for that selected mode.\n\nCommand usage: ${cmdusage}", + "childbotmessage": "Это один аккаунт, работающий в группе ботов.\nПожалуйста, добавьте главного бота и отправьте ему сообщение ${cmdprefix}help.\nЕсли вы хотите проверить, в чём дело, введите: ${cmdprefix}about\nЭто главный аккаунт бота:" } diff --git a/src/data/lang/traditional-chinese.json b/src/data/lang/traditional-chinese.json index 9e654df6..a3771fa0 100644 --- a/src/data/lang/traditional-chinese.json +++ b/src/data/lang/traditional-chinese.json @@ -143,5 +143,13 @@ "evalcmdturnedoff": "eval指令已關閉", "evalcmdlogininfoblock": "您的程式碼包含'logininfo'這是保護密碼的\n請使用其他的", + "managecmdaddusage": "Provide the credentials of the account to add to the current set of bot accounts as argument in this format:\n'${credentialsformat}' (${optionalparameter} is optional)\n\nCommand usage: ${cmdusage}", + "managecmdaddsuccess": "The bot will attempt to add & login '${username}'. Please check the log for potential errors.", + "managecmdremoveusage": "Provide the username (accountName) of the bot account to remove from the current set of bot accounts as argument.\n\nCommand usage: ${cmdusage}", + "managecmdremovesuccess": "The bot will log off & remove account '${username}' if it exists. Please check the log for potential errors.", + "managecmdfilterusage": "Provide one of these arguments to filter the current list of bot accounts and display their names:\n'${availablefilters}'\n\nCommand usage: ${cmdusage}", + "managecmdfiltersuccess": "These ${matchamount} bot accounts match the selected filter '${selectedfilter}':\n'${usernames}'", + "managecmdusage": "Provide one of these modes: '${availablemodes}'.\nRun command with mode but without argument parameter to display help for that selected mode.\n\nCommand usage: ${cmdusage}", + "childbotmessage": "這是接收支援的機器人\n請使用以下機器人:" }