generated from Lioness100/sapphire-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trackEnd.ts
67 lines (55 loc) · 2.16 KB
/
trackEnd.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import type { IncomingEventTrackEndPayload } from '@skyra/audio';
import { BrandingColors } from '#utils/constants';
import { GameEndReason } from '#game/Game';
import { ApplyOptions } from '@sapphire/decorators';
import { createEmbed } from '#utils/responses';
import { Listener } from '@sapphire/framework';
@ApplyOptions<Listener.Options>({ event: 'TrackEndEvent' })
export class UserAudioListener extends Listener {
public async run(payload: IncomingEventTrackEndPayload) {
// Only proceed if the track wasn't stopped because the player had
// stopped, or because another track started playing while the current
// had not finished.
if (['REPLACED', 'STOPPED'].includes(payload.reason)) {
return;
}
const game = this.container.games.get(payload.guildId);
if (!game) {
return;
}
const guesser = game.guesserThisRound;
let streak: number | null = null;
// If there is no guesser, it means the track ended because time ran out.
if (guesser) {
// TODO: give multiple people streaks and points for `AcceptedAnswer.Both`
game.leaderboard.inc(guesser.id);
streak = game.streaks.inc(guesser.id);
}
const { tracksPlayed, playlistLength } = game.queue;
let footerText = `${tracksPlayed}/${playlistLength}`;
if (streak ?? 0 > 2) {
footerText += ` • ${guesser!.tag} has a streak of ${streak} 🔥`;
}
if (game.goal) {
footerText += ` • Playing to ${game.goal} points`;
}
const { title, author } = game.queue.currentlyPlaying!;
const embed = createEmbed(guesser ? `${guesser} guessed it! 🎉` : 'Nobody got it! 🙁', BrandingColors.Secondary)
.setTitle(`That was "${title}" by ${author}`)
.addField('Leaderboard', game.leaderboard.compute())
.setFooter({ text: footerText });
await game.textChannel.send({ embeds: [embed] });
game.players.forEach((player) => {
// If the player is still in the voice channel (this property is
// turned to undefined when they leave).
if (player.lastGameEntryTime) {
player.songsListenedTo++;
}
});
const points = game.leaderboard.leader?.[1];
if (points && game.goal && points === game.goal) {
await game.end(GameEndReason.GoalMet);
}
await game.queue.next();
}
}