feat: add error handling and add missing await
parent
eed9837f1d
commit
3810f54b27
4
main.ts
4
main.ts
|
@ -1,5 +1,5 @@
|
||||||
import { bot } from "./src/mod.ts";
|
import { initBot } from "./src/mod.ts";
|
||||||
|
|
||||||
if (import.meta.main) {
|
if (import.meta.main) {
|
||||||
await bot.start();
|
initBot();
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,11 +10,17 @@ import { setupCommands } from "./commands/mod.ts";
|
||||||
import { setupConversations } from "./conversations/mod.ts";
|
import { setupConversations } from "./conversations/mod.ts";
|
||||||
import { setupSession } from "./session/mod.ts";
|
import { setupSession } from "./session/mod.ts";
|
||||||
import { setupMessages } from "./messages/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);
|
setupSession(bot);
|
||||||
setupConversations(bot);
|
setupConversations(bot);
|
||||||
setupCommands(bot);
|
setupCommands(bot);
|
||||||
setupCallbackQueries(bot);
|
setupCallbackQueries(bot);
|
||||||
setupMessages(bot);
|
setupMessages(bot);
|
||||||
|
bot.start();
|
||||||
|
}
|
||||||
|
|
|
@ -6,20 +6,20 @@ import { parseCallbackQueryData } from "./utils.ts";
|
||||||
|
|
||||||
|
|
||||||
export const setupCallbackQueries = (bot: Bot<BotContext>) => {
|
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);
|
const callbackQueryData = parseCallbackQueryData(ctx.callbackQuery.data);
|
||||||
|
|
||||||
switch (callbackQueryData.cq) {
|
switch (callbackQueryData.cq) {
|
||||||
case CallbackQueryEnum.APPROVE:
|
case CallbackQueryEnum.APPROVE:
|
||||||
case CallbackQueryEnum.REJECT:
|
case CallbackQueryEnum.REJECT:
|
||||||
voteCallback(ctx, bot, callbackQueryData);
|
await voteCallback(ctx, bot, callbackQueryData);
|
||||||
break;
|
break;
|
||||||
case CallbackQueryEnum.NOOP:
|
case CallbackQueryEnum.NOOP:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error("Invalid callback query data");
|
throw new Error("Invalid callback query data");
|
||||||
}
|
}
|
||||||
ctx.answerCallbackQuery();
|
await ctx.answerCallbackQuery();
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Callback Queries setup complete");
|
console.log("Callback Queries setup complete");
|
||||||
|
|
|
@ -13,18 +13,17 @@ export const voteCallback = async (
|
||||||
bot: Bot<BotContext>,
|
bot: Bot<BotContext>,
|
||||||
callbackQueryData: IButtonCallbackData,
|
callbackQueryData: IButtonCallbackData,
|
||||||
) => {
|
) => {
|
||||||
await ctx.answerCallbackQuery();
|
|
||||||
if (!callbackQueryData.sid) return;
|
if (!callbackQueryData.sid) return;
|
||||||
|
|
||||||
const isApproved = callbackQueryData.cq === CallbackQueryEnum.APPROVE;
|
const isApproved = callbackQueryData.cq === CallbackQueryEnum.APPROVE;
|
||||||
if (isApproved) {
|
if (isApproved) {
|
||||||
await ctx.copyMessage(CHANNEL_ID);
|
await ctx.copyMessage(CHANNEL_ID);
|
||||||
bot.api.sendMessage(
|
await bot.api.sendMessage(
|
||||||
getChatIdFromSession(callbackQueryData.sid),
|
getChatIdFromSession(callbackQueryData.sid),
|
||||||
"Post Approvato",
|
"Post Approvato",
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
bot.api.sendMessage(
|
await bot.api.sendMessage(
|
||||||
getChatIdFromSession(callbackQueryData.sid),
|
getChatIdFromSession(callbackQueryData.sid),
|
||||||
"Post Rifiutato",
|
"Post Rifiutato",
|
||||||
);
|
);
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
export { setupErrorHandling } from "./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<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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
|
@ -4,9 +4,9 @@ import { COMMUNITY_GROUP_ID } from "../config/mod.ts";
|
||||||
import { postCredits } from "./post_credits.ts";
|
import { postCredits } from "./post_credits.ts";
|
||||||
|
|
||||||
export const setupMessages = (bot: Bot<BotContext>) => {
|
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)) {
|
if (ctx.chat.id === Number(COMMUNITY_GROUP_ID)) {
|
||||||
postCredits(ctx, ctx.message.message_id);
|
await postCredits(ctx, ctx.message.message_id);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
export { bot } from "./bot.ts";
|
export { initBot } from "./bot.ts";
|
||||||
import { Context, SessionFlavor } from "grammy";
|
import { Context, SessionFlavor } from "grammy";
|
||||||
import { ConversationFlavor } from "grammy_conversations";
|
import { ConversationFlavor } from "grammy_conversations";
|
||||||
import { SessionData } from "./session/mod.ts";
|
import { SessionData } from "./session/mod.ts";
|
||||||
|
|
Loading…
Reference in New Issue