diff --git a/handlers/commands/ban.js b/handlers/commands/ban.js index 8040ba7..12a01e0 100644 --- a/handlers/commands/ban.js +++ b/handlers/commands/ban.js @@ -1,7 +1,7 @@ 'use strict'; // Utils -const { link } = require('../../utils/tg'); +const { link, scheduleDeletion } = require('../../utils/tg'); const { logError } = require('../../utils/log'); // Bot @@ -24,7 +24,7 @@ const banHandler = async ({ chat, message, reply, telegram, me, state }) => { if (!userToBan) { return reply('ℹ️ Reply to a message or mention a user.', - replyOptions); + replyOptions).then(scheduleDeletion); } 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) { - return reply('ℹ️ Need a reason to ban.', replyOptions); + return reply('ℹ️ Need a reason to ban.', replyOptions) + .then(scheduleDeletion); } if (message.reply_to_message) { diff --git a/handlers/commands/getwarns.js b/handlers/commands/getwarns.js index df8c3e0..8326a87 100644 --- a/handlers/commands/getwarns.js +++ b/handlers/commands/getwarns.js @@ -1,7 +1,7 @@ 'use strict'; // Utils -const { link } = require('../../utils/tg'); +const { link, scheduleDeletion } = require('../../utils/tg'); // Bot const { replyOptions } = require('../../bot/options'); @@ -20,7 +20,7 @@ const getWarnsHandler = async ({ message, reply, state }) => { : null; if (!theUser) { return reply('ℹ️ Reply to a message or mention a user.', - replyOptions); + replyOptions).then(scheduleDeletion); } let i = 0; const warns = await getWarns(theUser); diff --git a/handlers/commands/groups.js b/handlers/commands/groups.js index 55b096b..6c6b782 100644 --- a/handlers/commands/groups.js +++ b/handlers/commands/groups.js @@ -1,10 +1,7 @@ 'use strict'; // Utils -const { escapeHtml } = require('../../utils/tg'); - -// Bot -const bot = require('../../bot'); +const { escapeHtml, scheduleDeletion } = require('../../utils/tg'); // DB const { listGroups } = require('../../stores/group'); @@ -19,7 +16,7 @@ const entry = group => group.username ? `- @${group.username}` : `- ${escapeHtml(group.title)}`; -const groupsHandler = async ({ chat, replyWithHTML }) => { +const groupsHandler = async ({ replyWithHTML }) => { if (config.groupsString) { return replyWithHTML(config.groupsString); } @@ -28,15 +25,11 @@ const groupsHandler = async ({ chat, replyWithHTML }) => { const entries = groups.map(entry).join('\n'); - const { message_id } = await replyWithHTML( + return replyWithHTML( `🛠 Groups I manage:\n\n${entries}`, { disable_web_page_preview: true, reply_markup, - }); - - return setTimeout(() => - bot.telegram.deleteMessage(chat.id, message_id), 5 * 60 * 1000); - + }).then(scheduleDeletion); }; module.exports = groupsHandler; diff --git a/handlers/commands/link.js b/handlers/commands/link.js index 9310eec..7099651 100644 --- a/handlers/commands/link.js +++ b/handlers/commands/link.js @@ -1,7 +1,7 @@ 'use strict'; -// Bot -const bot = require('../../bot'); +// Utils +const { scheduleDeletion } = require('../../utils/tg'); // DB const { managesGroup } = require('../../stores/group'); @@ -9,12 +9,10 @@ const { managesGroup } = require('../../stores/group'); const linkHandler = async ({ chat, replyWithHTML }) => { const group = await managesGroup({ id: chat.id }); - const { message_id } = await replyWithHTML( + return replyWithHTML( 'ℹ️ Group\'s link:\n\n' + `${group.title}` - ); - return setTimeout(() => - bot.telegram.deleteMessage(chat.id, message_id), 5 * 60 * 1000); + ).then(scheduleDeletion); }; module.exports = linkHandler; diff --git a/handlers/commands/nowarns.js b/handlers/commands/nowarns.js index 3a533f2..2845fe2 100644 --- a/handlers/commands/nowarns.js +++ b/handlers/commands/nowarns.js @@ -1,7 +1,7 @@ 'use strict'; // Utils -const { link } = require('../../utils/tg'); +const { link, scheduleDeletion } = require('../../utils/tg'); const { logError } = require('../../utils/log'); // Bot @@ -22,7 +22,7 @@ const nowarnsHandler = async ({ message, reply, state }) => { if (!userToUnwarn) { return reply('ℹ️ Reply to a message or mention a user.', - replyOptions); + replyOptions).then(scheduleDeletion); } const warns = await getWarns(userToUnwarn); diff --git a/handlers/commands/report.js b/handlers/commands/report.js index 2ce087d..ce93db3 100644 --- a/handlers/commands/report.js +++ b/handlers/commands/report.js @@ -1,7 +1,7 @@ 'use strict'; // Utils -const { link } = require('../../utils/tg'); +const { link, scheduleDeletion } = require('../../utils/tg'); // Bot const { replyOptions } = require('../../bot/options'); @@ -13,7 +13,7 @@ const reportHandler = async ctx => { const msg = ctx.message; if (!msg.reply_to_message) { return ctx.reply('ℹ️ Reply to message you\'d like to report', - replyOptions); + replyOptions).then(scheduleDeletion); } const admins = await getAdmins(); const adminObjects = admins.map(user => ({ diff --git a/handlers/commands/staff.js b/handlers/commands/staff.js index cce791e..fe4a452 100644 --- a/handlers/commands/staff.js +++ b/handlers/commands/staff.js @@ -1,7 +1,7 @@ 'use strict'; // Utils -const { quietLink } = require('../../utils/tg'); +const { quietLink, scheduleDeletion } = require('../../utils/tg'); // DB const { getAdmins } = require('../../stores/user'); @@ -16,7 +16,7 @@ const staffHandler = async ctx => { return ctx.replyWithHTML(`Admins in the network:\n\n${list}`, { disable_notification: true, disable_web_page_preview: true, - }); + }).then(scheduleDeletion); }; module.exports = staffHandler; diff --git a/handlers/commands/unadmin.js b/handlers/commands/unadmin.js index 91363a0..ee7a007 100644 --- a/handlers/commands/unadmin.js +++ b/handlers/commands/unadmin.js @@ -1,7 +1,7 @@ 'use strict'; // Utils -const { link } = require('../../utils/tg'); +const { link, scheduleDeletion } = require('../../utils/tg'); const { logError } = require('../../utils/log'); // Bot @@ -22,7 +22,7 @@ const unAdminHandler = async ({ message, reply, state }) => { if (!userToUnadmin) { return reply('ℹ️ Reply to a message or mention a user.', - replyOptions); + replyOptions).then(scheduleDeletion); } if (!await isAdmin(userToUnadmin)) { diff --git a/handlers/commands/unban.js b/handlers/commands/unban.js index 8554a36..194ecc0 100644 --- a/handlers/commands/unban.js +++ b/handlers/commands/unban.js @@ -1,7 +1,7 @@ 'use strict'; // Utils -const { link } = require('../../utils/tg'); +const { link, scheduleDeletion } = require('../../utils/tg'); const { logError } = require('../../utils/log'); // Bot @@ -25,7 +25,7 @@ const unbanHandler = async ({ message, reply, telegram, state }) => { if (!userToUnban) { return reply('ℹ️ Reply to a message or mention a user.', - replyOptions); + replyOptions).then(scheduleDeletion); } diff --git a/handlers/commands/unwarn.js b/handlers/commands/unwarn.js index eb4f8b9..70b1c8e 100644 --- a/handlers/commands/unwarn.js +++ b/handlers/commands/unwarn.js @@ -1,7 +1,7 @@ 'use strict'; // Utils -const { link } = require('../../utils/tg'); +const { link, scheduleDeletion } = require('../../utils/tg'); // Bot const { replyOptions } = require('../../bot/options'); @@ -22,7 +22,7 @@ const unwarnHandler = async ({ message, reply, state, telegram }) => { if (!userToUnwarn) { return reply('ℹ️ Reply to a message or mention a user.', - replyOptions); + replyOptions).then(scheduleDeletion); } const allWarns = await getWarns(userToUnwarn); diff --git a/handlers/commands/warn.js b/handlers/commands/warn.js index 455473c..d362f34 100644 --- a/handlers/commands/warn.js +++ b/handlers/commands/warn.js @@ -1,7 +1,7 @@ 'use strict'; // Utils -const { link } = require('../../utils/tg'); +const { link, scheduleDeletion } = require('../../utils/tg'); const { logError } = require('../../utils/log'); // Config @@ -26,7 +26,7 @@ const warnHandler = async ({ message, chat, reply, me, state }) => { if (!userToWarn) { return reply('ℹ️ Reply to a message or mentoin a user.', - replyOptions); + replyOptions).then(scheduleDeletion); } if (message.chat.type === 'private' || userToWarn.username === me) { @@ -40,7 +40,8 @@ const warnHandler = async ({ message, chat, reply, me, state }) => { } if (reason.length === 0) { - return reply('ℹ️ Need a reason to warn.', replyOptions); + return reply('ℹ️ Need a reason to warn.', replyOptions) + .then(scheduleDeletion); } await warn(userToWarn, reason); diff --git a/utils/tg.js b/utils/tg.js index 719c02d..11346a3 100644 --- a/utils/tg.js +++ b/utils/tg.js @@ -1,5 +1,11 @@ 'use strict'; +const { telegram } = require('../bot'); + +const { promisify } = require('util'); + +const delay = promisify(setTimeout); + const escapeHtml = s => s .replace(/ ctx => ctx.telegram.deleteMessage(ctx.chat.id, ctx.message.message_id), 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 = { deleteAfter, escapeHtml, link, quietLink, + scheduleDeletion, };