2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-09-01 14:45:27 +00:00

(merge) #137 from C0rn3j: Upgrade to Telegraf 4.3

This commit is contained in:
Muthu Kumar
2021-05-08 02:31:52 +05:30
committed by GitHub
15 changed files with 59 additions and 56 deletions

View File

@@ -13,41 +13,41 @@ const {
} = require('../../stores/user'); } = require('../../stores/user');
/** @param { import('../../typings/context').ExtendedContext } ctx */ /** @param { import('../../typings/context').ExtendedContext } ctx */
const adminHandler = async ({ from, message, replyWithHTML }) => { const adminHandler = async (ctx) => {
if (!isMaster(from)) return null; if (!isMaster(ctx.from)) return null;
const { targets } = parse(message); const { targets } = parse(ctx.message);
if (targets.length > 1) { if (targets.length > 1) {
return replyWithHTML( return ctx.replyWithHTML(
' <b>Specify one user to promote.</b>', ' <b>Specify one user to promote.</b>',
).then(scheduleDeletion()); ).then(scheduleDeletion());
} }
const userToAdmin = targets.length const userToAdmin = targets.length
? await getUser(strip(targets[0])) ? await getUser(strip(targets[0]))
: from; : ctx.from;
if (!userToAdmin) { if (!userToAdmin) {
return replyWithHTML( return ctx.replyWithHTML(
'❓ <b>User unknown.</b>\n' + '❓ <b>User unknown.</b>\n' +
'Please forward their message, then try again.', 'Please forward their message, then try again.',
).then(scheduleDeletion()); ).then(scheduleDeletion());
} }
if (userToAdmin.status === 'banned') { if (userToAdmin.status === 'banned') {
return replyWithHTML(' <b>Can\'t admin banned user.</b>'); return ctx.replyWithHTML(' <b>Can\'t admin banned user.</b>');
} }
if (userToAdmin.status === 'admin') { if (userToAdmin.status === 'admin') {
return replyWithHTML( return ctx.replyWithHTML(
html`⭐️ ${link(userToAdmin)} <b>is already admin.</b>`, html`⭐️ ${link(userToAdmin)} <b>is already admin.</b>`,
); );
} }
await admin(userToAdmin); await admin(userToAdmin);
return replyWithHTML(html`⭐️ ${link(userToAdmin)} <b>is now admin.</b>`); return ctx.replyWithHTML(html`⭐️ ${link(userToAdmin)} <b>is now admin.</b>`);
}; };
module.exports = adminHandler; module.exports = adminHandler;

View File

@@ -30,7 +30,7 @@ const emojiRegex = XRegExp.tag('gx')`
const stripEmoji = s => s.replace(emojiRegex, ''); const stripEmoji = s => s.replace(emojiRegex, '');
/** @param { import('../../typings/context').ExtendedContext } ctx */ /** @param { import('../../typings/context').ExtendedContext } ctx */
const groupsHandler = async ({ replyWithHTML }) => { const groupsHandler = async (ctx) => {
const groups = await listVisibleGroups(); const groups = await listVisibleGroups();
groups.sort((a, b) => groups.sort((a, b) =>
@@ -38,7 +38,7 @@ const groupsHandler = async ({ replyWithHTML }) => {
const entries = TgHtml.join('\n', groups.map(entry)); const entries = TgHtml.join('\n', groups.map(entry));
return replyWithHTML(TgHtml.tag`🛠 <b>Groups I manage</b>:\n\n${entries}`, { return ctx.replyWithHTML(TgHtml.tag`🛠 <b>Groups I manage</b>:\n\n${entries}`, {
disable_web_page_preview: true, disable_web_page_preview: true,
reply_markup, reply_markup,
}).then(scheduleDeletion()); }).then(scheduleDeletion());

View File

@@ -19,10 +19,10 @@ might be a better choice for you.
`; `;
/** @param { import('../../typings/context').ExtendedContext } ctx */ /** @param { import('../../typings/context').ExtendedContext } ctx */
const helpHandler = ({ chat, replyWithHTML }) => { const helpHandler = (ctx) => {
if (chat.type !== 'private') return null; if (ctx.chat.type !== 'private') return null;
return replyWithHTML( return ctx.replyWithHTML(
message, message,
Markup.inlineKeyboard([ Markup.inlineKeyboard([
Markup.urlButton('🛠 Setup a New Bot', homepage) Markup.urlButton('🛠 Setup a New Bot', homepage)

View File

@@ -7,14 +7,14 @@ const { scheduleDeletion } = require('../../utils/tg');
const { managesGroup } = require('../../stores/group'); const { managesGroup } = require('../../stores/group');
/** @param { import('../../typings/context').ExtendedContext } ctx */ /** @param { import('../../typings/context').ExtendedContext } ctx */
const linkHandler = async ({ chat, replyWithHTML }, next) => { const linkHandler = async (ctx, next) => {
if (chat.type === 'private') { if (ctx.chat.type === 'private') {
return next(); return next();
} }
const group = await managesGroup({ id: chat.id }); const group = await managesGroup({ id: ctx.chat.id });
return replyWithHTML(group.link || ' <b>No link to this group</b>', { return ctx.replyWithHTML(group.link || ' <b>No link to this group</b>', {
disable_web_page_preview: false, disable_web_page_preview: false,
}).then(scheduleDeletion()); }).then(scheduleDeletion());
}; };

View File

@@ -6,18 +6,18 @@ const { getCommand, removeCommand } = require('../../stores/command');
const { isMaster } = require('../../utils/config'); const { isMaster } = require('../../utils/config');
/** @param { import('../../typings/context').ExtendedContext } ctx */ /** @param { import('../../typings/context').ExtendedContext } ctx */
const removeCommandHandler = async ({ from, chat, message, replyWithHTML }) => { const removeCommandHandler = async (ctx) => {
const { text } = message; const { text } = ctx.message;
if (chat.type !== 'private') return null; if (ctx.chat.type !== 'private') return null;
if (from.status !== 'admin') { if (ctx.from.status !== 'admin') {
return replyWithHTML( return ctx.replyWithHTML(
' <b>Sorry, only admins access this command.</b>', ' <b>Sorry, only admins access this command.</b>',
); );
} }
const [ , commandName ] = text.split(' '); const [ , commandName ] = text.split(' ');
if (!commandName) { if (!commandName) {
return replyWithHTML( return ctx.replyWithHTML(
'<b>Send a valid command.</b>\n\nExample:\n' + '<b>Send a valid command.</b>\n\nExample:\n' +
'<code>/removecommand rules</code>', '<code>/removecommand rules</code>',
); );
@@ -25,20 +25,20 @@ const removeCommandHandler = async ({ from, chat, message, replyWithHTML }) => {
const command = await getCommand({ name: commandName.toLowerCase() }); const command = await getCommand({ name: commandName.toLowerCase() });
if (!command) { if (!command) {
return replyWithHTML( return ctx.replyWithHTML(
' <b>Command couldn\'t be found.</b>', ' <b>Command couldn\'t be found.</b>',
); );
} }
const role = command.role.toLowerCase(); const role = command.role.toLowerCase();
if (role === 'master' && !isMaster(from)) { if (role === 'master' && !isMaster(ctx.from)) {
return replyWithHTML( return ctx.replyWithHTML(
' <b>Sorry, only master can remove this command.</b>', ' <b>Sorry, only master can remove this command.</b>',
); );
} }
await removeCommand({ name: commandName.toLowerCase() }); await removeCommand({ name: commandName.toLowerCase() });
return replyWithHTML( return ctx.replyWithHTML(
`✅ <code>!${commandName}</code> ` + `✅ <code>!${commandName}</code> ` +
'<b>has been removed successfully.</b>', '<b>has been removed successfully.</b>',
); );

View File

@@ -29,13 +29,13 @@ const tgUnadmin = async (userToUnadmin) => {
}; };
/** @param { import('../../typings/context').ExtendedContext } ctx */ /** @param { import('../../typings/context').ExtendedContext } ctx */
const unAdminHandler = async ({ from, message, replyWithHTML }) => { const unAdminHandler = async (ctx) => {
if (!isMaster(from)) return null; if (!isMaster(ctx.from)) return null;
const { targets } = parse(message); const { targets } = parse(ctx.message);
if (targets.length !== 1) { if (targets.length !== 1) {
return replyWithHTML( return ctx.replyWithHTML(
' <b>Specify one user to unadmin.</b>', ' <b>Specify one user to unadmin.</b>',
).then(scheduleDeletion()); ).then(scheduleDeletion());
} }
@@ -43,13 +43,13 @@ const unAdminHandler = async ({ from, message, replyWithHTML }) => {
const userToUnadmin = await getUser(strip(targets[0])); const userToUnadmin = await getUser(strip(targets[0]));
if (!userToUnadmin) { if (!userToUnadmin) {
return replyWithHTML( return ctx.replyWithHTML(
'❓ <b>User unknown.</b>', '❓ <b>User unknown.</b>',
).then(scheduleDeletion()); ).then(scheduleDeletion());
} }
if (userToUnadmin.status !== 'admin') { if (userToUnadmin.status !== 'admin') {
return replyWithHTML( return ctx.replyWithHTML(
html` ${link(userToUnadmin)} <b>is not admin.</b>`, html` ${link(userToUnadmin)} <b>is not admin.</b>`,
); );
} }
@@ -58,7 +58,7 @@ const unAdminHandler = async ({ from, message, replyWithHTML }) => {
await unadmin(userToUnadmin); await unadmin(userToUnadmin);
return replyWithHTML( return ctx.replyWithHTML(
html`❗️ ${link(userToUnadmin)} <b>is no longer admin.</b>`, html`❗️ ${link(userToUnadmin)} <b>is no longer admin.</b>`,
); );
}; };

View File

@@ -50,33 +50,33 @@ const title = user => {
}; };
/** @param { import('../../typings/context').ExtendedContext } ctx */ /** @param { import('../../typings/context').ExtendedContext } ctx */
const getWarnsHandler = async ({ from, message, replyWithHTML }) => { const getWarnsHandler = async (ctx) => {
if (!from) { if (!ctx.from) {
return replyWithHTML( return ctx.replyWithHTML(
' <b>This command is not available in channels.</b>', ' <b>This command is not available in channels.</b>',
).then(scheduleDeletion()); ).then(scheduleDeletion());
} }
const { flags, targets } = parse(message); const { flags, targets } = parse(ctx.message);
if (targets.length > 1) { if (targets.length > 1) {
return replyWithHTML( return ctx.replyWithHTML(
' <b>Specify one user.</b>', ' <b>Specify one user.</b>',
).then(scheduleDeletion()); ).then(scheduleDeletion());
} }
const theUser = targets.length && from.status === 'admin' const theUser = targets.length && ctx.from.status === 'admin'
? await getUser(strip(targets[0])) ? await getUser(strip(targets[0]))
: from; : ctx.from;
if (!theUser) { if (!theUser) {
return replyWithHTML( return ctx.replyWithHTML(
'❓ <b>User unknown.</b>', '❓ <b>User unknown.</b>',
).then(scheduleDeletion()); ).then(scheduleDeletion());
} }
if (flags.has('raw') && from.status === 'admin') { if (flags.has('raw') && ctx.from.status === 'admin') {
return replyWithHTML( return ctx.replyWithHTML(
TgHtml.pre(inspect(theUser)), TgHtml.pre(inspect(theUser)),
).then(scheduleDeletion()); ).then(scheduleDeletion());
} }
@@ -111,7 +111,7 @@ const getWarnsHandler = async ({ from, message, replyWithHTML }) => {
permitS, permitS,
].filter(isNotEmpty)); ].filter(isNotEmpty));
return replyWithHTML(TgHtml.join('\n\n', [ return ctx.replyWithHTML(TgHtml.join('\n\n', [
oneliners, oneliners,
userWarns, userWarns,
banReason, banReason,

View File

@@ -24,7 +24,7 @@ module.exports = (ctx, next) => {
Object.assign(cbCtx, contextCustomizations); Object.assign(cbCtx, contextCustomizations);
cbCtx.botInfo = ctx.botInfo; cbCtx.botInfo = ctx.botInfo;
cbCtx.reply = ctx.editMessageText; cbCtx.reply = ctx.editMessageText.bind(ctx);
return next(cbCtx); return next(cbCtx);
}; };

View File

@@ -1,8 +1,7 @@
'use strict'; 'use strict';
const R = require('ramda'); const R = require('ramda');
const { optional, passThru } = require('telegraf'); const { Telegraf: { optional, passThru } } = require('telegraf');
const { permit } = require('../../stores/user'); const { permit } = require('../../stores/user');
const { html, lrm } = require('../../utils/html'); const { html, lrm } = require('../../utils/html');

View File

@@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { hears } = require('telegraf'); const { Telegraf: { hears } } = require('telegraf');
const XRegExp = require('xregexp'); const XRegExp = require('xregexp');
const { managesGroup } = require.main.require('./stores/group'); const { managesGroup } = require.main.require('./stores/group');

View File

@@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { compose, hears } = require('telegraf'); const { Telegraf: { compose, hears } } = require('telegraf');
/* eslint-disable global-require */ /* eslint-disable global-require */

View File

@@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { hears } = require('telegraf'); const { Telegraf: { hears } } = require('telegraf');
const R = require('ramda'); const R = require('ramda');
// DB // DB
@@ -63,7 +63,11 @@ const runCustomCmdHandler = async (ctx, next) => {
}; };
return ctx[typeToMethod(type)](content, options) 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); module.exports = hears(/^! ?(\w+)/, runCustomCmdHandler);

View File

@@ -41,7 +41,7 @@
"require-directory": "^2.1.1", "require-directory": "^2.1.1",
"spamwatch": "^0.2.0", "spamwatch": "^0.2.0",
"string-replace-async": "^1.2.1", "string-replace-async": "^1.2.1",
"telegraf": "^3.38.0", "telegraf": "^4.3.0",
"ts-node": "^8.9.1", "ts-node": "^8.9.1",
"typescript": "^3.8.3", "typescript": "^3.8.3",
"xregexp": "^4.2.0" "xregexp": "^4.2.0"

View File

@@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { compose } = require('telegraf'); const { Telegraf: { compose } } = require('telegraf');
const { config } = require('../utils/config'); const { config } = require('../utils/config');
const names = config.plugins || []; const names = config.plugins || [];

View File

@@ -33,7 +33,7 @@ const displayUser = user =>
/** @param {number | string | false} ms */ /** @param {number | string | false} ms */
const deleteAfter = ms => (ctx, next) => { const deleteAfter = ms => (ctx, next) => {
if (ms !== false) { if (ms !== false) {
setTimeout(ctx.deleteMessage, millisecond(ms)); setTimeout(ctx.deleteMessage.bind(ctx), millisecond(ms));
} }
next(); next();
}; };