2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-29 13:17:56 +00:00

Improve and disable notifyBrokenLink

This commit is contained in:
GingerPlusPlus 2018-04-21 17:50:57 +02:00
parent 32694b18bf
commit 6cfdfbed45
2 changed files with 23 additions and 6 deletions

View File

@ -57,6 +57,8 @@ module.exports = {
*/ */
groupsInlineKeyboard: [], groupsInlineKeyboard: [],
notifyBrokenLink: false,
numberOfWarnsToBan: 3, numberOfWarnsToBan: 3,
/** /**

View File

@ -13,8 +13,11 @@ const { managesGroup } = require('../../stores/group');
const { telegram } = require('../../bot'); const { telegram } = require('../../bot');
const warn = require('../../actions/warn'); const warn = require('../../actions/warn');
const {
const { excludeLinks = [], warnInlineKeyboard } = require('../../config'); excludeLinks = [],
notifyBrokenLink,
warnInlineKeyboard,
} = require('../../config');
const reply_markup = { inline_keyboard: warnInlineKeyboard }; const reply_markup = { inline_keyboard: warnInlineKeyboard };
if (excludeLinks === false || excludeLinks === '*') { if (excludeLinks === false || excludeLinks === '*') {
@ -105,12 +108,18 @@ const domainHandlers = new Map([
const isWhitelisted = (url) => customWhitelist.has(url.toString()); const isWhitelisted = (url) => customWhitelist.has(url.toString());
class CodeError extends Error {
constructor(code) {
super(code);
this.code = code;
}
}
const unshorten = url => const unshorten = url =>
fetch(url, { redirect: 'follow' }).then(res => fetch(url, { redirect: 'follow' }).then(res =>
res.ok res.ok
? new URL(normalizeTme(res.url)) ? new URL(normalizeTme(res.url))
: Promise.reject(new Error(`Request to ${url} failed, ` + : Promise.reject(new CodeError(`${res.status} ${res.statusText}`)));
`reason: ${res.status} ${res.statusText}`)));
const checkLinkByDomain = url => { const checkLinkByDomain = url => {
const domain = url.host.toLowerCase(); const domain = url.host.toLowerCase();
@ -132,6 +141,7 @@ const classifyAsync = memoize(async url => {
if (isWhitelisted(longUrl)) return Action.Nothing; if (isWhitelisted(longUrl)) return Action.Nothing;
return checkLinkByDomain(longUrl); return checkLinkByDomain(longUrl);
} catch (e) { } catch (e) {
e.url = url;
return Action.Notify(e); return Action.Notify(e);
} }
}); });
@ -170,10 +180,15 @@ const classifyCtx = (ctx) => {
module.exports = async (ctx, next) => module.exports = async (ctx, next) =>
(await classifyCtx(ctx)).cata({ (await classifyCtx(ctx)).cata({
Nothing: next, Nothing: next,
Notify(errorMsg) { Notify(error) {
const message = ctx.message || ctx.editedMessage; const message = ctx.message || ctx.editedMessage;
const reply_to_message_id = message.message_id; const reply_to_message_id = message.message_id;
ctx.reply(` ${errorMsg}`, { reply_to_message_id }); if (notifyBrokenLink) {
ctx.reply(
` Link ${error.url} seems to be broken (${error.code}).`,
{ reply_to_message_id }
);
}
return next(); return next();
}, },
Warn: async (reason) => { Warn: async (reason) => {