Skip to content

Commit

Permalink
Fix floating promises
Browse files Browse the repository at this point in the history
  • Loading branch information
wojpawlik committed May 13, 2020
1 parent 20e142f commit 67f7120
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 6 additions & 15 deletions handlers/middlewares/checkLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const { telegram } = require('../../bot');
const {
excludeLinks = [],
blacklistedDomains = [],
notifyBrokenLink,
} = require('../../utils/config').config;

if (excludeLinks === false || excludeLinks === '*') {
Expand All @@ -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];
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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) => {
Expand All @@ -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,
Expand Down
14 changes: 4 additions & 10 deletions handlers/middlewares/logPresence.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions handlers/middlewares/removeChannelForwards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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(
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion handlers/middlewares/updateUserData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand Down
6 changes: 4 additions & 2 deletions handlers/unmatched.js
Original file line number Diff line number Diff line change
@@ -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?',
);
}
};

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
'use strict';

process.chdir(__dirname);
Expand All @@ -18,4 +19,5 @@ bot.use(

bot.catch(logError);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
bot.launch();

0 comments on commit 67f7120

Please sign in to comment.