diff --git a/main.ts b/main.ts index d77e6ce..81e6ee1 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,5 @@ -import { bot } from "./src/mod.ts"; +import { initBot } from "./src/mod.ts"; if (import.meta.main) { - await bot.start(); + initBot(); } diff --git a/src/bot.ts b/src/bot.ts index f20b6ef..5106ebf 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -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(BOT_TOKEN); -setupSession(bot); -setupConversations(bot); -setupCommands(bot); -setupCallbackQueries(bot); -setupMessages(bot); +export const initBot = () => { + const bot = new Bot(BOT_TOKEN) + + setupErrorHandling(bot); + setupSession(bot); + setupConversations(bot); + setupCommands(bot); + setupCallbackQueries(bot); + setupMessages(bot); + bot.start(); +} diff --git a/src/callback_queries/setup_callback_queries.ts b/src/callback_queries/setup_callback_queries.ts index 19c2cd6..57dc231 100644 --- a/src/callback_queries/setup_callback_queries.ts +++ b/src/callback_queries/setup_callback_queries.ts @@ -6,20 +6,20 @@ import { parseCallbackQueryData } from "./utils.ts"; export const setupCallbackQueries = (bot: Bot) => { - 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"); diff --git a/src/callback_queries/vote_callback.ts b/src/callback_queries/vote_callback.ts index ef2d9a5..724212b 100644 --- a/src/callback_queries/vote_callback.ts +++ b/src/callback_queries/vote_callback.ts @@ -13,18 +13,17 @@ export const voteCallback = async ( bot: Bot, 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", ); diff --git a/src/error_handling/mod.ts b/src/error_handling/mod.ts new file mode 100644 index 0000000..928af8e --- /dev/null +++ b/src/error_handling/mod.ts @@ -0,0 +1 @@ +export { setupErrorHandling } from "./setup_error_handling.ts"; \ No newline at end of file diff --git a/src/error_handling/setup_error_handling.ts b/src/error_handling/setup_error_handling.ts new file mode 100644 index 0000000..a8f8d71 --- /dev/null +++ b/src/error_handling/setup_error_handling.ts @@ -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) => { + 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); + } + }); + +}; diff --git a/src/messages/setup_messages.ts b/src/messages/setup_messages.ts index 6557e5c..d244a71 100644 --- a/src/messages/setup_messages.ts +++ b/src/messages/setup_messages.ts @@ -4,9 +4,9 @@ import { COMMUNITY_GROUP_ID } from "../config/mod.ts"; import { postCredits } from "./post_credits.ts"; export const setupMessages = (bot: Bot) => { - 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); } }); diff --git a/src/mod.ts b/src/mod.ts index ba8ff86..1a891cf 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -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";