From f6da25c32233d32a2d5a149940cca2ce08002735 Mon Sep 17 00:00:00 2001 From: Andriy2 Date: Wed, 15 Aug 2018 20:13:47 +0300 Subject: [PATCH] Added this.logging to telebot.js --- lib/telebot.js | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/telebot.js b/lib/telebot.js index 49df135..bd1ba55 100644 --- a/lib/telebot.js +++ b/lib/telebot.js @@ -54,6 +54,8 @@ class TeleBot { looping: false }; + this.logging = cfg.logging == null ? true : cfg.logging; + this.modList = {}; this.eventList = new Map(); @@ -98,10 +100,14 @@ class TeleBot { plugin.call(this, this, config || {}); - console.log(`[bot.plugin] loaded '${id}' plugin`); + if (this.logging) { + console.log(`[bot.plugin] loaded '${id}' plugin`); + } } else { - console.log('[bot.plugin] skip plugin without id'); + if (this.logging) { + console.log('[bot.plugin] skip plugin without id'); + } } } @@ -120,12 +126,16 @@ class TeleBot { return this.setWebhook(url, cert, this.allowedUpdates, this.maxConnections).then(() => { - console.log(`[bot.webhook] set to "${url}"`); + if (this.logging) { + console.log(`[bot.webhook] set to "${url}"`); + } return webhook.call(this, this, this.webhook); }).catch((error) => { - console.error('[bot.error.webhook]', error); + if (this.logging) { + console.error('[bot.error.webhook]', error); + } this.event('error', {error}); }); @@ -136,15 +146,19 @@ class TeleBot { this.setWebhook().then((response) => { f.poll = true; - if (response.description === 'Webhook was deleted') { - console.log('[bot.webhook] webhook was deleted'); - } + if (this.logging) { + if (response.description === 'Webhook was deleted') { + console.log('[bot.webhook] webhook was deleted'); + } - console.log('[bot.info] bot started'); + console.log('[bot.info] bot started'); + } }).catch((error) => { - console.error('[bot.error.webhook]', error); + if (this.logging) { + console.error('[bot.error.webhook]', error); + } this.event('error', {error}); }); @@ -173,7 +187,9 @@ class TeleBot { const now = Date.now(); const diff = (now - f.retry) / 1000; - console.log(`[bot.info.update] reconnected after ${ diff } seconds`); + if (this.logging) { + console.log(`[bot.info.update] reconnected after ${ diff } seconds`); + } this.event('reconnected', { startTime: f.retry, endTime: now, diffTime: diff }); @@ -195,7 +211,9 @@ class TeleBot { // Set retry flag as current date (for timeout calculations) if (f.retry === false) f.retry = Date.now(); - console.error(`[bot.error.update]`, error.stack || error); + if (this.logging) { + console.error(`[bot.error.update]`, error.stack || error); + } this.event(['error', 'error.update'], {error}); return Promise.reject(); @@ -203,7 +221,9 @@ class TeleBot { }).catch(() => { const seconds = this.retryTimeout / 1000; - console.log(`[bot.info.update] reconnecting in ${ seconds } seconds...`); + if (this.logging) { + console.log(`[bot.info.update] reconnecting in ${ seconds } seconds...`); + } this.event('reconnecting'); // Set reconnecting timeout @@ -223,7 +243,9 @@ class TeleBot { stop(message) { this.flags.looping = false; - console.log(`[bot.info] bot stopped ${ message ? ': ' + message : '' }`); + if (this.logging) { + console.log(`[bot.info] bot stopped ${ message ? ': ' + message : '' }`); + } this.event('stop', message); }