From aa6f2d5ca76b42e20ca700816c6b06929bf15c20 Mon Sep 17 00:00:00 2001 From: Sudo Space Date: Fri, 30 Aug 2024 19:48:18 +0330 Subject: [PATCH] Making the project more readable The feature of editing text messages and captions is also added in this commit --- .gitignore | 2 +- bun.lockb | Bin 57758 -> 58099 bytes package.json | 1 + src/app.ts | 84 +++++++-------------------------------- src/composer/commands.ts | 18 +++++++++ src/composer/edits.ts | 35 ++++++++++++++++ src/composer/messages.ts | 62 +++++++++++++++++++++++++++++ src/utils/utils.ts | 9 ++--- 8 files changed, 136 insertions(+), 75 deletions(-) create mode 100644 src/composer/commands.ts create mode 100644 src/composer/edits.ts create mode 100644 src/composer/messages.ts diff --git a/.gitignore b/.gitignore index 2f21777..a8d4585 100644 --- a/.gitignore +++ b/.gitignore @@ -175,5 +175,5 @@ dist .DS_Store # Custom ignores -config.json +config.env database.sqlite \ No newline at end of file diff --git a/bun.lockb b/bun.lockb index 92d75d8cfbd5bc9a8da4938dd047a567dc260941..6ff76cc687a0c178a3f5884f6d767773be124f20 100755 GIT binary patch delta 3252 zcmeH}eN0tl9LLY$UIed*lz<2!&Ct38_g=WX2n1dPdzs>j)krbAgn%z_U5dPn@>1!9 zmd<+Cl#3V=Bp@bu2eef)*VIa7kWwq>W^6fIjcWBr&5ing&wcK8TX%Eezx}q)`99C@ z_q?6woZorQ(NA^1ozopO8K?VVtJbxgtsjlfPKhd-&iww@J-I*2x#syD#~2wO?O`48GUfm(8jl%O_S574TH<{ zVQ3>P_aq9tAy};uedd+w3GoB2KMSK}Wq>(BY_()L=b#k{1oHq#jsWbv4A7`L@DNY| zCe<_nN}~pVL{?x_)=jLye3kvpfND_`Cqye~9a2zczzR5FI-pVM&j5OmE$fd+&4JRW zY?lkT02^REPwFZt{RMzlAm{gScsi{1#1-njpct^@dcXlU0PdO{(CpgPql}jUwl9}{ zf691;=l)SZg&iwp2VbSXO2#+KxUVw41#rM^GVV{=u3ENp$aYR|fyxHk0q<-Vp!rii zf<`UsQBK?h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SBC;40JIvb#f8)!UY1-0bz5DvDR#94<<5*QZH`K2*S8zuA-=E z{}+0B&`Y^VJ|K7loLlQfQ_FOsWiQ%n6LCe)>VHM$X8odz$wCm(?Efn zQHZO;@^uBOgWqO?aDfkz5VF0s261f+Saiol2mYJel}APHx0izuQg6y4rc) zGJAdVOph4m&B!`X{%Izi7GHQ~bFP#T9cfs|Gxb&~w+$?&`{8^Un(F9xp zy~tJ9ui;Xx+orBOC!kU{K>hy!QAPK?{2uQ8$WqcFpaH~eerFu_e(%%2I<5lEM77iH z%&v0l+Aa&_FK^!>emfA7zDCj7U(pL*Bc4+qOn`piH}t$onk zZrNyVwWON5owar_9NbwwC;W5AN7*UAAJQ_ktXn}-Igw$m1oO+L|B6WAgP_GKm!fI) zv&)HCmReAmc(b^7ejtIT`EHI5Z{_I3iC%quZcG=^0+9Ma<&xlGkOn|8-6Po`=@L+E zHspYG1QgSWwmAByB;*Dkz)L|I1QpEEs8Fci3=ceNGWxQzuao=+F2T2O8OC57&YGG& zUxD60Bi0BtB8}iX_#Siu=Yme?ui zRG$Q0TgTxvjKCSt)pZIqBf1u!hZo>Q*bFa04}1W+!C!+`cpaGaL}!{aO?$sDptDCe zohC8_pTHLA1kO#X3*Lgg@HXrN{RvnPb?_8C4IAJYsDdY;0yNKMPzXL)21lU-_CUS5 z*vdmUybJHaHh2}9pd37q2ghItw!;p18T9vW1>}Ri;aX^hhrkcDPy)qJ04re)O!o7G zazpyy`jS>Kd}CmJ{35MptzNyp*c9RiWsIAW!93GA=v~;nYpdViZdp%W+kc^>+jBBb zD_;s<986kZ`_0wyh4!eqJzkemujPV5<7}l$%9*YS@1i@r5?{Y-Uup4WyDe>c(k+!Z T6}~jl?lGM|C4~c1yY0UL9o1IK diff --git a/package.json b/package.json index e3a3b21..b61d8f5 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "@types/bun": "latest" }, "dependencies": { + "dotenv": "^16.4.5", "grammy": "^1.29.0", "sequelize": "^6.37.3", "sqlite3": "^5.1.7" diff --git a/src/app.ts b/src/app.ts index 3846c5a..e5bfdef 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,79 +1,25 @@ import { Bot } from "grammy"; -import { connectToDatabase, readConfigFile } from "utils/utils"; +import { connectToDatabase, setupEnv } from "utils/utils"; -import Archive from "models/archive"; +import messages from "composer/messages"; +import commands from "composer/commands"; +import edits from "composer/edits"; await connectToDatabase(); +await setupEnv(); -const conf = await readConfigFile(); -const bot = new Bot(conf.token); +const bot = new Bot(process.env.token!); -bot.command("start", (ctx) => { - ctx.reply("Welcome to my magical mailbox 🪄"); - ctx.reply(`My mailbox doese support: - 💬 Text - 🔉 Voice/Audio - 🎞 Video/Video note - 🎆 Sticker/Gif - 📁 Document - 🥰 Reactions`); -}); - -bot.on("message", async (ctx, next) => { - if (!ctx.msg.reply_to_message) { - return next(); - } - try { - const record: any = await Archive.findOne({ - where: { msgId: ctx.msg.reply_to_message.message_id }, - }); - const msg = await ctx.api.copyMessage( - record.senderId, - ctx.from.id, - ctx.msgId, - { - reply_parameters: { message_id: record.senderMsgId }, - } - ); - await Archive.create({ - msgId: msg.message_id, - senderId: ctx.from.id, - senderMsgId: ctx.msgId, - }); - } catch (err) { - return ctx.reply("Oops, something wrong 😢"); - } -}); - -bot.on("message", async (ctx) => { - try { - const msg = await ctx.api.copyMessage(conf.admin, ctx.from.id, ctx.msgId); - await Archive.create({ - msgId: msg.message_id, - senderId: ctx.from.id, - senderMsgId: ctx.msgId, - }); - } catch (err) { - return ctx.reply("Oops, something wrong 😢"); - } -}); - -bot.on("message_reaction", async (ctx) => { - try { - const record: any = await Archive.findOne({ - where: { msgId: ctx.msgId }, - }); - await ctx.api.setMessageReaction( - record.senderId, - record.senderMsgId, - ctx.messageReaction.new_reaction - ); - } catch (err) { - return ctx.reply("Oops, something wrong 😢"); - } -}); +bot.use(commands); +bot.use(messages); +bot.use(edits); bot.start({ - allowed_updates: ["message", "message_reaction", "message_reaction_count"], + allowed_updates: [ + "message", + "edited_message", + "message_reaction", + "message_reaction_count", + ], }); diff --git a/src/composer/commands.ts b/src/composer/commands.ts new file mode 100644 index 0000000..9a22f55 --- /dev/null +++ b/src/composer/commands.ts @@ -0,0 +1,18 @@ +import { Composer } from "grammy"; + +import Archive from "models/archive"; + +const commands = new Composer(); + +commands.command("start", (ctx) => { + ctx.reply("Welcome to my magical mailbox 🪄"); + ctx.reply(`My mailbox doese support: + 💬 Text + 🔉 Voice/Audio + 🎞 Video/Video note + 🎆 Sticker/Gif + 📁 Document + 🥰 Reactions`); +}); + +export default commands; diff --git a/src/composer/edits.ts b/src/composer/edits.ts new file mode 100644 index 0000000..93b6f56 --- /dev/null +++ b/src/composer/edits.ts @@ -0,0 +1,35 @@ +import { Composer } from "grammy"; + +import Archive from "models/archive"; + +const edits = new Composer(); + +edits.on("edit:text", async (ctx, next) => { + try { + const record: any = await Archive.findOne({ + where: { senderMsgId: ctx.msgId }, + }); + await ctx.api.editMessageText( + record.senderId, + record.msgId, + ctx.editedMessage?.text! + ); + } catch (err) { + return ctx.reply("Oops, something wrong 😢"); + } +}); + +edits.on("edit:caption", async (ctx, next) => { + try { + const record: any = await Archive.findOne({ + where: { senderMsgId: ctx.msgId }, + }); + await ctx.api.editMessageCaption(record.senderId, record.msgId, { + caption: ctx.editedMessage?.caption, + }); + } catch (err) { + return ctx.reply("Oops, something wrong 😢"); + } +}); + +export default edits; diff --git a/src/composer/messages.ts b/src/composer/messages.ts new file mode 100644 index 0000000..987f0a8 --- /dev/null +++ b/src/composer/messages.ts @@ -0,0 +1,62 @@ +import { Composer, InputMediaBuilder } from "grammy"; + +import Archive from "models/archive"; + +const messages = new Composer(); +const env = process.env; + +messages.on("message", async (ctx, next) => { + if (!ctx.msg.reply_to_message) { + return next(); + } + try { + const record: any = await Archive.findOne({ + where: { msgId: ctx.msg.reply_to_message.message_id }, + }); + const msg = await ctx.api.copyMessage( + record.senderId, + ctx.from.id, + ctx.msgId, + { + reply_parameters: { message_id: record.senderMsgId }, + } + ); + await Archive.create({ + msgId: msg.message_id, + senderId: ctx.from.id, + senderMsgId: ctx.msgId, + }); + } catch (err) { + return ctx.reply("Oops, something wrong 😢"); + } +}); + +messages.on("message", async (ctx) => { + try { + const msg = await ctx.api.copyMessage(env.admin!, ctx.from.id, ctx.msgId); + await Archive.create({ + msgId: msg.message_id, + senderId: ctx.from.id, + senderMsgId: ctx.msgId, + }); + } catch (err) { + return ctx.reply("Oops, something wrong 😢"); + } +}); + +messages.on("message_reaction", async (ctx) => { + try { + const record: any = await Archive.findOne({ + where: { msgId: ctx.msgId }, + }); + await ctx.api.setMessageReaction( + record.senderId, + record.senderMsgId, + ctx.messageReaction.new_reaction + ); + } catch (err) { + return ctx.reply("Oops, something wrong 😢"); + } +}); + +export default messages; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 9255687..9409259 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,4 +1,5 @@ -import fs from "fs/promises"; +import { config } from "dotenv"; + import { join } from "path"; import database from "database"; @@ -18,11 +19,9 @@ export async function connectToDatabase() { } } -export async function readConfigFile() { - const path = join(rootPath(), "config.json"); +export async function setupEnv() { try { - const buffer = await fs.readFile(path); - return JSON.parse(buffer.toString()); + await config({ path: join(rootPath(), "config.env") }); } catch (err) { console.log(err); }