Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions lib/telebot.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class TeleBot {
looping: false
};

this.logging = cfg.logging == null ? true : cfg.logging;

this.modList = {};
this.eventList = new Map();

Expand Down Expand Up @@ -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');
}
}

}
Expand All @@ -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});

});
Expand All @@ -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});

});
Expand Down Expand Up @@ -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
});
Expand All @@ -195,15 +211,19 @@ 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();

}).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
Expand All @@ -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);
}

Expand Down