From 3444fb01d0667e50194f57fbdca067e3f3431c45 Mon Sep 17 00:00:00 2001 From: pinks Date: Mon, 18 Sep 2023 17:51:19 +0200 Subject: [PATCH] feat: add cancel command --- bot/cancelCommand.ts | 9 +++++++++ bot/mod.ts | 8 ++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 bot/cancelCommand.ts diff --git a/bot/cancelCommand.ts b/bot/cancelCommand.ts new file mode 100644 index 0000000..5c8133a --- /dev/null +++ b/bot/cancelCommand.ts @@ -0,0 +1,9 @@ +import { jobStore } from "../db/jobStore.ts"; +import { Context } from "./mod.ts"; + +export async function cancelCommand(ctx: Context) { + const jobs = await jobStore.getBy("status.type", { value: "waiting" }); + const userJobs = jobs.filter((j) => j.value.from.id === ctx.from?.id); + for (const job of userJobs) await job.delete(); + await ctx.reply(`Cancelled ${userJobs.length} jobs`); +} diff --git a/bot/mod.ts b/bot/mod.ts index ad7dee6..08ba4c0 100644 --- a/bot/mod.ts +++ b/bot/mod.ts @@ -5,6 +5,7 @@ import { queueCommand } from "./queueCommand.ts"; import { txt2imgCommand, txt2imgQuestion } from "./txt2imgCommand.ts"; import { pnginfoCommand, pnginfoQuestion } from "./pnginfoCommand.ts"; import { img2imgCommand, img2imgQuestion } from "./img2imgCommand.ts"; +import { cancelCommand } from "./cancelCommand.ts"; export const logger = () => Log.getLogger(); @@ -98,10 +99,11 @@ bot.api.setMyDescription( "Send /txt2img to generate an image.", ); bot.api.setMyCommands([ - { command: "txt2img", description: "Generate an image from text" }, - { command: "img2img", description: "Generate an image based on another image" }, + { command: "txt2img", description: "Generate image from text" }, + { command: "img2img", description: "Generate image from image" }, { command: "pnginfo", description: "Show generation parameters of an image" }, { command: "queue", description: "Show the current queue" }, + { command: "cancel", description: "Cancel all your requests" }, ]); bot.command("start", (ctx) => ctx.reply("Hello! Use the /txt2img command to generate an image")); @@ -117,6 +119,8 @@ bot.use(pnginfoQuestion.middleware()); bot.command("queue", queueCommand); +bot.command("cancel", cancelCommand); + bot.command("pause", (ctx) => { if (!ctx.from?.username) return; const config = ctx.session.global;