-
Notifications
You must be signed in to change notification settings - Fork 1
/
answer-command.ts
53 lines (48 loc) · 1.98 KB
/
answer-command.ts
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
import {SlackCommandMiddlewareArgs} from '@slack/bolt/dist/types';
import {getCustomRepository} from 'typeorm';
import {Team} from './entities/Team';
import {TeamRepository} from './repositories/TeamRepository';
import {UserRepository} from './repositories/UserRepository';
import {runData} from './run-data';
export const answerCommand = async ({ command, ack, respond, say }: SlackCommandMiddlewareArgs) => {
// Acknowledge command request
await ack();
const answer = command.text.toLowerCase();
if (runData.membersWithCorrectAnswer.includes(command.user_id)) {
await respond(`You have already sent correct answer`);
return;
}
if (!runData.currentAnswer) {
await respond(`There is no quiz running currently`);
return;
}
if (answer !== runData.currentAnswer.toLowerCase()) {
await respond(`Your answer _${command.text}_ is not correct`);
return;
}
const userRepository = getCustomRepository(UserRepository);
const teamRepository = getCustomRepository(TeamRepository);
const user = await userRepository.getBySlackId(command.user_id);
if (!user) {
throw new Error('User not found')
}
const team = await teamRepository.getOneById(user.teamId);
if (!team) {
throw new Error('User not found')
}
const pointsAwarded = runData.currentPoints;
user.score += pointsAwarded;
team.score += pointsAwarded;
userRepository.save(user);
teamRepository.save(team);
runData.currentPoints--;
runData.membersWithCorrectAnswer.push(command.user_id);
await say(`<@${command.user_name}> sent correct answer. ${team.teamName} has been awarded ${pointsAwarded} points`);
if (runData.currentPoints === 5) {
await say(`All points have been taken. Correct answer was ${runData.currentAnswer}.`);
runData.currentAnswer = undefined;
runData.currentQuestion = undefined;
runData.currentPoints = 0;
runData.membersWithCorrectAnswer.length = 0;
}
}