From 20e142fa85745a459d01090d6219eef4d29787d6 Mon Sep 17 00:00:00 2001 From: Wojciech Pawlik Date: Wed, 13 May 2020 22:38:59 +0200 Subject: [PATCH] Remove replyOptions --- bot/options.js | 13 ------------- handlers/commands/addCommand.js | 14 +++++--------- handlers/commands/removeCommand.js | 25 +++++++++---------------- handlers/middlewares/antibot.js | 16 +++++----------- 4 files changed, 19 insertions(+), 49 deletions(-) delete mode 100644 bot/options.js diff --git a/bot/options.js b/bot/options.js deleted file mode 100644 index bbe3e995..00000000 --- a/bot/options.js +++ /dev/null @@ -1,13 +0,0 @@ -// @ts-check -'use strict'; - -/** @type {import('telegraf/typings/telegram-types').ExtraReplyMessage} */ -const replyOptions = { - disable_web_page_preview: true, - parse_mode: 'HTML', - reply_markup: { remove_keyboard: true } -}; - -module.exports = { - replyOptions -}; diff --git a/handlers/commands/addCommand.js b/handlers/commands/addCommand.js index c659dd51..24aa57be 100644 --- a/handlers/commands/addCommand.js +++ b/handlers/commands/addCommand.js @@ -5,7 +5,6 @@ const { addCommand, getCommand } = require('../../stores/command'); // Bot const { Markup } = require('telegraf'); -const { replyOptions } = require('../../bot/options'); const Cmd = require('../../utils/cmd'); const { isMaster } = require('../../utils/config'); @@ -48,9 +47,8 @@ const addCommandHandler = async (ctx) => { const { id } = ctx.from; if (ctx.from.status !== 'admin') { - return reply( + return ctx.replyWithHTML( 'ℹ️ Sorry, only admins access this command.', - replyOptions ); } @@ -59,10 +57,9 @@ const addCommandHandler = async (ctx) => { const isValidName = /^!?(\w+)$/.exec(commandName); if (!isValidName) { - return reply( + return ctx.replyWithHTML( 'Send a valid command.\n\nExample:\n' + '/addcommand rules', - replyOptions ); } const newCommand = isValidName[1].toLowerCase(); @@ -86,13 +83,12 @@ const addCommandHandler = async (ctx) => { Markup.keyboard([ [ `/addcommand -replace ${newCommand}` ] ]) .oneTime() .resize() - .extra() + .extra(), ); } if (cmdExists && cmdExists.role === 'master' && !isMaster(ctx.from)) { - return ctx.reply( + return ctx.replyWithHTML( 'ℹ️ Sorry, only master can replace this command.', - replyOptions ); } @@ -111,7 +107,7 @@ const addCommandHandler = async (ctx) => { return ctx.replyWithHTML( `✅ Successfully added !${isValidName[1]}.\n` + 'Who should be able to use it?', - inlineKeyboard(roleKbRow({ currentRole: role, newCommand })) + inlineKeyboard(roleKbRow({ currentRole: role, newCommand })), ); } diff --git a/handlers/commands/removeCommand.js b/handlers/commands/removeCommand.js index 038371f3..29a88b54 100644 --- a/handlers/commands/removeCommand.js +++ b/handlers/commands/removeCommand.js @@ -3,51 +3,44 @@ // DB const { getCommand, removeCommand } = require('../../stores/command'); -// Bot -const { replyOptions } = require('../../bot/options'); +const { isMaster } = require('../../utils/config'); /** @param { import('../../typings/context').ExtendedContext } ctx */ -const removeCommandHandler = async ({ chat, message, reply, state }) => { - const { isAdmin, isMaster } = state; +const removeCommandHandler = async ({ from, chat, message, replyWithHTML }) => { const { text } = message; if (chat.type !== 'private') return null; - if (!isAdmin) { - return reply( + if (from.status !== 'admin') { + return replyWithHTML( 'ℹ️ Sorry, only admins access this command.', - replyOptions ); } const [ , commandName ] = text.split(' '); if (!commandName) { - return reply( + return replyWithHTML( 'Send a valid command.\n\nExample:\n' + '/removecommand rules', - replyOptions ); } const command = await getCommand({ name: commandName.toLowerCase() }); if (!command) { - return reply( + return replyWithHTML( 'ℹ️ Command couldn\'t be found.', - replyOptions ); } const role = command.role.toLowerCase(); - if (role === 'master' && !isMaster) { - return reply( + if (role === 'master' && !isMaster(from)) { + return replyWithHTML( 'ℹ️ Sorry, only master can remove this command.', - replyOptions ); } await removeCommand({ name: commandName.toLowerCase() }); - return reply( + return replyWithHTML( `✅ !${commandName} ` + 'has been removed successfully.', - replyOptions ); }; diff --git a/handlers/middlewares/antibot.js b/handlers/middlewares/antibot.js index 556854e0..f8b1d3c7 100644 --- a/handlers/middlewares/antibot.js +++ b/handlers/middlewares/antibot.js @@ -1,10 +1,6 @@ 'use strict'; -// Bot -const { replyOptions } = require('../../bot/options'); - -// DB -const { isAdmin } = require('../../stores/user'); +const { pMap } = require('../../utils/promise'); const link = user => '@' + user.username; @@ -19,17 +15,15 @@ const antibotHandler = async (ctx, next) => { return next(); } - if (await isAdmin(ctx.from)) { + if (ctx.from.status === 'admin') { return next(); } - for (const bot of bots) { - ctx.telegram.kickChatMember(ctx.chat.id, bot.id); - } + await pMap(bots, bot => + ctx.kickChatMember(bot.id)); - ctx.reply( + await ctx.replyWithHTML( `🚫 Kicked bot(s): ${bots.map(link).join(', ')}`, - replyOptions ); return next();