From 67f712096889ec02677d1df34d884564f8da1954 Mon Sep 17 00:00:00 2001 From: Wojciech Pawlik Date: Wed, 13 May 2020 22:59:23 +0200 Subject: [PATCH] Fix floating promises --- .eslintrc.json | 2 +- handlers/middlewares/checkLinks.js | 21 ++++++------------- handlers/middlewares/logPresence.js | 14 ++++--------- handlers/middlewares/removeChannelForwards.js | 6 +++--- handlers/middlewares/updateUserData.js | 2 +- handlers/unmatched.js | 6 ++++-- index.js | 2 ++ 7 files changed, 21 insertions(+), 32 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 794a472d..dab293ae 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -47,7 +47,7 @@ "always" ], "@typescript-eslint/no-base-to-string": "error", - "@typescript-eslint/no-floating-promises": "warn", + "@typescript-eslint/no-floating-promises": "error", "no-console": [ "error", { "allow": [ "assert" ] } ], "for-direction": "error", "no-await-in-loop": "error", diff --git a/handlers/middlewares/checkLinks.js b/handlers/middlewares/checkLinks.js index 7bac96a2..986534d6 100644 --- a/handlers/middlewares/checkLinks.js +++ b/handlers/middlewares/checkLinks.js @@ -15,7 +15,6 @@ const { telegram } = require('../../bot'); const { excludeLinks = [], blacklistedDomains = [], - notifyBrokenLink, } = require('../../utils/config').config; if (excludeLinks === false || excludeLinks === '*') { @@ -27,7 +26,7 @@ const normalizeTme = R.replace( /^(?:@|(?:https?:\/\/)?(?:t\.me|telegram\.(?:me|dog))\/)(\w+)(\/.+)?/i, (_match, username, rest) => /^\/\d+$/.test(rest) ? `https://t.me/${username.toLowerCase()}` - : `https://t.me/${username.toLowerCase()}${rest || ''}` + : `https://t.me/${username.toLowerCase()}${rest || ''}`, ); const stripQuery = s => s.split('?', 1)[0]; @@ -58,7 +57,7 @@ const constructAbsUrl = R.constructN(1, URL); const isHttp = R.propSatisfies(R.test(/^https?:$/i), 'protocol'); const isLink = R.propSatisfies( conatained([ 'url', 'text_link', 'mention' ]), - 'type' + 'type', ); const memoize = R.memoizeWith(R.identity); @@ -106,7 +105,7 @@ const domainHandlers = new Map([ [ 't.me', dh.tme ], [ 'telegram.dog', dh.tme ], [ 'telegram.me', dh.tme ], - ...blacklistedDomains.map(domain => [ domain, dh.blacklistedDomain ]) + ...blacklistedDomains.map(domain => [ domain, dh.blacklistedDomain ]), ]); const isWhitelisted = (url) => customWhitelist.has(stripQuery(url.toString())); @@ -161,7 +160,7 @@ const buttonUrls = R.pipe( R.path([ 'reply_markup', 'inline_keyboard' ]), R.defaultTo([]), R.unnest, - R.chain(maybeProp('url')) + R.chain(maybeProp('url')), ); /** @param { import('../../typings/context').ExtendedContext } ctx */ @@ -192,15 +191,7 @@ const classifyCtx = (ctx) => { module.exports = async (ctx, next) => (await classifyCtx(ctx)).cata({ Nothing: next, - Notify(error) { - const message = ctx.message || ctx.editedMessage; - const reply_to_message_id = message.message_id; - if (notifyBrokenLink) { - ctx.reply( - `️ℹ️ Link ${error.url} seems to be broken (${error.code}).`, - { reply_to_message_id } - ); - } + Notify() { return next(); }, Warn: async (reason) => { @@ -210,7 +201,7 @@ module.exports = async (ctx, next) => if (userToWarn.id === 777000) return next(); if (await isAdmin(userToWarn)) return next(); - ctx.deleteMessage(); + ctx.deleteMessage().catch(() => null); return ctx.warn({ admin, reason, diff --git a/handlers/middlewares/logPresence.js b/handlers/middlewares/logPresence.js index 54abe51e..a6acbe5a 100644 --- a/handlers/middlewares/logPresence.js +++ b/handlers/middlewares/logPresence.js @@ -22,19 +22,13 @@ function log(ctx, next) { ctx.message.new_chat_members.map(getUsername).join(', ') + ' #joined ' + ctx.chat.title, { reply_markup: { inline_keyboard: [ [ { - text: '🚫 Ban all', + text: `🚫 Ban ${ctx.message.new_chat_members.length}`, callback_data: `/ban ${ ctx.message.new_chat_members .map(getId) - .join(' ')} [joining]` - } ] ] } } - ); - } else if (ctx.updateSubTypes[0] === 'left_chat_member') { - ctx.telegram.sendMessage( - chats.presenceLog, - getUsername(ctx.message.left_chat_member) + - ' #left ' + ctx.chat.title - ); + .join(' ')} [joining]`, + } ] ] } }, + ).catch(() => null); } return next(); } diff --git a/handlers/middlewares/removeChannelForwards.js b/handlers/middlewares/removeChannelForwards.js index 89052e1a..ebac4ec9 100644 --- a/handlers/middlewares/removeChannelForwards.js +++ b/handlers/middlewares/removeChannelForwards.js @@ -14,7 +14,7 @@ if (excludeLinks === false || excludeLinks === '*') { const isChannelForward = R.pathEq( [ 'message', 'forward_from_chat', 'type' ], - 'channel' + 'channel', ); const fromAdmin = R.pathEq([ 'from', 'status' ], 'admin'); @@ -24,7 +24,7 @@ const capturingGroups = R.tail; const toUsername = R.compose( capturingGroups, - R.match(/^(?:@|(?:https?:\/\/)?(?:t\.me|telegram\.(?:me|dog))\/)(\w+)/i) + R.match(/^(?:@|(?:https?:\/\/)?(?:t\.me|telegram\.(?:me|dog))\/)(\w+)/i), ); const customWhitelist = R.pipe( @@ -47,7 +47,7 @@ const pred = R.allPass([ /** @param { import('../../typings/context').ExtendedContext } ctx */ const handler = ctx => { - ctx.deleteMessage(); + ctx.deleteMessage().catch(() => null); return ctx.warn({ admin: ctx.botInfo, reason: 'Channel forward', diff --git a/handlers/middlewares/updateUserData.js b/handlers/middlewares/updateUserData.js index 565aa317..1f756e34 100644 --- a/handlers/middlewares/updateUserData.js +++ b/handlers/middlewares/updateUserData.js @@ -8,7 +8,7 @@ const { updateUser } = require('../../stores/user'); /** @param { import('telegraf').ContextMessageUpdate } ctx */ const updateUserDataHandler = async (ctx, next) => { if (ctx.message && ctx.message.forward_from) { - updateUser(ctx.message.forward_from); + updateUser(ctx.message.forward_from).catch(() => null); } const { entities = [] } = ctx.message || {}; diff --git a/handlers/unmatched.js b/handlers/unmatched.js index 5f03dc23..8dc4c110 100644 --- a/handlers/unmatched.js +++ b/handlers/unmatched.js @@ -1,10 +1,12 @@ 'use strict'; /** @param { import('../typings/context').ExtendedContext } ctx */ -const unmatchedHandler = ctx => { +const unmatchedHandler = async ctx => { ctx.state[unmatchedHandler.unmatched] = true; if (ctx.chat && ctx.chat.type === 'private') { - ctx.reply('Sorry, I couldn\'t understand that, do you need /help?'); + await ctx.reply( + 'Sorry, I couldn\'t understand that, do you need /help?', + ); } }; diff --git a/index.js b/index.js index bf38a8eb..3ff1890f 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +// @ts-check 'use strict'; process.chdir(__dirname); @@ -18,4 +19,5 @@ bot.use( bot.catch(logError); +// eslint-disable-next-line @typescript-eslint/no-floating-promises bot.launch();