-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
170 lines (136 loc) · 4.17 KB
/
index.mjs
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
import dotenv from "dotenv";
import Discord from "discord.js";
import schedule from "node-schedule-tz";
import Enmap from "enmap";
import _ from "lodash";
import moment from "moment";
import "moment-timezone";
dotenv.config();
const client = new Discord.Client();
const points = new Enmap({ name: "points" });
const servers = [
{
guild: process.env.SERVER,
channels: [process.env.CHANNEL]
}
];
const { L33T_HOUR, L33T_MINUTE } = process.env;
client.on("ready", () => {
console.log("connected");
setupRecurrentAnnouncement();
});
const pointsSpread = [25, 18, 15, 12, 10, 8, 6, 4, 2, 1];
let roundContestants = [];
client.on("message", message => {
let isInRound = _.find(roundContestants, { author: message.author.id });
if (isL33TTime() && message.content === "1337" && !isInRound) {
let authorPoints = points.get(message.author.id) || 0;
let messagePoints = pointsSpread[Math.min(roundContestants.length, 9)];
roundContestants.push({
author: message.author.id,
roundPoints: messagePoints
});
points.set(message.author.id, messagePoints + authorPoints);
console.log(
`${
message.author.username
} gets ${messagePoints} and has ${messagePoints + authorPoints} total`
);
}
});
function getL33Tmoment() {
return moment()
.tz("Europe/Vienna")
.hours(L33T_HOUR)
.minutes(L33T_MINUTE)
.utc();
}
function isL33TTime() {
let leetTime = getL33Tmoment();
let currentTime = moment().utc();
return (
leetTime.hours() === currentTime.hours() &&
leetTime.minutes() === currentTime.minutes()
);
}
function sendL33Tmessage() {
servers.map(server => {
let guild = client.guilds.get(server.guild);
if (guild) {
server.channels.map(channelId => {
let channel = guild.channels.get(channelId);
if (channel) {
let allPoints = points.fetchEverything();
Promise.all(
allPoints.map(async (_, authorId) => {
try {
return await guild.fetchMember(authorId);
} catch (e) {
return {
id: authorId,
user: { username: "unknown" }
};
}
})
)
.then(users => {
return composeRoundEnd(allPoints, users);
})
.then(embed => {
channel.send({ embed });
console.log(`Hmmm...-ed in ${guild.name} #${channel.name}`);
console.log(`This round ranking:`, roundContestants);
console.log(`Current standings`, points.fetchEverything());
});
}
});
}
});
}
function composeRoundEnd(allPoints, users) {
const embed = new Discord.RichEmbed();
embed.setColor(0xe9c452);
if (roundContestants.length !== 0) {
embed.setTitle("HMM...");
embed.setImage(
"https://media.giphy.com/media/26gYOXsPBh3qv420E/source.gif"
);
let leaderboardString = "";
let sortedPlayers = _.orderBy(
Array.from(allPoints.keys()),
key => allPoints.get(key),
"desc"
);
sortedPlayers.map(player => {
let points = allPoints.get(player);
let userName = _.find(users, { id: player }).user.username;
leaderboardString += `${userName} - ${points}`;
let inContest = _.find(roundContestants, { author: player });
if (inContest) {
leaderboardString += ` (+${inContest.roundPoints})`;
}
leaderboardString += `\n`;
});
embed.addField("Leaderboard", leaderboardString);
} else {
embed.setTitle("HMM... DISQUALIFIED");
embed.setImage("https://media.giphy.com/media/12w45ho280Tg88/giphy.gif");
}
roundContestants = [];
return embed;
}
function setupRecurrentAnnouncement() {
const rule = new schedule.RecurrenceRule();
let leetTime = getL33Tmoment();
rule.hour = leetTime.local().hours() + 1; // daylight savings time hotfix
rule.minute = leetTime.local().minutes() + 1;
schedule.scheduleJob(rule, () => {
console.log("schedule run");
if (points.isReady) {
sendL33Tmessage();
} else {
console.error("Cannot send leet messages because DB is not ready");
}
});
}
client.login(process.env.DISCORD_TOKEN);