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 92d75d8..6ff76cc 100755 Binary files a/bun.lockb and b/bun.lockb differ 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); }