-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
255 lines (235 loc) · 11.1 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { Client, Intents, Permissions, MessageEmbed } from 'discord.js';
import dayjs from 'dayjs';
import { keys } from './keys.js';
import { sleep, log } from './utils.js';
import { mongoConnect, insertGuild, getConfig, initUser, getUser, getRank, getTotalRank, incrUserStreak, clearUserStreak, zeroUserStreak } from './mongo.js';
const discord = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS] });
const nummoji = [
['0️⃣', '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣'],
[
'<:_0:932432735987966013>',
'<:_1:932432736248021022>',
'<:_2:932432736097030204>',
'<:_3:932432735669190657>',
'<:_4:932432735698559037>',
'<:_5:932432735820197989>',
'<:_6:932432735367204974>',
'<:_7:932432734733889617>',
'<:_8:932432735069437993>',
'<:_9:932432734880682014>'
],
[
'<:__0:932432736168333352>',
'<:__1:932432735954436117>',
'<:__2:932432736067657778>',
'<:__3:932432735669219379>',
'<:__4:932432736013144064>',
'<:__5:932432735430139904>',
'<:__6:932432735203647518>',
'<:__7:932432734863908865>',
'<:__8:932432734524170320>',
'<:__9:932432734335426610>'
]
];
const commands = ['!gm', '!gm wen', '!gm rank'];
async function checkTime(userId, now) {
const formerRaw = await getUser(userId).then( (t) => { return t.ts } );
const formerTime = dayjs(formerRaw);
if ((now >= formerTime.add(15,'hours') && now <= formerTime.endOf('day').add(1, 'day'))) {
return 1;
} else if (now > formerTime.endOf('day').add(1, 'day')) {
return -1;
} else {
return 0;
}
}
async function handleUser (userId, username) {
const now = dayjs().valueOf();
if (await getUser(userId) === null) {
return await initUser(userId, username, now).then( () => {
return 1;
});
} else {
const check = await checkTime(userId, now);
if (check === 1) {
return await incrUserStreak(userId, now).then( (user) => {
return user.streak;
});
} else if (check === -1) {
return await clearUserStreak(userId, now).then( () => {
return 1;
});
} else {
return 0;
}
}
}
function numToEmoji (num) {
var emoji = [];
const numString = num.toString();
const numArray = numString.split('');
for (var i = 0; i < numArray.length; i++) {
emoji.push(
nummoji[i][numArray[i]]
);
}
return emoji;
}
discord.on('messageCreate', async m => {
if (!m.author.bot) {
const botHasPermish = m.channel.permissionsFor(m.guild.me).has(
Permissions.FLAGS.READ_MESSAGE_HISTORY&&
Permissions.FLAGS.ADD_REACTIONS&&
Permissions.FLAGS.EMBED_LINKS);
const config = await getConfig(m.guild.id).then( (c) => {
if (c == undefined) { return 0 }
else { return c }
});
const userId = m.author.id;
const username = m.author.tag;
try {
if (m.content.indexOf('!gm setup') === 0) {
try {
if (m.channel.permissionsFor(m.author).has(Permissions.FLAGS.MANAGE_GUILD)) {
var keyword = m.content.split('!gm setup')[1].trim();
if (keyword === '') { keyword = 'gm'; }
await insertGuild(m.guild.name, m.channel.name, m.channel.id, keyword);
log(`New guild added! ${m.guild.name}, ${m.channel.name}, word: ${keyword}`, 'g');
await discord.channels.cache.get(m.channelId).send(`Setup to track ${keyword} in ${m.channel.name}`);
} else {
await discord.channels.cache.get(m.channelId).send('Must be a bot wrangler to perform setup!');
}
} catch (e) {
console.log(e);
return;
}
} else if (m.content === '!gm help') {
if (botHasPermish) {
const message = new MessageEmbed()
.setTitle('👋')
.setDescription(`thanks for adding me!\nmy purpose is to count your gm's\nsay gm once a day to increment your streak\nmiss a day and your streak gets reset :(\nwhen you have successfully gm'ed you'll see reaction emojis with your current streak\nif you haven't waited long enough since your last gm you'll see a ⏰ reaction`)
.addField('other commands',"`!gm` responds with your current streak and running total\n`!gm wen` responds with wen you last said it and wen to say it next\n`!gm rank` displays the current streak leader board\n`!gm setup` setups me up to track in the channel where it is sent\n")
.addField('===========================','gm')
.addField('brought to you by', '[CanuDAO](https://discord.gg/dv7SXUaMKD)');
try {
await discord.channels.cache.get(m.channelId).send({embeds:[message]});
} catch(e) { return }
} else {
log(`Missing permissions in ${m.guild.name}, ${m.channel.name}`);
try {
await discord.channels.cache.get(m.channelId).send("Missing permissions to send help message");
} catch(e) { return }
}
} else if (config !== 0) {
if (m.content.toLowerCase() === config.keyword && config.channelId === m.channel.id) {
const streak = await handleUser(userId, username);
if (streak === 0) {
if (botHasPermish){
try { await m.react('⏰'); } catch(e) { log(`ERROR: clock reaction ${m.guild.name}, ${m.channel.name}\n\t\t\t\t${e}`, 'e')}
} else {
log(`Missing permissions in ${m.guild.name}, ${m.channel.name}`,'e');
try {
await discord.channels.cache.get(m.channelId).send("Missing permissions to react to message");
} catch(e) { return }
}
} else {
const streakmoji = numToEmoji(streak);
for (var i = 0; i < streakmoji.length; i++){
if (botHasPermish){
try { m.react(streakmoji[i]); } catch(e) { log(`ERROR: nummoji reaction ${m.guild.name}, ${m.channel.name}\n\t\t\t\t${e}`, 'e')}
} else {
log(`Missing permissions in ${m.guild.name}, ${m.channel.name}`);
try {
await discord.channels.cache.get(m.channelId).send("Missing permissions to react to message");
} catch(e) { return }
}
}
}
} else if (m.content === '!gm') {
const now = dayjs().valueOf();
const check = await checkTime(userId, now).catch( () => 0);
if (check === -1) zeroUserStreak(userId);
const user = await getUser(userId).then( (u) => {
if (u === null) { return 0 }
else { return u; }
});
if (user === 0) {
try {
await discord.channels.cache.get(m.channelId).send(`gm ${m.author}, you've never said gm, give it a try!`);
} catch(e) { return }
} else {
try {
await discord.channels.cache.get(m.channelId).send(`gm ${m.author}, you have a streak of ${user.streak} and overall have said ${config.keyword} ${user.history.length} times`);
} catch(e) { return }
}
} else if (m.content === '!gm avg') {
const avg = await getUser(userId).then( (t) => {
const history = t.history;
const length = history.length;
let ret = 0;
history.forEach(h => {
const i = dayjs(parseInt(h)).format('HH');
ret = ret + parseInt(i);
})
return ret/length;
});
try {
await discord.channels.cache.get(m.channelId).send(`You usually say ${config.keyword} around ${Math.round(avg)}:00`);
} catch(e) { return }
} else if (m.content === '!gm rank') {
const cutoff = dayjs().subtract(2,'day').valueOf();
const rank = await getRank(cutoff);
(rank[0] == undefined || rank[0].streak === 0) ? rank[0] = ({'username': 'no one', 'streak': 'NA'}) : null;
(rank[1] == undefined || rank[1].streak === 0) ? rank[1] = ({'username': 'no one', 'streak': 'NA'}) : null;
(rank[2] == undefined || rank[2].streak === 0) ? rank[2] = ({'username': 'no one', 'streak': 'NA'}) : null;
(rank[3] == undefined || rank[3].streak === 0) ? rank[3] = ({'username': 'no one', 'streak': 'NA'}) : null;
(rank[4] == undefined || rank[4].streak === 0) ? rank[4] = ({'username': 'no one', 'streak': 'NA'}) : null;
try {
await discord.channels.cache.get(m.channelId).send(
`🥇 ${rank[0].username} -> ${rank[0].streak}\n🥈 ${rank[1].username} -> ${rank[1].streak}\n🥉 ${rank[2].username} -> ${rank[2].streak}\n4️⃣ ${rank[3].username} -> ${rank[3].streak}\n5️⃣ ${rank[4].username} -> ${rank[4].streak}`);
} catch(e) { return }
} else if (m.content === '!gm wen') {
const formerRaw = await getUser(userId).then( (t) => { return t.ts } );
const formerTime = dayjs(formerRaw);
const lower = formerTime.add(15,'hours');
const upper = formerTime.endOf('day').add(1, 'day');
try {
await discord.channels.cache.get(m.channelId).send(`You previously said ${config.keyword} at <t:${formerTime.unix()}>. Say it again after <t:${lower.unix()}> but before <t:${upper.unix()}>`);
} catch(e) { return }
} else if (m.content === '!gm rank total') {
const rank = await getTotalRank();
(rank[0] == undefined || rank[0].historyCount === 0) ? rank[0] = ({'username': 'no one', 'streak': 'NA'}) : null;
(rank[1] == undefined || rank[1].historyCount === 0) ? rank[1] = ({'username': 'no one', 'streak': 'NA'}) : null;
(rank[2] == undefined || rank[2].historyCount === 0) ? rank[2] = ({'username': 'no one', 'streak': 'NA'}) : null;
(rank[3] == undefined || rank[3].historyCount === 0) ? rank[3] = ({'username': 'no one', 'streak': 'NA'}) : null;
(rank[4] == undefined || rank[4].historyCount === 0) ? rank[4] = ({'username': 'no one', 'streak': 'NA'}) : null;
try {
await discord.channels.cache.get(m.channelId).send(
`**All time ranking**:\n🥇 ${rank[0].username} -> ${rank[0].historyCount}\n🥈 ${rank[1].username} -> ${rank[1].historyCount}\n🥉 ${rank[2].username} -> ${rank[2].historyCount}\n4️⃣ ${rank[3].username} -> ${rank[3].historyCount}\n5️⃣ ${rank[4].username} -> ${rank[4].historyCount}`);
} catch(e) { return }
}
} else {
if (commands.indexOf(m.content) > -1) {
try {
await discord.channels.cache.get(m.channelId).send('Do setup with\n```!gm setup```')
} catch(e) { return }
}
}
} catch(e) {
console.log(e)
}
}
});
discord.once('ready', async c => {
log(`Ready! Logged in as ${c.user.tag}`);
});
discord.login(keys.DISCORD_KEY);
await mongoConnect().then( (r) => {
if (r === 1) {
log('MongoDB connected');
return;
} else {
log('Error connecting.');
process.exit();
}
});