Skip to content

Commit

Permalink
Refactor/cleaning (#87)
Browse files Browse the repository at this point in the history
* refactor: cleaned code, added types

* refactor: better types for bot related code
  • Loading branch information
atassis authored Dec 2, 2020
1 parent 517e0b5 commit fda0c72
Show file tree
Hide file tree
Showing 24 changed files with 207 additions and 509 deletions.
93 changes: 24 additions & 69 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
},
"license": "MIT",
"dependencies": {
"@elastic/elasticsearch": "^7.10.0",
"@sentry/node": "^5.27.6",
"dotenv": "^8.2.0",
"elasticsearch": "^16.7.2",
"pg": "^8.5.1",
"pg-hstore": "^2.3.3",
"sequelize": "^6.3.5",
Expand Down
3 changes: 0 additions & 3 deletions src/.chatlist.example.json

This file was deleted.

22 changes: 12 additions & 10 deletions src/features/ban-hammer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import Extra from 'telegraf/extra';
import * as text from '../../text';
import { allowWhiteListChat } from '../../middlewares/allowed-chat';
import { adminRequiredSilenced } from '../../middlewares/admin-required';
import { BotContext } from '../../types';

async function handleBanCommand({
from,
privateChannel,
deleteMessage,
message,
match,
reply,
getChat,
chat,
}) {
async function handleBanCommand(ctx: BotContext) {
const {
from,
privateChannel,
deleteMessage,
message,
match,
reply,
getChat,
chat,
} = ctx;
console.log('handleBanCommand', message, match);
const [, , reason] = match;
const replyMessage = message.reply_to_message;
Expand Down
20 changes: 0 additions & 20 deletions src/features/index.ts

This file was deleted.

14 changes: 4 additions & 10 deletions src/features/readonly-mode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ import Extra from 'telegraf/extra';
import * as text from '../../text';
import { allowWhiteListChat } from '../../middlewares/allowed-chat';
import { adminRequiredSilenced } from '../../middlewares/admin-required';
import { Bot, BotContext } from '../../types';

/** 1 day in seconds */
const RO_TIME = 86400;
const SECOND = 1000;

async function handleReadonlyCommand({
from,
privateChannel,
message,
match,
reply,
getChat,
chat,
}) {
async function handleReadonlyCommand(ctx: BotContext) {
const { from, privateChannel, message, match, reply, getChat, chat } = ctx;
console.log('handleReadonlyCommand', message, match);
const [, , reason] = match;

Expand All @@ -41,7 +35,7 @@ async function handleReadonlyCommand({
}
}

export function featureReadonlyMode(bot) {
export function featureReadonlyMode(bot: Bot) {
bot.hears(
new RegExp(`^(${text.commands.readonly()}|!ro)( .*)?`),
allowWhiteListChat,
Expand Down
36 changes: 18 additions & 18 deletions src/features/spam-hammer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { allowWhiteListChat } from '../../middlewares/allowed-chat';
import { adminRequiredSilenced } from '../../middlewares/admin-required';
import { Message } from '../../models';
import { installKeyboardActions, keyboardUnspamUser } from './keyboards';
import { Bot, BotContext } from '../../types';

async function handleEachMessage(ctx, next) {
async function handleEachMessage(ctx: BotContext, next) {
const {
message,
from: userFrom,
Expand Down Expand Up @@ -53,18 +54,19 @@ async function handleEachMessage(ctx, next) {
}

/* eslint-disable no-restricted-syntax, no-await-in-loop */
async function handleSpamCommand({
message,
from,
update,
chat,
match,
reply,
privateChannel,
getHammer,
deleteMessage,
getChat,
}) {
async function handleSpamCommand(ctx: BotContext) {
const {
message,
from,
update,
chat,
match,
reply,
privateChannel,
getHammer,
deleteMessage,
getChat,
} = ctx;
console.log('handleSpamCommand');
const [, rawReason] = match;
const reason = `${text.spamHammer.shortSpamReason()} ${rawReason || ''}`;
Expand Down Expand Up @@ -105,7 +107,7 @@ async function handleSpamCommand({
await deleteMessage();

// Ban user in another controlled chats #66.7
const blacklistedList = await hammer.blacklistUser(spammer, [chat.id]);
await hammer.blacklistUser(spammer, [chat.id]);

try {
// Delete messages in all another chats #66.8
Expand All @@ -120,9 +122,6 @@ async function handleSpamCommand({
{
originChat: chat,
banned: spammer,
chats: blacklistedList.filter(
(blacklisted) => blacklisted.id !== chat.id,
),
moder: from,
reason,
},
Expand All @@ -138,13 +137,14 @@ async function handleSpamCommand({
} else {
reply(
text.common.commandShouldBeReplied(text.commands.spam()),
// @ts-ignore
Extra.inReplyTo(message.message_id),
);
}
}
/* eslint-enable no-restricted-syntax */

export function featureSpamHammer(bot) {
export function featureSpamHammer(bot: Bot) {
bot.on('message', allowWhiteListChat, handleEachMessage);
bot.hears(
new RegExp(`^${text.commands.spam()}( .*)?`),
Expand Down
Loading

0 comments on commit fda0c72

Please sign in to comment.