feat: add error handling and add missing await

main
drendog 2024-01-31 23:21:08 +01:00
parent eed9837f1d
commit 3810f54b27
8 changed files with 45 additions and 17 deletions

View File

@ -1,5 +1,5 @@
import { bot } from "./src/mod.ts";
import { initBot } from "./src/mod.ts";
if (import.meta.main) {
await bot.start();
initBot();
}

View File

@ -10,11 +10,17 @@ import { setupCommands } from "./commands/mod.ts";
import { setupConversations } from "./conversations/mod.ts";
import { setupSession } from "./session/mod.ts";
import { setupMessages } from "./messages/mod.ts";
import { setupErrorHandling } from "./error_handling/mod.ts";
export const bot = new Bot<BotContext>(BOT_TOKEN);
export const initBot = () => {
const bot = new Bot<BotContext>(BOT_TOKEN)
setupErrorHandling(bot);
setupSession(bot);
setupConversations(bot);
setupCommands(bot);
setupCallbackQueries(bot);
setupMessages(bot);
bot.start();
}

View File

@ -6,20 +6,20 @@ import { parseCallbackQueryData } from "./utils.ts";
export const setupCallbackQueries = (bot: Bot<BotContext>) => {
bot.on("callback_query:data", (ctx) => {
bot.on("callback_query:data", async (ctx) => {
const callbackQueryData = parseCallbackQueryData(ctx.callbackQuery.data);
switch (callbackQueryData.cq) {
case CallbackQueryEnum.APPROVE:
case CallbackQueryEnum.REJECT:
voteCallback(ctx, bot, callbackQueryData);
await voteCallback(ctx, bot, callbackQueryData);
break;
case CallbackQueryEnum.NOOP:
break;
default:
throw new Error("Invalid callback query data");
}
ctx.answerCallbackQuery();
await ctx.answerCallbackQuery();
});
console.log("Callback Queries setup complete");

View File

@ -13,18 +13,17 @@ export const voteCallback = async (
bot: Bot<BotContext>,
callbackQueryData: IButtonCallbackData,
) => {
await ctx.answerCallbackQuery();
if (!callbackQueryData.sid) return;
const isApproved = callbackQueryData.cq === CallbackQueryEnum.APPROVE;
if (isApproved) {
await ctx.copyMessage(CHANNEL_ID);
bot.api.sendMessage(
await bot.api.sendMessage(
getChatIdFromSession(callbackQueryData.sid),
"Post Approvato",
);
} else {
bot.api.sendMessage(
await bot.api.sendMessage(
getChatIdFromSession(callbackQueryData.sid),
"Post Rifiutato",
);

View File

@ -0,0 +1 @@
export { setupErrorHandling } from "./setup_error_handling.ts";

View File

@ -0,0 +1,22 @@
import { Bot } from "grammy";
import { BotContext } from "../mod.ts";
import { GROUP_ID } from "../config/mod.ts";
export const setupErrorHandling = (bot: Bot<BotContext>) => {
bot.catch(async (err) => {
try {
await bot.api.sendMessage(GROUP_ID, `🚨⚠️ ERROR ⚠️🚨
\`\`\`Log
Error: ${err.error}
Cause: ${err.cause}
Message: ${err.message}
Name: ${err.name}
Stack: ${err.stack}\`\`\``, {
parse_mode: "MarkdownV2",
});
} catch (error) {
console.error("Failed to send error message to the group:", error);
}
});
};

View File

@ -4,9 +4,9 @@ import { COMMUNITY_GROUP_ID } from "../config/mod.ts";
import { postCredits } from "./post_credits.ts";
export const setupMessages = (bot: Bot<BotContext>) => {
bot.on("message:is_automatic_forward", (ctx) => {
bot.on("message:is_automatic_forward", async (ctx) => {
if (ctx.chat.id === Number(COMMUNITY_GROUP_ID)) {
postCredits(ctx, ctx.message.message_id);
await postCredits(ctx, ctx.message.message_id);
}
});

View File

@ -1,4 +1,4 @@
export { bot } from "./bot.ts";
export { initBot } from "./bot.ts";
import { Context, SessionFlavor } from "grammy";
import { ConversationFlavor } from "grammy_conversations";
import { SessionData } from "./session/mod.ts";