forked from bra1n/judgebot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
65 lines (58 loc) · 2.06 KB
/
utils.js
File metadata and controls
65 lines (58 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const log4js = require("log4js");
const request = require("request");
const chalk = require("chalk");
// setup logger
const getLogger = (name) => {
let logPattern = '%[[%p]%] '+chalk.red('[%c]') +' - %m';
if (!process.env.PAPERTRAIL_API_TOKEN) {
logPattern = '[%d{yy/MM/dd hh:mm:ss}] ' + logPattern;
}
// configure pattern
log4js.configure({
appenders: {out: {type: 'stdout', layout: {type: 'pattern', pattern: logPattern}}},
categories: { default: { appenders: ['out'], level: process.env.LOG_LEVEL || "info" } }
});
return log4js.getLogger(name + '-' + process.pid);
}
// create a pretty log message for a user / guild
const prettyLog = ({guild, channel = {}, author = {}}, action, log = '') => {
const logMessage = [
chalk.blue('[' + (guild ? guild.name : 'direct message') + '#' + (channel.name || '') +']'),
chalk.yellow('[' + (author.username ? author.username + '#' + author.discriminator : 'server') + ']'),
chalk.magenta('[' + action + ']'),
log
];
return logMessage.join(' ');
}
// send updated stats to bots.discord.com
const updateServerCount = (bot) => {
bot.user.setPresence({
activity: {
name: 'MTG on '+ bot.guilds.cache.size +' servers (' + bot.ws.shards.size + ' shards)',
type: 'PLAYING',
url: 'https://github.com/bra1n/judgebot'
}
});
const options = {
url: 'https://bots.discord.pw/api/bots/240537940378386442/stats',
method: 'POST',
headers: {'Authorization': process.env.BOT_TOKEN},
body: {"server_count": bot.guilds.size || 0},
json: true
};
// post stats to bots.discord.pw
if (process.env.BOT_TOKEN) {
request(options);
}
// post stats to discordbots.org
if (process.env.BOT_TOKEN2) {
options.url = 'https://discordbots.org/api/bots/240537940378386442/stats';
options.headers['Authorization'] = process.env.BOT_TOKEN2;
request(options);
}
};
module.exports = {
getLogger,
prettyLog,
updateServerCount
}