Skip to content

Commit

Permalink
Upgrade to Telegraf 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
C0rn3j committed May 7, 2021
1 parent a71292a commit 96e6a3c
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 56 deletions.
18 changes: 9 additions & 9 deletions handlers/commands/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 2 additions & 2 deletions handlers/commands/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ 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) =>
stripEmoji(a.title).localeCompare(stripEmoji(b.title)));

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());
Expand Down
6 changes: 3 additions & 3 deletions handlers/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions handlers/commands/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
Expand Down
20 changes: 10 additions & 10 deletions handlers/commands/removeCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@ 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>',
);
}

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>',
);
Expand Down
14 changes: 7 additions & 7 deletions handlers/commands/unadmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@ 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());
}

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>`,
);
}
Expand All @@ -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>`,
);
};
Expand Down
22 changes: 11 additions & 11 deletions handlers/commands/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion handlers/middlewares/commandButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
3 changes: 1 addition & 2 deletions handlers/middlewares/removeChannelForwards.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
2 changes: 1 addition & 1 deletion handlers/regex/groupLinker.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
2 changes: 1 addition & 1 deletion handlers/regex/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { compose, hears } = require('telegraf');
const { Telegraf: { compose, hears } } = require('telegraf');

/* eslint-disable global-require */

Expand Down
8 changes: 6 additions & 2 deletions handlers/regex/runCustomCmd.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { hears } = require('telegraf');
const { Telegraf: { hears } } = require('telegraf');
const R = require('ramda');

// DB
Expand Down Expand Up @@ -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);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { compose } = require('telegraf');
const { Telegraf: { compose } } = require('telegraf');

const { config } = require('../utils/config');
const names = config.plugins || [];
Expand Down
2 changes: 1 addition & 1 deletion utils/tg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down

0 comments on commit 96e6a3c

Please sign in to comment.