Skip to content

Commit

Permalink
Default logger
Browse files Browse the repository at this point in the history
  • Loading branch information
brussell98 committed Nov 27, 2016
1 parent 244f5cd commit 47b032f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
37 changes: 18 additions & 19 deletions lib/Bot.js
Expand Up @@ -19,14 +19,13 @@ try {
* var mirai = new Mirai(config);
*/
class Bot extends Eris.Client {
constructor(config, rLogger, rChatHandler) {
constructor(config = {}, rLogger, rChatHandler) {
super(config.token, config.eris);

config = config || {};
this.carbonBotsKey = config.carbonBotsKey;
this.discordBotsKey = config.discordBotsKey;

this.logger = rLogger || new Logger();
this.logger = rLogger || new Logger(config.logger);
this.chatHandler = rChatHandler || new ChatHandler(this, config.chatHandler);
this.blacklistedGuilds = config.blacklistedGuilds || [];
this.blacklistedUsers = config.blacklistedUsers || [];
Expand All @@ -36,34 +35,35 @@ class Bot extends Eris.Client {

this.on('ready', () => {
this.chatHandler.run();
this.logger.info('ready'); // TODO
this.logger.info('Mirai: Connected to Discord');
});

this.on('error', (error, shard) => {
this.logger.error('error: ' + error, shard); // TODO
if (error)
this.logger.error(`Mirai: Shard ${shard} error: `, error);
});

this.on('disconnected', () => {
this.logger.error('disconnected'); // TODO
this.logger.error('Mirai: Disconnected from Discord');
});

this.on('shardReady', shard => {
this.logger.info('shard', shard, 'ready'); // TODO
this.logger.info(`Mirai: Shard ${shard} ready`);
});

this.on('shardDisconnect', (error, shard) => {
this.logger.warn('shard', shard, 'disconnected'); // TODO
this.logger.warn(`Mirai: Shard ${shard} disconnected` + (error ? ': ' + error.message : ''));
});

this.on('shardResume', shard => {
this.logger.info('shard', shard, 'resumed'); // TODO
this.logger.info(`Mirai: Shard ${shard} resumed`);
});

if (this.blacklistedGuilds.length !== 0) {
this.on('guildCreate', guild => {
if (this.blacklistedGuilds.includes(guild.id)) {
guild.leave();
this.logger.info('left', guild.name); // TODO
this.logger.info('Mirai: Left blacklisted guild', guild.name); // TODO
}
});
}
Expand Down Expand Up @@ -163,24 +163,23 @@ class Bot extends Eris.Client {
key: key || this.carbonBotsKey,
servercount: this.guilds.size
}).then(response => {
this.logger.debug('Sent servercount: ' + this.guilds.size + ' to carbonitex.net. Response: ' + response.status);
this.logger.debug('Sent servercount:', this.guilds.size, 'to carbonitex.net. Response:', response.status);
}).catch(error => {
if (error.response)
return this.logger.warn('Error response status ' + error.response.status + ' from carbonitex.net. Data: ' + error.response.data);
this.logger.warn('Error during axios request: ' + error.stack);
return this.logger.warn('Error response status', error.response.status, 'from carbonitex.net. Data:', error.response.data);
this.logger.error('Error during axios request:', error.stack);
});
}

updateDiscordBots(key) {
return axios.post(`https://bots.discord.pw/api/bots/${this.user.id}/stats`, {
data: { server_count: this.guilds.size },
return axios.post(`https://bots.discord.pw/api/bots/${this.user.id}/stats`, { server_count: this.guilds.size }, {
headers: { 'Authorization': key || this.discordBotsKey }
}).then(response => {
this.logger.debug('Sent server_count: ' + this.guilds.size + ' to bots.discord.pw. Response: ' + response.status);
this.logger.debug('Sent server_count:', this.guilds.size, 'to bots.discord.pw. Response:', response.status);
}).catch(error => {
if (error.response)
return this.logger.warn('Error response status ' + error.response.status + ' from bots.discord.pw. Data: ' + error.response.data);
this.logger.warn('Error during axios request: ' + error.stack);
return this.logger.warn('Error response status', error.response.status, 'from bots.discord.pw. Data:', error.response.data);
this.logger.error('Error during axios request:', error.stack);
});
}

Expand All @@ -195,7 +194,7 @@ class Bot extends Eris.Client {
this.logger.debug('Updated avatar from ' + url);
resolve();
}).catch(error => {
this.logger.warn('Failed to update avatar from ' + url + ': ' + error);
this.logger.warn(`Failed to update avatar from ${url}: ${error}`);
reject(error);
});
}).catch(error => {
Expand Down
3 changes: 3 additions & 0 deletions lib/ChatHandler.js
@@ -1,3 +1,5 @@
const chalk = new (require('chalk')).constructor({enabled: true});

class ChatHandler {
constructor(bot, config) {
this.bot = bot;
Expand Down Expand Up @@ -51,6 +53,7 @@ class ChatHandler {
}

getHelp(message) {
this.bot.logger.log(`${(message.channel.guild ? chalk.bold.green(message.channel.guild.name) + ' >> ' : '')}${chalk.cyan(message.author.username)} > ${message.cleanContent}`);
message.channel.createMessage(`${this.help.before + '\n' || ''}${this.bot.commandPlugins.map(plugin => plugin.help).filter(help => !!help).join('\n')}${this.help.after ? '\n' + this.help.after : ''}`);
}

Expand Down
36 changes: 30 additions & 6 deletions lib/Logger.js
@@ -1,15 +1,39 @@
const chalk = new (require('chalk')).constructor({enabled: true});

class Logger {
constructor() { }
constructor(options = {}) {
this.showDebug = !!options.showDebug;
this.onlyErrors = !!options.onlyErrors;
this.timestamps = !!options.timestamps
}

get timestamp() {
return this.timestamps ? `[${new Date().toLocaleString()}] ` : '';
}

log(text) { console.log(text); }
log() {
if (!this.onlyErrors)
console.log(this.timestamp + Array.prototype.join.call(arguments, ' '));
}

info(text) { console.info(text); } // TODO: Blue info text
info() {
if (!this.onlyErrors)
console.info(this.timestamp + chalk.cyan(...arguments));
}

debug(text) { console.log(text); } // TODO: Grey debug text
debug() {
if (this.showDebug)
console.log(this.timestamp + chalk.gray(...arguments));
}

warn(text) { console.warn(text); } // TODO: Orange warn text
warn() {
if (!this.onlyErrors)
console.warn(this.timestamp + chalk.yellow(...arguments));
}

error(text) { console.error(text); } // TODO: Red error text
error() {
console.error(this.timestamp + chalk.red(...arguments));
}
}

module.exports = Logger;

0 comments on commit 47b032f

Please sign in to comment.