-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
211 lines (197 loc) · 6.97 KB
/
app.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
// TODO : maybe make traces of message ids that will then be removed
export const client = new Discord.Client();
client.login(botKey);
const loginLock = {};
const p = new Promise((res, rej) => {
loginLock.res = res;
loginLock.rej = rej;
});
loginLock.p = p;
client.on('ready', () => {
loginLock.res();
console.log('Bot online');
});
mongoose.connect(mongoDbKey, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
(async () => {
await loginLock.p;
await init();
client.on('messageReactionAdd', async (msgReact, user) => {
if (!shouldHandleReact(msgReact, user)) return;
// TODO : Implement filter, right now all messages that are reacted to get pushed through here
const msgInPool = isPartOfPool(msgReact.message.id);
if (!msgInPool) return;
if (isLocked(msgReact, POOLS[msgInPool])) return;
switch (msgInPool) {
case 'IS_REPLAY_POOL': {
switch (msgReact._emoji.name) {
case '✅': {
const ticket = IS_REPLAY_POOL[msgReact.message.id];
delete IS_REPLAY_POOL[msgReact.message.id];
clearTTimeout(ticket);
// TODO : Put this (await ticket.origMsg.delete();) in after the message has been verified that all data is on it.
// await ticket.origMsg.delete();
DATA_FLOW[getRecipId(msgReact)].resolveInd(0);
break;
}
case '🛑': {
const ticket = IS_REPLAY_POOL[msgReact.message.id];
delete IS_REPLAY_POOL[msgReact.message.id];
clearTTimeout(ticket);
DATA_FLOW[getRecipId(msgReact)].abort().rejectAll('Not a replay').remove();
await msgReact.message.channel.send(isNotSC2Replay);
await sleep(10 * 1000);
await delAllMsgs({ DMChannels: msgReact.message.channel });
break;
}
default:
return badEmoji(msgReact);
}
return;
}
case 'DATA_VALIDATION_POOL': {
// TODO : add the functionality to extend timeouts to handleTimeout
let isCorrectEmoji = 0;
for (let key in allEmojis) {
const emoji = allEmojis[key];
isCorrectEmoji |= emoji.id === msgReact._emoji.name;
isCorrectEmoji |= emoji.id === msgReact._emoji.id;
}
if (!isCorrectEmoji) return badEmoji(msgReact);
const ticket = DATA_VALIDATION_POOL[msgReact.message.id];
const group = getActualGroup(msgReact, DATA_VALIDATION_POOL);
await lockEmojiInterWGroup(group, ticket, msgReact);
const hasAllEmojies = hasAllProperties(
DATA_VALIDATION_POOL[msgReact.message.id],
['race', 'rank', 'vsRace']
);
if (hasAllEmojies) {
// TODO : Add datavalidation pool deletion to everywhere a buildticket queuepool is called
clearTTimeout(ticket);
ticket.timedOut = false;
Object.freeze(ticket);
DATA_FLOW[getRecipId(msgReact)].resolveInd(1);
console.log('all emojies were received.');
}
// TODO : Has to have max time that the timeout can be extended.
return;
}
case 'DESCRIPTION_POOL': {
const allowedEmojis = ['✅', '🛑'];
if (!allowedEmojis.includes(emojiFromMsgReact(msgReact)))
return badEmoji(msgReact);
clearTTimeout(DESCRIPTION_POOL[msgReact.message.id]);
await lockEmojiInter(msgReact, DESCRIPTION_POOL[msgReact.message.id]);
return;
}
case 'DASHBOARD_POOL': {
const allowedEmojis = DashEmojis;
if (!allowedEmojis.includes(msgReact.emoji.name)) return badEmoji(msgReact);
await lockEmojiInter(msgReact, DASHBOARD_POOL[msgReact.message.id]);
return;
}
case 'COACHLOG_POOL': {
const allowedEmojis = ['✅', '🛑'];
if (!allowedEmojis.includes(emojiFromMsgReact(msgReact)))
return badEmoji(msgReact);
clearTTimeout(COACHLOG_POOL[msgReact.message.id]);
await lockEmojiInter(msgReact, COACHLOG_POOL[msgReact.message.id]);
return;
}
default:
return console.log(`POOL ${msgInPool} not implemented`);
}
});
client.on('messageReactionRemove', async (msgReact, user) => {
if (!shouldHandleReact(msgReact, user)) return;
const msgInPool = isPartOfPool(msgReact.message.id);
if (!msgInPool) return;
switch (msgInPool) {
case 'DATA_VALIDATION_POOL': {
await freeEmojiInter(msgReact, DATA_VALIDATION_POOL[msgReact.message.id]);
return;
}
case 'DASHBOARD_POOL': {
const allowedEmojis = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '◀', '▶️'];
if (!allowedEmojis.includes(msgReact.emoji.name)) {
return badEmoji(msgReact);
}
await freeEmojiInter(msgReact, DASHBOARD_POOL[msgReact.message.id]);
return;
}
default:
return console.log(
`POOL ${msgInPool} not implemented, or it doesn't have a undo capability.`
);
}
});
client.on('message', async msg => {
if (await isCoachCmd(msg)) {
handleConfigCoach(msg);
return;
}
if (!shouldHandleMsg(msg)) return;
const [hasReplay, url] = getMsgAttachments(msg);
const { playingAgainst, playingAs, rank, replay } = whichDataPresent(msg);
if (!hasReplay) return;
try {
// TODO : aborted should be set to false after timing out any of these handlers but it isn't for handleMissingData
const aborted = await newInterruptRunner({
dataFlowId: msg.author.id,
actions: [
() => handleConfIsReplay(replay, msg, url),
() => handleMissingData(msg, playingAgainst, playingAs, rank, url),
() => handleDescription(msg),
() => handleConfirmation(msg),
],
});
if (aborted) return;
// TODO : put push to database and queuepool in here
await handlePushToCoaches();
} catch (e) {
console.error(new Error(e));
}
});
})();
import init, {
DATA_VALIDATION_POOL,
IS_REPLAY_POOL,
DASHBOARD_POOL,
COACHLOG_POOL,
DESCRIPTION_POOL,
} from './init.js';
import mongoose from 'mongoose';
import Discord from 'discord.js';
import { botKey, mongoDbKey } from './config/keys.js';
import {
freeEmojiInter,
lockEmojiInterWGroup,
getActualGroup,
isLocked,
lockEmojiInter,
emojiFromMsgReact,
} from './utils/emojiInteraction.js';
import {
handleConfirmation,
handlePushToCoaches,
handleMissingData,
handleConfIsReplay,
shouldHandleMsg,
getRecipId,
clearTTimeout,
hasAllProperties,
delAllMsgs,
sleep,
badEmoji,
shouldHandleReact,
handleDescription,
} from './utils/utils.js';
import { whichDataPresent, getMsgAttachments } from './utils/ticket.js';
import { newInterruptRunner } from './utils/interruptRunner.js';
import { handleConfigCoach, isCoachCmd } from './utils/coach.js';
import { isPartOfPool, POOLS } from './utils/pool.js';
import { DATA_FLOW } from './provider/dataFlow.js';
import { allEmojis, DashEmojis } from './Emojis.js';
import { isNotSC2Replay } from './messages.js';