diff --git a/backend/src/index.ts b/backend/src/index.ts index 363ba806..142e3a07 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -29,7 +29,6 @@ import { runUpcomingScheduledPostsLoop } from "./data/loops/upcomingScheduledPos import { runExpiringTempbansLoop } from "./data/loops/expiringTempbansLoop"; import { runExpiringVCAlertsLoop } from "./data/loops/expiringVCAlertsLoop"; import { runExpiredArchiveDeletionLoop } from "./data/loops/expiredArchiveDeletionLoop"; -import blockedAt from "blocked-at"; if (!process.env.KEY) { // tslint:disable-next-line:no-console @@ -337,13 +336,6 @@ connect().then(async () => { logRateLimit(data); }); - blockedAt( - (time, stack) => { - console.error(`Blocked for ${time}ms, operation started here:`, stack); - }, - { threshold: 1000 }, - ); - bot.on("loadingFinished", async () => { runExpiringMutesLoop(); await sleep(10 * SECONDS); diff --git a/backend/src/plugins/BotControl/BotControlPlugin.ts b/backend/src/plugins/BotControl/BotControlPlugin.ts index 7897d837..d7a17b77 100644 --- a/backend/src/plugins/BotControl/BotControlPlugin.ts +++ b/backend/src/plugins/BotControl/BotControlPlugin.ts @@ -23,6 +23,7 @@ import { AddServerFromInviteCmd } from "./commands/AddServerFromInviteCmd"; import { ChannelToServerCmd } from "./commands/ChannelToServerCmd"; import { RestPerformanceCmd } from "./commands/RestPerformanceCmd"; import { RateLimitPerformanceCmd } from "./commands/RateLimitPerformanceCmd"; +import { ToggleBlockDetectionCmd } from "./commands/ToggleBlockDetectionCmd"; const defaultOptions = { config: { @@ -58,6 +59,7 @@ export const BotControlPlugin = zeppelinGlobalPlugin()({ RateLimitPerformanceCmd, AddServerFromInviteCmd, ChannelToServerCmd, + ToggleBlockDetectionCmd, ], async afterLoad(pluginData) { diff --git a/backend/src/plugins/BotControl/commands/ToggleBlockDetectionCmd.ts b/backend/src/plugins/BotControl/commands/ToggleBlockDetectionCmd.ts new file mode 100644 index 00000000..e8b32ec2 --- /dev/null +++ b/backend/src/plugins/BotControl/commands/ToggleBlockDetectionCmd.ts @@ -0,0 +1,33 @@ +import { commandTypeHelpers as ct } from "../../../commandTypes"; +import { botControlCmd } from "../types"; +import blockedAt from "blocked-at"; + +let stop; + +export const ToggleBlockDetectionCmd = botControlCmd({ + trigger: ["toggle_block_detection"], + permission: "can_performance", + + signature: { + threshold: ct.number({ required: false }), + }, + + async run({ pluginData, message: msg, args }) { + if (stop) { + stop(); + stop = null; + msg.channel.send("Disabled block detection"); + return; + } + + const threshold = args.threshold || 1000; + const result = blockedAt( + (time, stack) => { + console.error(`Blocked for ${time}ms, operation started here:`, stack); + }, + { threshold }, + ); + stop = result.stop; + msg.channel.send(`Block detection enabled with ${threshold}ms threshold`); + }, +});