From 96e6a3c76172678065859f3f1e427171de3d2f7c Mon Sep 17 00:00:00 2001 From: Martin Rys Date: Fri, 7 May 2021 22:46:11 +0200 Subject: [PATCH] Upgrade to Telegraf 4.3 --- handlers/commands/admin.js | 18 +++++++-------- handlers/commands/groups.js | 4 ++-- handlers/commands/help.js | 6 ++--- handlers/commands/link.js | 8 +++---- handlers/commands/removeCommand.js | 20 ++++++++--------- handlers/commands/unadmin.js | 14 ++++++------ handlers/commands/user.js | 22 +++++++++---------- handlers/middlewares/commandButtons.js | 2 +- handlers/middlewares/removeChannelForwards.js | 3 +-- handlers/regex/groupLinker.js | 2 +- handlers/regex/index.js | 2 +- handlers/regex/runCustomCmd.js | 8 +++++-- package.json | 2 +- plugins/index.js | 2 +- utils/tg.js | 2 +- 15 files changed, 59 insertions(+), 56 deletions(-) diff --git a/handlers/commands/admin.js b/handlers/commands/admin.js index 32b48709..6a4c755f 100644 --- a/handlers/commands/admin.js +++ b/handlers/commands/admin.js @@ -13,41 +13,41 @@ const { } = require('../../stores/user'); /** @param { import('../../typings/context').ExtendedContext } ctx */ -const adminHandler = async ({ from, message, replyWithHTML }) => { - if (!isMaster(from)) return null; +const adminHandler = async (ctx) => { + if (!isMaster(ctx.from)) return null; - const { targets } = parse(message); + const { targets } = parse(ctx.message); if (targets.length > 1) { - return replyWithHTML( + return ctx.replyWithHTML( 'ℹ️ Specify one user to promote.', ).then(scheduleDeletion()); } const userToAdmin = targets.length ? await getUser(strip(targets[0])) - : from; + : ctx.from; if (!userToAdmin) { - return replyWithHTML( + return ctx.replyWithHTML( '❓ User unknown.\n' + 'Please forward their message, then try again.', ).then(scheduleDeletion()); } if (userToAdmin.status === 'banned') { - return replyWithHTML('ℹ️ Can\'t admin banned user.'); + return ctx.replyWithHTML('ℹ️ Can\'t admin banned user.'); } if (userToAdmin.status === 'admin') { - return replyWithHTML( + return ctx.replyWithHTML( html`⭐️ ${link(userToAdmin)} is already admin.`, ); } await admin(userToAdmin); - return replyWithHTML(html`⭐️ ${link(userToAdmin)} is now admin.`); + return ctx.replyWithHTML(html`⭐️ ${link(userToAdmin)} is now admin.`); }; module.exports = adminHandler; diff --git a/handlers/commands/groups.js b/handlers/commands/groups.js index 5674d64b..96d52e8c 100644 --- a/handlers/commands/groups.js +++ b/handlers/commands/groups.js @@ -30,7 +30,7 @@ const emojiRegex = XRegExp.tag('gx')` const stripEmoji = s => s.replace(emojiRegex, ''); /** @param { import('../../typings/context').ExtendedContext } ctx */ -const groupsHandler = async ({ replyWithHTML }) => { +const groupsHandler = async (ctx) => { const groups = await listVisibleGroups(); groups.sort((a, b) => @@ -38,7 +38,7 @@ const groupsHandler = async ({ replyWithHTML }) => { const entries = TgHtml.join('\n', groups.map(entry)); - return replyWithHTML(TgHtml.tag`🛠 Groups I manage:\n\n${entries}`, { + return ctx.replyWithHTML(TgHtml.tag`🛠 Groups I manage:\n\n${entries}`, { disable_web_page_preview: true, reply_markup, }).then(scheduleDeletion()); diff --git a/handlers/commands/help.js b/handlers/commands/help.js index 1da8d477..dae33c58 100644 --- a/handlers/commands/help.js +++ b/handlers/commands/help.js @@ -19,10 +19,10 @@ might be a better choice for you. `; /** @param { import('../../typings/context').ExtendedContext } ctx */ -const helpHandler = ({ chat, replyWithHTML }) => { - if (chat.type !== 'private') return null; +const helpHandler = (ctx) => { + if (ctx.chat.type !== 'private') return null; - return replyWithHTML( + return ctx.replyWithHTML( message, Markup.inlineKeyboard([ Markup.urlButton('🛠 Setup a New Bot', homepage) diff --git a/handlers/commands/link.js b/handlers/commands/link.js index 3de005d4..dc9d1d82 100644 --- a/handlers/commands/link.js +++ b/handlers/commands/link.js @@ -7,14 +7,14 @@ const { scheduleDeletion } = require('../../utils/tg'); const { managesGroup } = require('../../stores/group'); /** @param { import('../../typings/context').ExtendedContext } ctx */ -const linkHandler = async ({ chat, replyWithHTML }, next) => { - if (chat.type === 'private') { +const linkHandler = async (ctx, next) => { + if (ctx.chat.type === 'private') { return next(); } - const group = await managesGroup({ id: chat.id }); + const group = await managesGroup({ id: ctx.chat.id }); - return replyWithHTML(group.link || '️ℹ️ No link to this group', { + return ctx.replyWithHTML(group.link || '️ℹ️ No link to this group', { disable_web_page_preview: false, }).then(scheduleDeletion()); }; diff --git a/handlers/commands/removeCommand.js b/handlers/commands/removeCommand.js index 29a88b54..64bfab7c 100644 --- a/handlers/commands/removeCommand.js +++ b/handlers/commands/removeCommand.js @@ -6,18 +6,18 @@ const { getCommand, removeCommand } = require('../../stores/command'); const { isMaster } = require('../../utils/config'); /** @param { import('../../typings/context').ExtendedContext } ctx */ -const removeCommandHandler = async ({ from, chat, message, replyWithHTML }) => { - const { text } = message; - if (chat.type !== 'private') return null; +const removeCommandHandler = async (ctx) => { + const { text } = ctx.message; + if (ctx.chat.type !== 'private') return null; - if (from.status !== 'admin') { - return replyWithHTML( + if (ctx.from.status !== 'admin') { + return ctx.replyWithHTML( 'ℹ️ Sorry, only admins access this command.', ); } const [ , commandName ] = text.split(' '); if (!commandName) { - return replyWithHTML( + return ctx.replyWithHTML( 'Send a valid command.\n\nExample:\n' + '/removecommand rules', ); @@ -25,20 +25,20 @@ const removeCommandHandler = async ({ from, chat, message, replyWithHTML }) => { const command = await getCommand({ name: commandName.toLowerCase() }); if (!command) { - return replyWithHTML( + return ctx.replyWithHTML( 'ℹ️ Command couldn\'t be found.', ); } const role = command.role.toLowerCase(); - if (role === 'master' && !isMaster(from)) { - return replyWithHTML( + if (role === 'master' && !isMaster(ctx.from)) { + return ctx.replyWithHTML( 'ℹ️ Sorry, only master can remove this command.', ); } await removeCommand({ name: commandName.toLowerCase() }); - return replyWithHTML( + return ctx.replyWithHTML( `✅ !${commandName} ` + 'has been removed successfully.', ); diff --git a/handlers/commands/unadmin.js b/handlers/commands/unadmin.js index f7fa2985..9bd9f392 100644 --- a/handlers/commands/unadmin.js +++ b/handlers/commands/unadmin.js @@ -29,13 +29,13 @@ const tgUnadmin = async (userToUnadmin) => { }; /** @param { import('../../typings/context').ExtendedContext } ctx */ -const unAdminHandler = async ({ from, message, replyWithHTML }) => { - if (!isMaster(from)) return null; +const unAdminHandler = async (ctx) => { + if (!isMaster(ctx.from)) return null; - const { targets } = parse(message); + const { targets } = parse(ctx.message); if (targets.length !== 1) { - return replyWithHTML( + return ctx.replyWithHTML( 'ℹ️ Specify one user to unadmin.', ).then(scheduleDeletion()); } @@ -43,13 +43,13 @@ const unAdminHandler = async ({ from, message, replyWithHTML }) => { const userToUnadmin = await getUser(strip(targets[0])); if (!userToUnadmin) { - return replyWithHTML( + return ctx.replyWithHTML( '❓ User unknown.', ).then(scheduleDeletion()); } if (userToUnadmin.status !== 'admin') { - return replyWithHTML( + return ctx.replyWithHTML( html`ℹ️ ${link(userToUnadmin)} is not admin.`, ); } @@ -58,7 +58,7 @@ const unAdminHandler = async ({ from, message, replyWithHTML }) => { await unadmin(userToUnadmin); - return replyWithHTML( + return ctx.replyWithHTML( html`❗️ ${link(userToUnadmin)} is no longer admin.`, ); }; diff --git a/handlers/commands/user.js b/handlers/commands/user.js index 9bb6ca8c..4f1766f5 100644 --- a/handlers/commands/user.js +++ b/handlers/commands/user.js @@ -50,33 +50,33 @@ const title = user => { }; /** @param { import('../../typings/context').ExtendedContext } ctx */ -const getWarnsHandler = async ({ from, message, replyWithHTML }) => { - if (!from) { - return replyWithHTML( +const getWarnsHandler = async (ctx) => { + if (!ctx.from) { + return ctx.replyWithHTML( 'ℹ️ This command is not available in channels.', ).then(scheduleDeletion()); } - const { flags, targets } = parse(message); + const { flags, targets } = parse(ctx.message); if (targets.length > 1) { - return replyWithHTML( + return ctx.replyWithHTML( 'ℹ️ Specify one user.', ).then(scheduleDeletion()); } - const theUser = targets.length && from.status === 'admin' + const theUser = targets.length && ctx.from.status === 'admin' ? await getUser(strip(targets[0])) - : from; + : ctx.from; if (!theUser) { - return replyWithHTML( + return ctx.replyWithHTML( '❓ User unknown.', ).then(scheduleDeletion()); } - if (flags.has('raw') && from.status === 'admin') { - return replyWithHTML( + if (flags.has('raw') && ctx.from.status === 'admin') { + return ctx.replyWithHTML( TgHtml.pre(inspect(theUser)), ).then(scheduleDeletion()); } @@ -111,7 +111,7 @@ const getWarnsHandler = async ({ from, message, replyWithHTML }) => { permitS, ].filter(isNotEmpty)); - return replyWithHTML(TgHtml.join('\n\n', [ + return ctx.replyWithHTML(TgHtml.join('\n\n', [ oneliners, userWarns, banReason, diff --git a/handlers/middlewares/commandButtons.js b/handlers/middlewares/commandButtons.js index bd03141c..84e00abb 100644 --- a/handlers/middlewares/commandButtons.js +++ b/handlers/middlewares/commandButtons.js @@ -24,7 +24,7 @@ module.exports = (ctx, next) => { Object.assign(cbCtx, contextCustomizations); cbCtx.botInfo = ctx.botInfo; - cbCtx.reply = ctx.editMessageText; + cbCtx.reply = ctx.editMessageText.bind(ctx); return next(cbCtx); }; diff --git a/handlers/middlewares/removeChannelForwards.js b/handlers/middlewares/removeChannelForwards.js index 75e2e2c0..d39166d6 100644 --- a/handlers/middlewares/removeChannelForwards.js +++ b/handlers/middlewares/removeChannelForwards.js @@ -1,8 +1,7 @@ 'use strict'; const R = require('ramda'); -const { optional, passThru } = require('telegraf'); - +const { Telegraf: { optional, passThru } } = require('telegraf'); const { permit } = require('../../stores/user'); const { html, lrm } = require('../../utils/html'); diff --git a/handlers/regex/groupLinker.js b/handlers/regex/groupLinker.js index 8128f1e9..891a46bb 100644 --- a/handlers/regex/groupLinker.js +++ b/handlers/regex/groupLinker.js @@ -1,6 +1,6 @@ 'use strict'; -const { hears } = require('telegraf'); +const { Telegraf: { hears } } = require('telegraf'); const XRegExp = require('xregexp'); const { managesGroup } = require.main.require('./stores/group'); diff --git a/handlers/regex/index.js b/handlers/regex/index.js index 4b813337..478cde38 100644 --- a/handlers/regex/index.js +++ b/handlers/regex/index.js @@ -1,6 +1,6 @@ 'use strict'; -const { compose, hears } = require('telegraf'); +const { Telegraf: { compose, hears } } = require('telegraf'); /* eslint-disable global-require */ diff --git a/handlers/regex/runCustomCmd.js b/handlers/regex/runCustomCmd.js index 75db0e20..c3313af9 100644 --- a/handlers/regex/runCustomCmd.js +++ b/handlers/regex/runCustomCmd.js @@ -1,6 +1,6 @@ 'use strict'; -const { hears } = require('telegraf'); +const { Telegraf: { hears } } = require('telegraf'); const R = require('ramda'); // DB @@ -63,7 +63,11 @@ const runCustomCmdHandler = async (ctx, next) => { }; return ctx[typeToMethod(type)](content, options) - .then(scheduleDeletion(autoDelete(command) && deleteCustom.after)); + .then(({ message_id }) => + scheduleDeletion( + autoDelete(command) && deleteCustom.after)({ + chat: ctx.chat, message_id + })); }; module.exports = hears(/^! ?(\w+)/, runCustomCmdHandler); diff --git a/package.json b/package.json index c99a9669..6e41993e 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "require-directory": "^2.1.1", "spamwatch": "^0.2.0", "string-replace-async": "^1.2.1", - "telegraf": "^3.38.0", + "telegraf": "^4.3.0", "ts-node": "^8.9.1", "typescript": "^3.8.3", "xregexp": "^4.2.0" diff --git a/plugins/index.js b/plugins/index.js index 0ad70abe..12e3e873 100644 --- a/plugins/index.js +++ b/plugins/index.js @@ -1,6 +1,6 @@ 'use strict'; -const { compose } = require('telegraf'); +const { Telegraf: { compose } } = require('telegraf'); const { config } = require('../utils/config'); const names = config.plugins || []; diff --git a/utils/tg.js b/utils/tg.js index d3728cd3..51b93690 100644 --- a/utils/tg.js +++ b/utils/tg.js @@ -33,7 +33,7 @@ const displayUser = user => /** @param {number | string | false} ms */ const deleteAfter = ms => (ctx, next) => { if (ms !== false) { - setTimeout(ctx.deleteMessage, millisecond(ms)); + setTimeout(ctx.deleteMessage.bind(ctx), millisecond(ms)); } next(); };