-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (150 loc) · 5.05 KB
/
index.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
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
import dotenv from 'dotenv'
dotenv.config()
import fetch from "node-fetch";
import { Client, GatewayIntentBits } from 'discord.js';
// types of data we want to receive from Discord
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
],
});
import cron from 'cron'
let currentleaderboard = [];
let changes = [];
let Newdata = {};
let job = new cron.CronJob('*/15 * * * *', async() => {
console.log('running a task every minute');
//Newdata = null;
console.log(currentleaderboard.length)
const channel = client.channels.cache.get(channel_id)
if (currentleaderboard.length == 0) {
Newdata = await download();
processData(Newdata)
let leaderboardText = "";
leaderboardText = "``` Leaderboard: \n";
for (let i = 0; i < currentleaderboard.length; i++) {
leaderboardText += `${currentleaderboard[i].name} Completed ${currentleaderboard[i].stars} \n`;
}
leaderboardText += "```";
channel.send(leaderboardText);
}
else{
Newdata = await download();
console.log(Newdata)
processData(Newdata)
}
let textChanges = "";
if (changes.length > 0) {
textChanges += "``` Leaderboard changes: \n";
for (let i = 0; i < changes.length; i++) {
textChanges += `${changes[i]} \n`;
}
changes = [];
textChanges += "```";
channel.send(textChanges);
let leaderboardText = "";
leaderboardText = "``` Leaderboard: \n";
for (let i = 0; i < currentleaderboard.length; i++) {
leaderboardText += `${currentleaderboard[i].name} Completed ${currentleaderboard[i].stars} \n`;
}
leaderboardText += "```";
channel.send(leaderboardText);
}
});
// login to Discord
client.login(process.env.DISCORD_TOKEN);
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
var channel_id = 888888888888888888
client.on('messageCreate', async (message) => {
//console.log(message)
if (message.content === 'ping') {
message.reply('pong');
}
if (message.content === 'This Channel') {
message.reply(`channel id: ${message.channel.id}`);
channel_id = message.channel.id
}
if (message.content === '!start') {
channel_id = message.channel.id
job.start();
message.reply(`Advent will Start`);
}
if (message.content === '!stop') {
job.stop();
message.reply(`Advent will Stop`);
});
let download = async function(){
const cookie = process.env.cookie;
const url = process.env.url;
console.log(url, cookie)
var options = {
method: 'GET',
uri: url,
headers: {
'Content-Type': 'application/json;charset=utf-8',
'cookie': `session=${cookie}`
},
};
const ressponse = await fetch(url, options)
//console.log(ressponse)
const data = await ressponse.json()
//console.log(data)
console.log(data.event)
return data;
}
let processData = function(data){
let json = data;
let json2 = data;
let localdata = json
let proccessleaderboard = [];
if(Object.keys(json2).length != 0) {
let AOCkeys = Object.keys(json2.members);
console.log(AOCkeys);
for (let i = 0; i < AOCkeys.length; i++) {
let name = json2.members[AOCkeys[i]].name;
let stars = json2.members[AOCkeys[i]].stars;
let id = json2.members[AOCkeys[i]].id;
let local = {
name: name,
stars: stars,
id: id
}
proccessleaderboard.push(local);
}
}
if(json2.length === undefined){
json2 = json;
}
if (currentleaderboard.length === 0) {
let AOCkeys2 = Object.keys(json.members);
for (let i = 0; i < AOCkeys2.length; i++) {
let name = localdata.members[AOCkeys2[i]].name;
let stars = localdata.members[AOCkeys2[i]].stars;
let id = localdata.members[AOCkeys2[i]].id;
let local = {
name: name,
stars: stars,
id: id
}
currentleaderboard.push(local);
}
}
for(let i = 0; i < currentleaderboard.length; i++){
for(let x = 0; x < proccessleaderboard.length; x++) {
if(currentleaderboard[i].id === proccessleaderboard[x].id){
if(currentleaderboard[i].stars < proccessleaderboard[x].stars){
changes.push(currentleaderboard[i].name + " has gained " + (proccessleaderboard[x].stars - currentleaderboard[i].stars) + " stars");
currentleaderboard[i].stars = proccessleaderboard[x].stars;
}
}
}
}
currentleaderboard.sort((a, b) => (a.stars < b.stars) ? 1 : -1)
}