Skip to content

Commit

Permalink
Remove replyOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
wojpawlik committed May 13, 2020
1 parent 5b5cf33 commit 20e142f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 49 deletions.
13 changes: 0 additions & 13 deletions bot/options.js

This file was deleted.

14 changes: 5 additions & 9 deletions handlers/commands/addCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -48,9 +47,8 @@ const addCommandHandler = async (ctx) => {
const { id } = ctx.from;

if (ctx.from.status !== 'admin') {
return reply(
return ctx.replyWithHTML(
'ℹ️ <b>Sorry, only admins access this command.</b>',
replyOptions
);
}

Expand All @@ -59,10 +57,9 @@ const addCommandHandler = async (ctx) => {

const isValidName = /^!?(\w+)$/.exec(commandName);
if (!isValidName) {
return reply(
return ctx.replyWithHTML(
'<b>Send a valid command.</b>\n\nExample:\n' +
'<code>/addcommand rules</code>',
replyOptions
);
}
const newCommand = isValidName[1].toLowerCase();
Expand All @@ -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(
'ℹ️ <b>Sorry, only master can replace this command.</b>',
replyOptions
);
}

Expand All @@ -111,7 +107,7 @@ const addCommandHandler = async (ctx) => {
return ctx.replyWithHTML(
`✅ <b>Successfully added <code>!${isValidName[1]}</code></b>.\n` +
'Who should be able to use it?',
inlineKeyboard(roleKbRow({ currentRole: role, newCommand }))
inlineKeyboard(roleKbRow({ currentRole: role, newCommand })),
);
}

Expand Down
25 changes: 9 additions & 16 deletions handlers/commands/removeCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'ℹ️ <b>Sorry, only admins access this command.</b>',
replyOptions
);
}
const [ , commandName ] = text.split(' ');
if (!commandName) {
return reply(
return replyWithHTML(
'<b>Send a valid command.</b>\n\nExample:\n' +
'<code>/removecommand rules</code>',
replyOptions
);
}

const command = await getCommand({ name: commandName.toLowerCase() });
if (!command) {
return reply(
return replyWithHTML(
'ℹ️ <b>Command couldn\'t be found.</b>',
replyOptions
);
}

const role = command.role.toLowerCase();
if (role === 'master' && !isMaster) {
return reply(
if (role === 'master' && !isMaster(from)) {
return replyWithHTML(
'ℹ️ <b>Sorry, only master can remove this command.</b>',
replyOptions
);
}

await removeCommand({ name: commandName.toLowerCase() });
return reply(
return replyWithHTML(
`✅ <code>!${commandName}</code> ` +
'<b>has been removed successfully.</b>',
replyOptions
);
};

Expand Down
16 changes: 5 additions & 11 deletions handlers/middlewares/antibot.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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(
`🚫 <b>Kicked bot(s):</b> ${bots.map(link).join(', ')}`,
replyOptions
);

return next();
Expand Down

0 comments on commit 20e142f

Please sign in to comment.