forked from pinks/eris
1
0
Fork 0

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)
This commit is contained in:
Akiru 2024-01-26 12:54:05 +00:00
parent b7a551afc7
commit 9f75621c70
1 changed files with 48 additions and 18 deletions

View File

@ -1,7 +1,7 @@
import { sequentialize } from "grammy_runner";
import { Api, Bot, Context, RawApi, session, SessionFlavor } from "grammy"; import { Api, Bot, Context, RawApi, session, SessionFlavor } from "grammy";
import { FileFlavor, hydrateFiles } from "grammy_files"; import { FileFlavor, hydrateFiles } from "grammy_files";
import { hydrateReply, ParseModeFlavor } from "grammy_parse_mode"; import { hydrateReply, ParseModeFlavor } from "grammy_parse_mode";
import { sequentialize } from "grammy_runner";
import { error, info, warning } from "std/log/mod.ts"; import { error, info, warning } from "std/log/mod.ts";
import { sessions } from "../api/sessionsRoute.ts"; import { sessions } from "../api/sessionsRoute.ts";
import { formatUserChat } from "../utils/formatUserChat.ts"; import { formatUserChat } from "../utils/formatUserChat.ts";
@ -12,6 +12,16 @@ import { img2imgCommand, img2imgQuestion } from "./img2imgCommand.ts";
import { pnginfoCommand, pnginfoQuestion } from "./pnginfoCommand.ts"; import { pnginfoCommand, pnginfoQuestion } from "./pnginfoCommand.ts";
import { queueCommand } from "./queueCommand.ts"; import { queueCommand } from "./queueCommand.ts";
import { txt2imgCommand, txt2imgQuestion } from "./txt2imgCommand.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 { interface SessionData {
chat: ErisChatData; chat: ErisChatData;
@ -107,18 +117,38 @@ bot.use(async (ctx, next) => {
} }
}); });
bot.api.setMyShortDescription("I can generate furry images from text"); // Wrap the calls in try-catch for error handling
bot.api.setMyDescription( async function setupBotCommands() {
"I can generate furry images from text. " + try {
"Send /txt2img to generate an image.", await bot.api.setMyShortDescription("Generate furry images in '704x704', '576x832', '832x576'. https://ko-fi.com/nyxthebot https://nyx.akiru.de/");
); } catch (err) {
bot.api.setMyCommands([ error(`Failed to set short description: ${err.message}`);
{ command: "txt2img", description: "Generate image from text" }, }
{ command: "img2img", description: "Generate image from image" },
{ command: "pnginfo", description: "Try to extract prompt from raw file" }, try {
{ command: "queue", description: "Show the current queue" }, await bot.api.setMyDescription(
{ command: "cancel", description: "Cancel all your requests" }, "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) => { bot.command("start", async (ctx) => {
if (ctx.match) { if (ctx.match) {
@ -174,20 +204,20 @@ bot.command("crash", () => {
}); });
// Set up the webhook in the telegram API and initialize the bot // 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(); await bot.init();
// Function to handle incoming webhook requests // Function to handle incoming webhook requests
export async function handleWebhook(req: Request): Promise<Response> { export async function handleWebhook(req: Request): Promise<Response> {
try { try {
const body = await req.json(); 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 // Log before processing update
console.log("Processing update through handleUpdate..."); // console.log("Processing update through handleUpdate...");
await bot.handleUpdate(body); // Process the update await bot.handleUpdate(body); // Process the update
// Log after processing 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' } }); return new Response(JSON.stringify({ status: 'ok' }), { headers: { 'Content-Type': 'application/json' } });
} catch (error) { } catch (error) {
@ -195,4 +225,4 @@ export async function handleWebhook(req: Request): Promise<Response> {
console.error("Error in handleWebhook:", error); console.error("Error in handleWebhook:", error);
return new Response("Error", { status: 500 }); return new Response("Error", { status: 500 });
} }
} }