-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminder.js
139 lines (118 loc) Β· 4.51 KB
/
reminder.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
const { MessageEmbed } = require('discord.js');
const { SETTINGS_FILE_PATH, readInFile, writeFile } = require('./file_reader.js');
const { LEADERBOARD_FILE_PATH } = require('./leaderboard.js');
var savedMessage = null;
// Public
function scheduleReminder(client) {
readInFile(SETTINGS_FILE_PATH, data => {
const settings = JSON.parse(data);
const differenceNowToScheduledTime = settings.timeToSendMessage - Date.now();
setTimeout(() => {
remind(client, true);
}, differenceNowToScheduledTime);
});
}
function remind(client, isFromScheduler = false) {
readInFile(SETTINGS_FILE_PATH, data => {
const settings = JSON.parse(data);
var channel = null;
var wordleRoll = null;
for (const [guildKey, guild] of client.guilds.cache) {
for (const [channelKey, cachedChannel] of guild.channels.cache) {
if (channelKey == settings.channelToSendTo) {
channel = cachedChannel;
}
}
for (const [roleKey, role] of guild.roles.cache) {
if (role.name == 'wordle') {
wordleRoll = role.id;
}
}
}
if (channel === null) {
console.log('Cannot find channel described in settings!');
} else {
if (isFromScheduler) {
const offsetTime = 86400000; // One day in milliseconds
settings.timeToSendMessage += offsetTime;
writeFile(SETTINGS_FILE_PATH, JSON.stringify(settings, null, '\t'), succeeded => {
if (succeeded) {
scheduleReminder(client);
}
});
}
sendLeaderboardMessage(channel, `New <@&${wordleRoll}> just dropped!`);
}
})
};
function updateSavedMessageLeaderboard(leaderboard) {
if (savedMessage !== null) {
const editedEmbed = new MessageEmbed(savedMessage.embeds[0]);
editedEmbed.setFields(getMessageFields(leaderboard));
savedMessage.edit({ embeds: [editedEmbed] }).then(editedMessage => {
savedMessage = editedMessage;
});
}
}
function sendLeaderboardMessage(channel, messageContent) {
readInFile(LEADERBOARD_FILE_PATH, data => {
const leaderboard = JSON.parse(data);
const messageToSend = new MessageEmbed()
.setTitle('β¬β¬π¨π© Click me to log your result! π©π¨β¬β¬')
.setURL('https://www.powerlanguage.co.uk/wordle/')
.setColor('0x91f59e')
.setDescription('Current leaderboard:')
.setFields(getMessageFields(leaderboard));
var message = null;
if (messageContent == null) {
message = { embeds: [messageToSend] }
} else {
message = { content: messageContent, embeds: [messageToSend] }
}
channel.send(message)
.then(embededMessage => {
savedMessage = embededMessage;
});
});
}
exports.scheduleReminder = scheduleReminder;
exports.remind = remind;
exports.updateSavedMessageLeaderboard = updateSavedMessageLeaderboard;
exports.sendLeaderboardMessage = sendLeaderboardMessage;
// Helpers
function getSortedLeaderboard(leaderboard_json) {
var values = [];
Object.entries(leaderboard_json).forEach(function([key, scores]) {
total = 0;
for (const value of scores) {
total += value;
}
values.push({ "user": `<@${key}>`, "total": total, "average": total / scores.length })
});
values.sort(function(left, right) {
return right.total - left.total
})
return values;
}
function getMessageFields(leaderboard) {
var nameList = '';
var averageList = '';
var totalList = '';
var numberOfItems = 0;
var sumOfAll = 0;
for (const entry of getSortedLeaderboard(leaderboard)) {
nameList += `${entry.user}\n`;
averageList += `${entry.average}\n`;
totalList += `${entry.total}\n`;
numberOfItems += entry.total / entry.average;
sumOfAll += entry.total;
}
return [
{ name: 'π Who', value: nameList, inline: true },
{ name: 'π Average', value: averageList, inline: true },
{ name: 'π― Total', value: totalList, inline: true },
{ name: 'Attempts', value: `${numberOfItems}`, inline: true },
{ name: 'Overall Average', value: `${sumOfAll / numberOfItems}`, inline: true },
{ name: 'Sum', value: `${sumOfAll}`, inline: true },
];
}