From 9f75621c706b0173de5c8dc9570adf9a0f792483 Mon Sep 17 00:00:00 2001 From: Akiru Date: Fri, 26 Jan 2024 12:54:05 +0000 Subject: [PATCH] bot/mod.ts aktualisiert - Comment logging in "handleWebhook" function - Handle Errors when setting the Bot commands so that the entire bot doesn't fail when they can't be set due to a temporary error - Set default configuration for the bot (Job Limit) --- bot/mod.ts | 66 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/bot/mod.ts b/bot/mod.ts index 1d0e761..27b2e8d 100644 --- a/bot/mod.ts +++ b/bot/mod.ts @@ -1,7 +1,7 @@ -import { sequentialize } from "grammy_runner"; import { Api, Bot, Context, RawApi, session, SessionFlavor } from "grammy"; import { FileFlavor, hydrateFiles } from "grammy_files"; import { hydrateReply, ParseModeFlavor } from "grammy_parse_mode"; +import { sequentialize } from "grammy_runner"; import { error, info, warning } from "std/log/mod.ts"; import { sessions } from "../api/sessionsRoute.ts"; import { formatUserChat } from "../utils/formatUserChat.ts"; @@ -12,6 +12,16 @@ import { img2imgCommand, img2imgQuestion } from "./img2imgCommand.ts"; import { pnginfoCommand, pnginfoQuestion } from "./pnginfoCommand.ts"; import { queueCommand } from "./queueCommand.ts"; import { txt2imgCommand, txt2imgQuestion } from "./txt2imgCommand.ts"; +import { setConfig, getConfig } from "../app/config.ts"; + +// Set the new configuration +await setConfig({ maxUserJobs: 1, maxJobs: 500 }); + +// Fetch the updated configuration +const updatedConfig = await getConfig(); + +// Log the updated configuration to the console +console.log("Updated Configuration:", updatedConfig); interface SessionData { chat: ErisChatData; @@ -107,18 +117,38 @@ bot.use(async (ctx, next) => { } }); -bot.api.setMyShortDescription("I can generate furry images from text"); -bot.api.setMyDescription( - "I can generate furry images from text. " + - "Send /txt2img to generate an image.", -); -bot.api.setMyCommands([ - { command: "txt2img", description: "Generate image from text" }, - { command: "img2img", description: "Generate image from image" }, - { command: "pnginfo", description: "Try to extract prompt from raw file" }, - { command: "queue", description: "Show the current queue" }, - { command: "cancel", description: "Cancel all your requests" }, -]); +// Wrap the calls in try-catch for error handling +async function setupBotCommands() { + try { + await bot.api.setMyShortDescription("Generate furry images in '704x704', '576x832', '832x576'. https://ko-fi.com/nyxthebot https://nyx.akiru.de/"); + } catch (err) { + error(`Failed to set short description: ${err.message}`); + } + + try { + await bot.api.setMyDescription( + "I can generate furry images from text. If you want a different size, use 'Size: 576x832' for example." + + "Send /txt2img to generate an image.", + ); + } catch (err) { + error(`Failed to set description: ${err.message}`); + } + + try { + await bot.api.setMyCommands([ + { command: "txt2img", description: "Generate image from text" }, + { command: "img2img", description: "Generate image from image" }, + { command: "pnginfo", description: "Try to extract prompt from raw file" }, + { command: "queue", description: "Show the current queue" }, + { command: "cancel", description: "Cancel all your requests" }, + ]); + } catch (err) { + error(`Failed to set commands: ${err.message}`); + } +} + +// Call the setup function +setupBotCommands(); bot.command("start", async (ctx) => { if (ctx.match) { @@ -174,20 +204,20 @@ bot.command("crash", () => { }); // Set up the webhook in the telegram API and initialize the bot -await bot.api.setWebhook('https://nyxdev.akiru.de:8443/webhook'); +await bot.api.setWebhook('https://nyx.akiru.de/webhook'); await bot.init(); // Function to handle incoming webhook requests export async function handleWebhook(req: Request): Promise { try { const body = await req.json(); - console.log("Received webhook data:", JSON.stringify(body, null, 2)); + // console.log("Received webhook data:", JSON.stringify(body, null, 2)); // Log before processing update - console.log("Processing update through handleUpdate..."); + // console.log("Processing update through handleUpdate..."); await bot.handleUpdate(body); // Process the update // Log after processing update - console.log("Update processed successfully."); + // console.log("Update processed successfully."); return new Response(JSON.stringify({ status: 'ok' }), { headers: { 'Content-Type': 'application/json' } }); } catch (error) { @@ -195,4 +225,4 @@ export async function handleWebhook(req: Request): Promise { console.error("Error in handleWebhook:", error); return new Response("Error", { status: 500 }); } -} \ No newline at end of file +}