2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-30 21:55:17 +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');
/** @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(
' <b>Specify one user to promote.</b>',
).then(scheduleDeletion());
}
const userToAdmin = targets.length
? await getUser(strip(targets[0]))
: from;
: ctx.from;
if (!userToAdmin) {
return replyWithHTML(
return ctx.replyWithHTML(
'❓ <b>User unknown.</b>\n' +
'Please forward their message, then try again.',
).then(scheduleDeletion());
}
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') {
return replyWithHTML(
return ctx.replyWithHTML(
html`⭐️ ${link(userToAdmin)} <b>is already admin.</b>`,
);
}
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;

View File

@@ -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`🛠 <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,
reply_markup,
}).then(scheduleDeletion());

View File

@@ -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)

View File

@@ -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 || ' <b>No link to this group</b>', {
return ctx.replyWithHTML(group.link || ' <b>No link to this group</b>', {
disable_web_page_preview: false,
}).then(scheduleDeletion());
};

View File

@@ -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(
' <b>Sorry, only admins access this command.</b>',
);
}
const [ , commandName ] = text.split(' ');
if (!commandName) {
return replyWithHTML(
return ctx.replyWithHTML(
'<b>Send a valid command.</b>\n\nExample:\n' +
'<code>/removecommand rules</code>',
);
@@ -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(
' <b>Command couldn\'t be found.</b>',
);
}
const role = command.role.toLowerCase();
if (role === 'master' && !isMaster(from)) {
return replyWithHTML(
if (role === 'master' && !isMaster(ctx.from)) {
return ctx.replyWithHTML(
' <b>Sorry, only master can remove this command.</b>',
);
}
await removeCommand({ name: commandName.toLowerCase() });
return replyWithHTML(
return ctx.replyWithHTML(
`✅ <code>!${commandName}</code> ` +
'<b>has been removed successfully.</b>',
);

View File

@@ -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(
' <b>Specify one user to unadmin.</b>',
).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(
'❓ <b>User unknown.</b>',
).then(scheduleDeletion());
}
if (userToUnadmin.status !== 'admin') {
return replyWithHTML(
return ctx.replyWithHTML(
html` ${link(userToUnadmin)} <b>is not admin.</b>`,
);
}
@@ -58,7 +58,7 @@ const unAdminHandler = async ({ from, message, replyWithHTML }) => {
await unadmin(userToUnadmin);
return replyWithHTML(
return ctx.replyWithHTML(
html`❗️ ${link(userToUnadmin)} <b>is no longer admin.</b>`,
);
};

View File

@@ -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(
' <b>This command is not available in channels.</b>',
).then(scheduleDeletion());
}
const { flags, targets } = parse(message);
const { flags, targets } = parse(ctx.message);
if (targets.length > 1) {
return replyWithHTML(
return ctx.replyWithHTML(
' <b>Specify one user.</b>',
).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(
'❓ <b>User unknown.</b>',
).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,

View File

@@ -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);
};

View File

@@ -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');

View File

@@ -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');

View File

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

View File

@@ -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);

View File

@@ -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"

View File

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

View File

@@ -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();
};