feat(pause): allow specifying reason for pausing

This commit is contained in:
pinks 2023-09-03 23:37:13 +02:00
parent 124f711118
commit 2030b382f4
1 changed files with 8 additions and 7 deletions

15
main.ts
View File

@ -21,7 +21,7 @@ import { MessageEntity } from "https://deno.land/x/grammy@v1.18.1/types.ts";
const maxUserJobs = 3; const maxUserJobs = 3;
const maxJobs = 10; const maxJobs = 10;
let isRunning = true; let pausedReason: string | null = null;
const sdApiUrl = Deno.env.get("SD_API_URL"); const sdApiUrl = Deno.env.get("SD_API_URL");
if (!sdApiUrl) throw new Error("SD_API_URL not set"); if (!sdApiUrl) throw new Error("SD_API_URL not set");
@ -65,8 +65,8 @@ bot.command("txt2img", async (ctx) => {
if (!ctx.from?.id) { if (!ctx.from?.id) {
return ctx.reply("I don't know who you are"); return ctx.reply("I don't know who you are");
} }
if (!isRunning) { if (pausedReason != null) {
return ctx.reply("I'm currently paused. Try again later."); return ctx.reply(`I'm paused: ${pausedReason}`);
} }
if (queue.length >= maxJobs) { if (queue.length >= maxJobs) {
return ctx.reply( return ctx.reply(
@ -123,16 +123,17 @@ bot.command("queue", async (ctx) => {
bot.command("pause", async (ctx) => { bot.command("pause", async (ctx) => {
if (!ctx.from?.username) return; if (!ctx.from?.username) return;
if (!adminUsernames.includes(ctx.from.username)) return; if (!adminUsernames.includes(ctx.from.username)) return;
if (!isRunning) return await ctx.reply("Already paused"); if (pausedReason != null)
isRunning = false; return await ctx.reply(`Already paused: ${pausedReason}`);
pausedReason = ctx.match ?? "No reason given";
return await ctx.reply("Paused"); return await ctx.reply("Paused");
}); });
bot.command("resume", async (ctx) => { bot.command("resume", async (ctx) => {
if (!ctx.from?.username) return; if (!ctx.from?.username) return;
if (!adminUsernames.includes(ctx.from.username)) return; if (!adminUsernames.includes(ctx.from.username)) return;
if (isRunning) return await ctx.reply("Already running"); if (pausedReason == null) return await ctx.reply("Already running");
isRunning = true; pausedReason = null;
return await ctx.reply("Resumed"); return await ctx.reply("Resumed");
}); });