-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
125 lines (114 loc) · 3.92 KB
/
app.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Load up the discord.js library
const Discord = require('discord.js')
const fs = require('fs')
const _ = require('underscore')
const client = new Discord.Client()
const config = require ('./config.json')
const plotly = require('plotly')(config.plotlyuser, config.plotlykey)
const gotkicked = require('./gotkicked.json')
const joinmessages = require('./joinmessages.json')
const stream = require('stream')
var vote = {};
// Since the events are moved to seperate files, need to smuggle these in through the config argument
config.colors = {
red: 0x781706,
orange: 0xBA430D,
green: 0x037800
}
config.gotkicked = gotkicked
config.joinmessages = joinmessages
function loadEvents () {
fs.readdir('./events/', (err, files) => {
if (err) {
return 'Failed to load Events'
} else {
files.forEach(file => {
const name = file.split('.')[0]
const event = require(`./events/${file}`)
client.on(name, event.bind(null, config, client, influx, vote))
console.log(`Loading Event: ${name}`)
})
}
})
}
config.loadplugins = loadplugins()
function loadplugins () {
var pluginsfile = fs.readFileSync('./plugins/plugins.json')
config.plugins = JSON.parse(pluginsfile)
console.log('Reloaded plugins. Current plugins:\n\n`' + Object.keys(config.plugins) + '`')
}
var Influx = require('influx')
var influx = new Influx.InfluxDB({
host: 'oort.zwater.us:8086',
database: 'nixnest',
schema: [
{
measurement: 'message',
fields: {
value: Influx.FieldType.FLOAT,
manual: Influx.FieldType.FLOAT,
join: Influx.FieldType.FLOAT
},
tags: [
'id'
]
}
]
})
async function getStats() {
console.log('Grabbing new stats, generating graphs');
const { execFile } = require('child_process')
execFile('./leaderboard.py', null, (error, stdout, stderr) => {
if (error) {
throw error
}
var leaders = JSON.parse(stdout)
//console.log(leaders);
//console.log(client.guilds);
//logGuild = _.find(client.guilds, function(guild) {return guild.id == config.logserver; });
//console.log(config.logserver);
//console.log(client.guilds);
var logGuild = client.guilds.find('id', config.logserver);
var users = [];
var counts = [];
//console.log(logGuild);
for (var entry in leaders) {
if (logGuild.members.find('id', leaders[entry].id)) {
users[entry] = logGuild.members.find('id', leaders[entry].id).displayName;
counts[entry] = leaders[entry].count;
leaders[entry].id = (logGuild.members.find('id', leaders[entry].id).displayName);
} else {
users[entry] = '<notfound-' + entry + '>';
counts[entry] = leaders[entry].count;
leaders[entry].id = ('<notfound>')
}
}
plotData = [
{
x: users,
y: counts,
type: "bar",
mode: "markers+text"
}
];
var layoutopts = {
title: "Top Posters, Last 7 days"
}
var figure = { 'data': plotData };
var graphOptions= {layout: layoutopts, format: "png", width: 1000, height: 500};
plotly.getImage(figure, graphOptions, function (err, msg) {
if (err) return console.log(err);
var output = fs.createWriteStream('leaderboard.png');
msg.pipe(output);
});
//console.log(leaders);
//console.log(users);
//console.log(counts);
//console.log(leaders[1].id);
//console.log(logGuild.members.find('id', leaders[1].id).displayName);
})
}
setInterval(getStats, 3600000);
loadEvents()
loadplugins()
client.login(config.token)