From 591ee3470518363532645c52c531ef1069754354 Mon Sep 17 00:00:00 2001 From: JandereDev Date: Thu, 9 Dec 2021 22:45:54 +0100 Subject: [PATCH] command to manually scan user list --- src/bot/commands/botctl.ts | 57 ++++++++++++++++++++++++++++++++++++ src/bot/modules/user_scan.ts | 2 ++ 2 files changed, 59 insertions(+) create mode 100644 src/bot/commands/botctl.ts diff --git a/src/bot/commands/botctl.ts b/src/bot/commands/botctl.ts new file mode 100644 index 0000000..d8ca4c3 --- /dev/null +++ b/src/bot/commands/botctl.ts @@ -0,0 +1,57 @@ +import { FindOneResult } from "monk"; +import { client } from "../.."; +import Command from "../../struct/Command"; +import MessageCommandContext from "../../struct/MessageCommandContext"; +import ServerConfig from "../../struct/ServerConfig"; +import { scanServer } from "../modules/user_scan"; +import { isBotManager, NO_MANAGER_MSG } from "../util"; + +let userscans: string[] = []; + +export default { + name: 'botctl', + aliases: null, + description: 'Perform administrative actions', + run: async (message: MessageCommandContext, args: string[]) => { + if (!isBotManager(message.member!, message.serverContext)) return message.reply(NO_MANAGER_MSG); + + let action = args.shift(); + switch(action) { + case 'scan_userlist': + try { + let serverConf: FindOneResult = await client.db.get('servers').findOne({ id: message.serverContext._id }); + + if (!serverConf?.userScan?.enable) return message.reply(`User scanning is not enabled for this server.`); + if (userscans.includes(message.serverContext._id)) return message.reply(`There is already a scan running for this server.`); + userscans.push(message.serverContext._id); + + let msg = await message.reply(`Fetching users...`); + + let counter = 0; + + let onUserScan = async () => { + counter++; + if (counter % 10 == 0) await msg?.edit({ content: `Fetching users... ${counter}` }); + } + + let onDone = async () => { + msg?.edit({ content: `All done! (${counter} users fetched)` }); + userscans = userscans.filter(s => s != message.serverContext._id); + } + + await scanServer(message.serverContext._id, onUserScan, onDone); + } catch(e) { + message.reply(`An error occurred: ${e}`); + userscans = userscans.filter(s => s != message.serverContext._id); + } + break; + case undefined: + case '': + message.reply(`### Available subcommands\n` + + `- \`scan_userlist\` - If user scanning is enabled, this will scan the entire user list.`); + break + default: + message.reply(`Unknown option`); + } + } +} as Command; diff --git a/src/bot/modules/user_scan.ts b/src/bot/modules/user_scan.ts index b566394..6a23d84 100644 --- a/src/bot/modules/user_scan.ts +++ b/src/bot/modules/user_scan.ts @@ -200,3 +200,5 @@ new Promise((res: (value: void) => void) => client.user ? res() : client.once('r } catch(e) { console.error(e) } }); }); + +export { scanServer };