-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscord-client.js
More file actions
210 lines (194 loc) · 9.46 KB
/
discord-client.js
File metadata and controls
210 lines (194 loc) · 9.46 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**CONST**/
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
const sortedDestination = require("./sortedDestinations.json");
const fs = require("fs");
const baseHTML = fs.readFileSync("./base.html").toString();
let channel = false;
let sorted = {
"mostVotes": [],
"leastVotes": [],
"mostPositiveVotes": [],
"mostNegativeVotes": [],
"mostLiked": [],
"mostDisliked": [],
"mostVotedTies": []
};
async function sort(message, type){
let upVotes = message.reactions.find(function(reaction){
return reaction.emoji.id === config.upVoteEmoji;
}).count - 1 || 0;
let downVotes = message.reactions.find(function(reaction){
return reaction.emoji.id === config.downVoteEmoji;
}).count - 1 || 0;
let total = upVotes + downVotes;
let pushed = false;
let upVotePercent = ((upVotes / total) * 100);
let targetIndex = sorted[type].length > 0 ? sorted[type].length-1: 0;
let modified = false;
function push(index){
sorted[type].splice(index, 0, {upVotes, downVotes, id: message.id});
pushed = true;
}
if(config.maxSuggestionsPerPage > 0 && sorted[type].length >= config.maxSuggestionsPerPage) return;
for(let i = sorted[type].length-1; i >= 0; i--){
if(pushed) break;
let upVotePercent2 = ((sorted[type][i].upVotes / (sorted[type][i].upVotes + sorted[type][i].downVotes)) * 100);
let upVotePercent3 = ((sorted[type][targetIndex].upVotes / (sorted[type][targetIndex].upVotes + sorted[type][targetIndex].downVotes)) * 100);
switch(type){
case "mostPositiveVotes":
if(upVotes > sorted[type][i].upVotes && upVotes > sorted[type][targetIndex].upVotes) {
targetIndex = i;
modified = true;
}
break;
case "mostNegativeVotes":
if(downVotes > sorted[type][i].downVotes && downVotes > sorted[type][targetIndex].downVotes) {
targetIndex = i;
modified = true;
}
break;
case "mostVotes":
console.log(total,sorted[type][i].downVotes + sorted[type][i].upVotes,sorted[type][targetIndex].downVotes + sorted[type][targetIndex].upVotes, upVotes, sorted[type][i].upVotes,sorted[type][targetIndex].upVotes);
if(total >= sorted[type][i].downVotes + sorted[type][i].upVotes && total >= sorted[type][targetIndex].downVotes + sorted[type][targetIndex].upVotes && upVotes > sorted[type][i].upVotes && upVotes > sorted[type][targetIndex].upVotes) {
console.log(i);
targetIndex = i;
modified = true;
}
break;
case "leastVotes":
if(total < sorted[type][i].downVotes + sorted[type][i].upVotes && total < sorted[type][targetIndex].downVotes + sorted[type][targetIndex].upVotes) {
targetIndex = i;
modified = true;
}
break;
case "mostLiked":
if(upVotePercent >= upVotePercent2 && upVotePercent >= upVotePercent3 && upVotes > sorted[type][i].upVotes && upVotes > sorted[type][targetIndex].upVotes) {
targetIndex = i;
modified = true;
}
break;
case "mostDisliked":
if(upVotePercent <= upVotePercent2 && upVotePercent <= upVotePercent3 && downVotes > sorted[type][i].downVotes && downVotes > sorted[type][targetIndex].downVotes) {
targetIndex = i;
modified = true;
}
break;
case "mostVotedTies":{
let isTie = upVotes > downVotes ? upVotes - (total / config.tieGracePercent) < downVotes : downVotes - (total / config.tieGracePercent) < upVotes;
if(isTie && total > sorted[type][i].upVotes + sorted[type][i].downVotes && total > sorted[type][targetIndex].upVotes + sorted[type][targetIndex].downVotes) {
targetIndex = i;
modified = true;
}
}
break;
default:
push(i);
break;
}
}
if(!pushed && modified) push(targetIndex);
else if(!pushed && !modified) push(sorted[type].length);
}
async function checkMessages(){
return new Promise(function(cb, err){
if(!config.channel) throw Error("No channel id found. Please place the channel's id in the config file.");
channel = client.channels.get(config.channel);
if(!channel){
if(config.debug) console.error("Couldn't fetch channel. Possible problems:\n1. Client doesn't have read/access to channel\n2. Channel's id is wrong.");
return;
}
if(channel.type === "dm" || channel.type === "text" || channel.type === "news" || channel.type === "store"){
channel.fetchMessages().then(function(msgs){
msgs = msgs.filter(function(msg){
return msg.author.bot;
});
let arr = msgs.array();
let keys = Object.keys(sorted);
function quickSort(item, index){
return new Promise(function(cb){
if(!keys[index]){
cb();
return;
}
if(config.debug) console.log(`Sorting ${keys[index]}`);
if(sortedDestination[keys[index]]){
sort(item, keys[index]).then(function(){
quickSort(item, index + 1).then(cb);
});
} else quickSort(item, index + 1).then(cb);
});
}
function mainSort(index){
if(config.debug) console.log(`[------ Sorting Message #${index + 1} -----]`);
quickSort(arr[index], 0).then(function(){
if(index + 1 === arr.length) cb();
else mainSort(index + 1);
});
}
mainSort(0);
});
} else if(config.debug) console.error(`Un-supported channel type: [ ${channel.type} ]`);
});
}
function createHTML(){
let top = baseHTML.split("<!-- Divider -->")[0];
let bottom = baseHTML.split("<!-- Divider -->")[1];
let baseLink = `https://discordapp.com/channels/${channel.guild.id}/${channel.id}/`;
function fileMaking(j){
let keys = Object.keys(sorted);
let index = keys[j];
let file = top;
function createRow(i){
if(sortedDestination[index]){
if(sorted[index][i]){
channel.fetchMessage(sorted[index][i].id).then(function(msg){
file += `\n<tr>\n<td class="col1">${msg.embeds[0].title.substring("Suggestion from ".length).split(" (")[0]}<br><a href="${baseLink}${msg.id}">link to suggestion</a></td>\n<td class="col2">${sorted[index][i].upVotes}<img src="./UpVote.png"></td>\n<td class="col3">${sorted[index][i].downVotes}<img src="./DownVote.png"></td>\n<td class="col4">${msg.embeds[0].description}</td>\n</tr>`;
createRow(i + 1);
});
} else{
file += bottom;
fs.writeFile(`./${sortedDestination[index]}`, file, function(){
fileMaking(j + 1);
});
}
} else fileMaking(j + 1);
}
if(sorted[index] !== undefined && sorted[index] != false){
if(config.debug && sortedDestination[index]) console.log(`Creating HTML file for ${index}`);
createRow(0);
}
else if(sorted[index] !== undefined) fileMaking(j + 1);
}
fileMaking(0);
}
client.on("ready", function(){
if(config.debug) console.log(`Bot's ready`);
function update (){
checkMessages().then(function(){
if(config.debug) console.log(`\n\n{\n mostVotes: ${sorted.mostVotes.length} messages sorted,\n leastVotes: ${sorted.leastVotes.length} messages sorted.\n mostPositiveVotes: ${sorted.mostPositiveVotes.length} messages sorted,\n mostNegativeVotes: ${sorted.mostNegativeVotes.length} messages sorted,\n mostLiked: ${sorted.mostLiked.length} messages sorted,\n mostDisliked: ${sorted.mostDisliked.length} messages sorted,\n mostVotedTies: ${sorted.mostVotedTies.length} messages sorted\n}`);
createHTML();
}).catch(console.error);
}
if(!(config.updateHours === false || config.updateHours === "false") && config.updateHours != undefined){
setTimeout(update,3600000 * parseFloat(config.updateHours));
}
update();
});
client.on("message", function(message){
let isOwner = false;
for(let i =0;i<config.ownerIDs.length;i++){
if(message.id === config.ownerIDs[i]) isOwner = true;
}
if(!isOwner) return;
if(message.content === config.stopCmd){
message.author.send('Stopping...').then(function(){
process.exit();
})
}
if(message.content === ">stopSuggestionBotNow"){
process.exit();
}
});
client.login(config.token);