mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-09-01 14:45:27 +00:00
Added and used scheduleDeletion
...only where I was certain that whis was a good idea, perhaps it could be useful in more places.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const { link } = require('../../utils/tg');
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
||||||
const { logError } = require('../../utils/log');
|
const { logError } = require('../../utils/log');
|
||||||
|
|
||||||
// Bot
|
// Bot
|
||||||
@@ -24,7 +24,7 @@ const banHandler = async ({ chat, message, reply, telegram, me, state }) => {
|
|||||||
|
|
||||||
if (!userToBan) {
|
if (!userToBan) {
|
||||||
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
||||||
replyOptions);
|
replyOptions).then(scheduleDeletion);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.chat.type === 'private' || userToBan.username === me) {
|
if (message.chat.type === 'private' || userToBan.username === me) {
|
||||||
@@ -36,7 +36,8 @@ const banHandler = async ({ chat, message, reply, telegram, me, state }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (reason.length === 0) {
|
if (reason.length === 0) {
|
||||||
return reply('ℹ️ <b>Need a reason to ban.</b>', replyOptions);
|
return reply('ℹ️ <b>Need a reason to ban.</b>', replyOptions)
|
||||||
|
.then(scheduleDeletion);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.reply_to_message) {
|
if (message.reply_to_message) {
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const { link } = require('../../utils/tg');
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
||||||
|
|
||||||
// Bot
|
// Bot
|
||||||
const { replyOptions } = require('../../bot/options');
|
const { replyOptions } = require('../../bot/options');
|
||||||
@@ -20,7 +20,7 @@ const getWarnsHandler = async ({ message, reply, state }) => {
|
|||||||
: null;
|
: null;
|
||||||
if (!theUser) {
|
if (!theUser) {
|
||||||
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
||||||
replyOptions);
|
replyOptions).then(scheduleDeletion);
|
||||||
}
|
}
|
||||||
let i = 0;
|
let i = 0;
|
||||||
const warns = await getWarns(theUser);
|
const warns = await getWarns(theUser);
|
||||||
|
@@ -1,10 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const { escapeHtml } = require('../../utils/tg');
|
const { escapeHtml, scheduleDeletion } = require('../../utils/tg');
|
||||||
|
|
||||||
// Bot
|
|
||||||
const bot = require('../../bot');
|
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { listGroups } = require('../../stores/group');
|
const { listGroups } = require('../../stores/group');
|
||||||
@@ -19,7 +16,7 @@ const entry = group => group.username
|
|||||||
? `- @${group.username}`
|
? `- @${group.username}`
|
||||||
: `- <a href="${group.link}">${escapeHtml(group.title)}</a>`;
|
: `- <a href="${group.link}">${escapeHtml(group.title)}</a>`;
|
||||||
|
|
||||||
const groupsHandler = async ({ chat, replyWithHTML }) => {
|
const groupsHandler = async ({ replyWithHTML }) => {
|
||||||
if (config.groupsString) {
|
if (config.groupsString) {
|
||||||
return replyWithHTML(config.groupsString);
|
return replyWithHTML(config.groupsString);
|
||||||
}
|
}
|
||||||
@@ -28,15 +25,11 @@ const groupsHandler = async ({ chat, replyWithHTML }) => {
|
|||||||
|
|
||||||
const entries = groups.map(entry).join('\n');
|
const entries = groups.map(entry).join('\n');
|
||||||
|
|
||||||
const { message_id } = await replyWithHTML(
|
return replyWithHTML(
|
||||||
`🛠 <b>Groups I manage</b>:\n\n${entries}`, {
|
`🛠 <b>Groups I manage</b>:\n\n${entries}`, {
|
||||||
disable_web_page_preview: true,
|
disable_web_page_preview: true,
|
||||||
reply_markup,
|
reply_markup,
|
||||||
});
|
}).then(scheduleDeletion);
|
||||||
|
|
||||||
return setTimeout(() =>
|
|
||||||
bot.telegram.deleteMessage(chat.id, message_id), 5 * 60 * 1000);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = groupsHandler;
|
module.exports = groupsHandler;
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Bot
|
// Utils
|
||||||
const bot = require('../../bot');
|
const { scheduleDeletion } = require('../../utils/tg');
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { managesGroup } = require('../../stores/group');
|
const { managesGroup } = require('../../stores/group');
|
||||||
@@ -9,12 +9,10 @@ const { managesGroup } = require('../../stores/group');
|
|||||||
const linkHandler = async ({ chat, replyWithHTML }) => {
|
const linkHandler = async ({ chat, replyWithHTML }) => {
|
||||||
const group = await managesGroup({ id: chat.id });
|
const group = await managesGroup({ id: chat.id });
|
||||||
|
|
||||||
const { message_id } = await replyWithHTML(
|
return replyWithHTML(
|
||||||
'ℹ️ <b>Group\'s link:</b>\n\n' +
|
'ℹ️ <b>Group\'s link:</b>\n\n' +
|
||||||
`<a href="${group.link}">${group.title}</a>`
|
`<a href="${group.link}">${group.title}</a>`
|
||||||
);
|
).then(scheduleDeletion);
|
||||||
return setTimeout(() =>
|
|
||||||
bot.telegram.deleteMessage(chat.id, message_id), 5 * 60 * 1000);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = linkHandler;
|
module.exports = linkHandler;
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const { link } = require('../../utils/tg');
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
||||||
const { logError } = require('../../utils/log');
|
const { logError } = require('../../utils/log');
|
||||||
|
|
||||||
// Bot
|
// Bot
|
||||||
@@ -22,7 +22,7 @@ const nowarnsHandler = async ({ message, reply, state }) => {
|
|||||||
|
|
||||||
if (!userToUnwarn) {
|
if (!userToUnwarn) {
|
||||||
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
||||||
replyOptions);
|
replyOptions).then(scheduleDeletion);
|
||||||
}
|
}
|
||||||
|
|
||||||
const warns = await getWarns(userToUnwarn);
|
const warns = await getWarns(userToUnwarn);
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const { link } = require('../../utils/tg');
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
||||||
|
|
||||||
// Bot
|
// Bot
|
||||||
const { replyOptions } = require('../../bot/options');
|
const { replyOptions } = require('../../bot/options');
|
||||||
@@ -13,7 +13,7 @@ const reportHandler = async ctx => {
|
|||||||
const msg = ctx.message;
|
const msg = ctx.message;
|
||||||
if (!msg.reply_to_message) {
|
if (!msg.reply_to_message) {
|
||||||
return ctx.reply('ℹ️ <b>Reply to message you\'d like to report</b>',
|
return ctx.reply('ℹ️ <b>Reply to message you\'d like to report</b>',
|
||||||
replyOptions);
|
replyOptions).then(scheduleDeletion);
|
||||||
}
|
}
|
||||||
const admins = await getAdmins();
|
const admins = await getAdmins();
|
||||||
const adminObjects = admins.map(user => ({
|
const adminObjects = admins.map(user => ({
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const { quietLink } = require('../../utils/tg');
|
const { quietLink, scheduleDeletion } = require('../../utils/tg');
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { getAdmins } = require('../../stores/user');
|
const { getAdmins } = require('../../stores/user');
|
||||||
@@ -16,7 +16,7 @@ const staffHandler = async ctx => {
|
|||||||
return ctx.replyWithHTML(`<b>Admins in the network:</b>\n\n${list}`, {
|
return ctx.replyWithHTML(`<b>Admins in the network:</b>\n\n${list}`, {
|
||||||
disable_notification: true,
|
disable_notification: true,
|
||||||
disable_web_page_preview: true,
|
disable_web_page_preview: true,
|
||||||
});
|
}).then(scheduleDeletion);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = staffHandler;
|
module.exports = staffHandler;
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const { link } = require('../../utils/tg');
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
||||||
const { logError } = require('../../utils/log');
|
const { logError } = require('../../utils/log');
|
||||||
|
|
||||||
// Bot
|
// Bot
|
||||||
@@ -22,7 +22,7 @@ const unAdminHandler = async ({ message, reply, state }) => {
|
|||||||
|
|
||||||
if (!userToUnadmin) {
|
if (!userToUnadmin) {
|
||||||
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
||||||
replyOptions);
|
replyOptions).then(scheduleDeletion);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!await isAdmin(userToUnadmin)) {
|
if (!await isAdmin(userToUnadmin)) {
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const { link } = require('../../utils/tg');
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
||||||
const { logError } = require('../../utils/log');
|
const { logError } = require('../../utils/log');
|
||||||
|
|
||||||
// Bot
|
// Bot
|
||||||
@@ -25,7 +25,7 @@ const unbanHandler = async ({ message, reply, telegram, state }) => {
|
|||||||
|
|
||||||
if (!userToUnban) {
|
if (!userToUnban) {
|
||||||
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
||||||
replyOptions);
|
replyOptions).then(scheduleDeletion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const { link } = require('../../utils/tg');
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
||||||
|
|
||||||
// Bot
|
// Bot
|
||||||
const { replyOptions } = require('../../bot/options');
|
const { replyOptions } = require('../../bot/options');
|
||||||
@@ -22,7 +22,7 @@ const unwarnHandler = async ({ message, reply, state, telegram }) => {
|
|||||||
|
|
||||||
if (!userToUnwarn) {
|
if (!userToUnwarn) {
|
||||||
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
return reply('ℹ️ <b>Reply to a message or mention a user.</b>',
|
||||||
replyOptions);
|
replyOptions).then(scheduleDeletion);
|
||||||
}
|
}
|
||||||
|
|
||||||
const allWarns = await getWarns(userToUnwarn);
|
const allWarns = await getWarns(userToUnwarn);
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const { link } = require('../../utils/tg');
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
||||||
const { logError } = require('../../utils/log');
|
const { logError } = require('../../utils/log');
|
||||||
|
|
||||||
// Config
|
// Config
|
||||||
@@ -26,7 +26,7 @@ const warnHandler = async ({ message, chat, reply, me, state }) => {
|
|||||||
|
|
||||||
if (!userToWarn) {
|
if (!userToWarn) {
|
||||||
return reply('ℹ️ <b>Reply to a message or mentoin a user.</b>',
|
return reply('ℹ️ <b>Reply to a message or mentoin a user.</b>',
|
||||||
replyOptions);
|
replyOptions).then(scheduleDeletion);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.chat.type === 'private' || userToWarn.username === me) {
|
if (message.chat.type === 'private' || userToWarn.username === me) {
|
||||||
@@ -40,7 +40,8 @@ const warnHandler = async ({ message, chat, reply, me, state }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (reason.length === 0) {
|
if (reason.length === 0) {
|
||||||
return reply('ℹ️ <b>Need a reason to warn.</b>', replyOptions);
|
return reply('ℹ️ <b>Need a reason to warn.</b>', replyOptions)
|
||||||
|
.then(scheduleDeletion);
|
||||||
}
|
}
|
||||||
|
|
||||||
await warn(userToWarn, reason);
|
await warn(userToWarn, reason);
|
||||||
|
15
utils/tg.js
15
utils/tg.js
@@ -1,5 +1,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { telegram } = require('../bot');
|
||||||
|
|
||||||
|
const { promisify } = require('util');
|
||||||
|
|
||||||
|
const delay = promisify(setTimeout);
|
||||||
|
|
||||||
const escapeHtml = s => s
|
const escapeHtml = s => s
|
||||||
.replace(/</g, '<');
|
.replace(/</g, '<');
|
||||||
|
|
||||||
@@ -21,9 +27,18 @@ const deleteAfter = ms => ctx =>
|
|||||||
ctx.telegram.deleteMessage(ctx.chat.id, ctx.message.message_id),
|
ctx.telegram.deleteMessage(ctx.chat.id, ctx.message.message_id),
|
||||||
ms);
|
ms);
|
||||||
|
|
||||||
|
const scheduleDeletion = async ({ chat, message_id }) => {
|
||||||
|
if (chat.type === 'private') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
await delay(5 * 60 * 1000);
|
||||||
|
return telegram.deleteMessage(chat.id, message_id);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
deleteAfter,
|
deleteAfter,
|
||||||
escapeHtml,
|
escapeHtml,
|
||||||
link,
|
link,
|
||||||
quietLink,
|
quietLink,
|
||||||
|
scheduleDeletion,
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user