Block and unblock senders feature
This commit is contained in:
parent
6863775cc6
commit
f3f1a0c96e
3 changed files with 89 additions and 1 deletions
|
@ -1,5 +1,8 @@
|
|||
import { Composer } from "grammy";
|
||||
|
||||
import Archive from "models/archive";
|
||||
import BlockList from "models/block-list";
|
||||
|
||||
const commands = new Composer();
|
||||
const env = process.env;
|
||||
|
||||
|
@ -14,4 +17,60 @@ commands.command("start", (ctx) => {
|
|||
🥰 Reactions`);
|
||||
});
|
||||
|
||||
commands.command("block", async (ctx) => {
|
||||
if (ctx.chatId != +env.admin!) {
|
||||
return ctx.reply("You are not owner");
|
||||
}
|
||||
if (!ctx.msg.reply_to_message) {
|
||||
return ctx.reply("Please reply on a message to block it sender");
|
||||
}
|
||||
try {
|
||||
const record: any = await Archive.findOne({
|
||||
where: { msgId: ctx.msg.reply_to_message.message_id },
|
||||
});
|
||||
if (record.senderId == +env.admin!) {
|
||||
return ctx.reply("Owner can't block itself");
|
||||
}
|
||||
const isSenderBlocked = await BlockList.findOne({
|
||||
where: { senderId: record.senderId },
|
||||
});
|
||||
if (!isSenderBlocked) {
|
||||
await BlockList.create({ senderId: record.senderId });
|
||||
return ctx.reply("The sender has been blocked");
|
||||
} else {
|
||||
return ctx.reply("The sender blocked before");
|
||||
}
|
||||
} catch (err) {
|
||||
return ctx.reply("Oops, something wrong 😢");
|
||||
}
|
||||
});
|
||||
|
||||
commands.command("unblock", async (ctx) => {
|
||||
if (ctx.chatId != +env.admin!) {
|
||||
return ctx.reply("You are not owner");
|
||||
}
|
||||
if (!ctx.msg.reply_to_message) {
|
||||
return ctx.reply("Please reply on a message to make unblock it sender");
|
||||
}
|
||||
try {
|
||||
const record: any = await Archive.findOne({
|
||||
where: { msgId: ctx.msg.reply_to_message.message_id },
|
||||
});
|
||||
if (record.senderId == +env.admin!) {
|
||||
return ctx.reply("Owner is free forever");
|
||||
}
|
||||
const isSenderBlocked = await BlockList.findOne({
|
||||
where: { senderId: record.senderId },
|
||||
});
|
||||
if (isSenderBlocked) {
|
||||
await BlockList.destroy({ where: { senderId: record.senderId } });
|
||||
return ctx.reply("The sender is a freebird from now");
|
||||
} else {
|
||||
return ctx.reply("The sender is already free");
|
||||
}
|
||||
} catch (err) {
|
||||
return ctx.reply("Oops, something wrong 😢");
|
||||
}
|
||||
});
|
||||
|
||||
export default commands;
|
||||
|
|
|
@ -1,10 +1,26 @@
|
|||
import { Composer, InputMediaBuilder } from "grammy";
|
||||
import { Composer } from "grammy";
|
||||
|
||||
import Archive from "models/archive";
|
||||
import BlockList from "models/block-list";
|
||||
|
||||
const messages = new Composer();
|
||||
const env = process.env;
|
||||
|
||||
messages.on("message", async (ctx, next) => {
|
||||
try {
|
||||
const isSenderBlocked = await BlockList.findOne({
|
||||
where: { senderId: ctx.chatId },
|
||||
});
|
||||
if (isSenderBlocked) {
|
||||
return ctx.reply("Sorry, You aren't allowed to send message 💔");
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
} catch (err) {
|
||||
return ctx.reply("Oops, something wrong 😢");
|
||||
}
|
||||
});
|
||||
|
||||
// Handle reply messages
|
||||
messages.on("message", async (ctx, next) => {
|
||||
if (!ctx.msg.reply_to_message) {
|
||||
|
|
13
src/models/block-list.ts
Normal file
13
src/models/block-list.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { DataTypes } from "sequelize";
|
||||
|
||||
import database from "database";
|
||||
|
||||
const BlockList = database.define("BlockList", {
|
||||
senderId: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
},
|
||||
});
|
||||
|
||||
export default BlockList;
|
Loading…
Add table
Add a link
Reference in a new issue