From 7d16216081d872619dec657b9dd06d7556d805e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Garapich?= Date: Fri, 12 Jun 2020 11:07:15 +0200 Subject: [PATCH] feat(discord): delete substitute request msgsWhen the substitute request is canceled or resolved delete the discordannouncement. --- .../services/__mocks__/discord.service.ts | 8 ++++-- .../player-substitution.service.spec.ts | 26 +++++++++++++++++++ .../services/player-substitution.service.ts | 18 ++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/discord/services/__mocks__/discord.service.ts b/src/discord/services/__mocks__/discord.service.ts index 8140a5732..243de4445 100644 --- a/src/discord/services/__mocks__/discord.service.ts +++ b/src/discord/services/__mocks__/discord.service.ts @@ -1,14 +1,18 @@ import { Injectable } from '@nestjs/common'; +class Message { + delete() { return Promise.resolve(this); } +} + @Injectable() export class DiscordService { playersChannel = { - send: () => Promise.resolve(), + send: () => Promise.resolve(new Message()), }; adminsChannel = { - send: () => Promise.resolve(), + send: () => Promise.resolve(new Message()), }; getPlayersChannel() { diff --git a/src/games/services/player-substitution.service.spec.ts b/src/games/services/player-substitution.service.spec.ts index 3240cb3c3..cdf5ee5a1 100644 --- a/src/games/services/player-substitution.service.spec.ts +++ b/src/games/services/player-substitution.service.spec.ts @@ -180,7 +180,14 @@ describe('PlayerSubstitutionService', () => { }); describe('#cancelSubstitutionRequest()', () => { + let discordMessage: any; + const channel = { send: () => discordMessage }; + beforeEach(async () => { + discordMessage = await discordService.getPlayersChannel().send({ }); + // @ts-expect-error + discordService.getPlayersChannel = () => channel; + await service.substitutePlayer(mockGame.id, player1.id); }); @@ -239,10 +246,23 @@ describe('PlayerSubstitutionService', () => { await service.cancelSubstitutionRequest(mockGame.id, player1.id); expect(spy).toHaveBeenCalled(); }); + + it('should get rid of discord announcement', async () => { + const spy = jest.spyOn(discordMessage, 'delete'); + await service.cancelSubstitutionRequest(mockGame.id, player1.id); + expect(spy).toHaveBeenCalled(); + }); }); describe('#replacePlayer()', () => { + let discordMessage: any; + const channel = { send: () => discordMessage }; + beforeEach(async () => { + discordMessage = await discordService.getPlayersChannel().send({ }); + // @ts-expect-error + discordService.getPlayersChannel = () => channel; + await service.substitutePlayer(mockGame.id, player1.id); }); @@ -346,5 +366,11 @@ describe('PlayerSubstitutionService', () => { await service.replacePlayer(mockGame.id, player1.id, player3.id); expect(spy).toHaveBeenCalledWith(mockGame.gameServer.toString(), 'fake_player_3 is replacing fake_player_1 on soldier.'); }); + + it('should delete the discord announcement', async () => { + const spy = jest.spyOn(discordMessage, 'delete'); + await service.replacePlayer(mockGame.id, player1.id, player3.id); + expect(spy).toHaveBeenCalled(); + }); }); }); diff --git a/src/games/services/player-substitution.service.ts b/src/games/services/player-substitution.service.ts index 3ad458b36..29d035505 100644 --- a/src/games/services/player-substitution.service.ts +++ b/src/games/services/player-substitution.service.ts @@ -10,6 +10,7 @@ import { QueueService } from '@/queue/services/queue.service'; import { DiscordService } from '@/discord/services/discord.service'; import { substituteRequest } from '@/discord/notifications'; import { Environment } from '@/environment/environment'; +import { Message } from 'discord.js'; /** * A service that handles player substitution logic. @@ -21,6 +22,7 @@ import { Environment } from '@/environment/environment'; export class PlayerSubstitutionService { private logger = new Logger(PlayerSubstitutionService.name); + private discordNotifications = new Map(); // playerId <-> message pairs constructor( @Inject(forwardRef(() => GamesService)) private gamesService: GamesService, @@ -57,12 +59,13 @@ export class PlayerSubstitutionService { this.gamesGateway.emitGameUpdated(game); this.queueGateway.updateSubstituteRequests(); - this.discordService.getPlayersChannel().send(substituteRequest({ + const message = await this.discordService.getPlayersChannel()?.send(substituteRequest({ gameNumber: game.number, gameClass: slot.gameClass, team: game.teams.get(slot.teamId), gameUrl: `${this.environment.clientUrl}/game/${game.id}`, })); + this.discordNotifications.set(playerId, message); return game; } @@ -89,6 +92,13 @@ export class PlayerSubstitutionService { await game.save(); this.gamesGateway.emitGameUpdated(game); this.queueGateway.updateSubstituteRequests(); + + const message = this.discordNotifications.get(playerId); + if (message) { + await message.delete({ reason: 'substitution request canceled' }); + this.discordNotifications.delete(playerId); + } + return game; } @@ -156,6 +166,12 @@ export class PlayerSubstitutionService { `${replacement.name} is replacing ${replacee.name} on ${replacementSlot.gameClass}.`, ); + const message = this.discordNotifications.get(replaceeId); + if (message) { + await message.delete(); + this.discordNotifications.delete(replaceeId); + } + this.logger.verbose(`player ${replacement.name} is replacing ${replacee.name} on ${replacementSlot.gameClass} in game #${game.number}`); setImmediate(() => this.gameRuntimeService.replacePlayer(game.id, replaceeId, replacementSlot));