Skip to content

Commit

Permalink
refactor: discord notifications refactor (#425)
Browse files Browse the repository at this point in the history
* add DiscordService, refactor PlayerBansService

* PlayersService refactor

* PlayerSubstitutionService refactor

* PlayerSkillService refactor

* QueueNotificationsService refactor

* get rid of DiscordNotificationsService
  • Loading branch information
garrappachc authored Jun 12, 2020
1 parent c095022 commit acc4d1c
Show file tree
Hide file tree
Showing 31 changed files with 879 additions and 1,019 deletions.
6 changes: 3 additions & 3 deletions __mocks__/discord.js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class TextChannel {
send(message: string) { return Promise.resolve(new Message()); }
}

export const queueChannel = new TextChannel('queue');
export const playersChannel = new TextChannel('players');
export const adminChannel = new TextChannel('admins');

export class Role {
Expand All @@ -30,8 +30,8 @@ export class Guild {

channels = {
cache: new Collection([
[ 'queue', queueChannel ],
[ 'admins', adminChannel ],
[ 'queue', playersChannel ],
[ 'players', adminChannel ],
]),
};

Expand Down
15 changes: 4 additions & 11 deletions src/discord/discord.module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { Module, forwardRef } from '@nestjs/common';
import { DiscordNotificationsService } from './services/discord-notifications.service';
import { PlayersModule } from '@/players/players.module';
import { MessageEmbedFactoryService } from './services/message-embed-factory.service';
import { Module } from '@nestjs/common';
import { DiscordService } from './services/discord.service';

@Module({
imports: [
forwardRef(() => PlayersModule),
],
providers: [
DiscordNotificationsService,
MessageEmbedFactoryService,
DiscordService,
],
exports: [
DiscordNotificationsService,
MessageEmbedFactoryService,
DiscordService,
],
})
export class DiscordModule { }
8 changes: 8 additions & 0 deletions src/discord/notifications/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum Colors {
PlayerBanAdded = '#dc3545',
PlayerBanRevoked = '#9838dc',
NewPlayer = '#33dc7f',
PlayerNameChanged = '#5230dc',
SubstituteRequest = '#ff557f',
SkillChanged = '#ff953e',
}
6 changes: 6 additions & 0 deletions src/discord/notifications/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { playerBanAdded } from './player-ban-added';
export { playerBanRevoked } from './player-ban-revoked';
export { newPlayer } from './new-player';
export { playerNameChanged } from './player-name-changed';
export { substituteRequest } from './substitute-request';
export { skillChanged } from './skill-changed';
22 changes: 22 additions & 0 deletions src/discord/notifications/new-player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { MessageEmbedOptions } from 'discord.js';
import { Colors } from './colors';

interface NewPlayerFields {
name: string;
profileUrl: string;
}

export function newPlayer(fields: NewPlayerFields): MessageEmbedOptions {
return {
color: Colors.NewPlayer,
title: 'New player',
fields: [
{
name: 'Name',
value: fields.name,
},
],
url: fields.profileUrl,
timestamp: new Date(),
};
}
39 changes: 39 additions & 0 deletions src/discord/notifications/player-ban-added.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { MessageEmbedOptions } from 'discord.js';
import { Colors } from './colors';
import moment = require('moment');

interface PlayerBanAddedFields {
admin: string;
player: string;
reason: string;
ends: Date;
playerProfileUrl: string;
}

export function playerBanAdded(fields: PlayerBanAddedFields): MessageEmbedOptions {
const endsText = moment(fields.ends).fromNow();

return {
color: Colors.PlayerBanAdded,
title: 'Ban added',
fields: [
{
name: 'Admin',
value: fields.admin,
},
{
name: 'Player',
value: `[${fields.player}](${fields.playerProfileUrl})`,
},
{
name: 'Reason',
value: fields.reason,
},
{
name: 'Ends',
value: endsText,
},
],
timestamp: new Date(),
};
}
26 changes: 26 additions & 0 deletions src/discord/notifications/player-ban-revoked.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { MessageEmbedOptions } from 'discord.js';
import { Colors } from './colors';

interface PlayerBanRevokedFields {
player: string;
reason: string;
playerProfileUrl: string;
}

export function playerBanRevoked(fields: PlayerBanRevokedFields): MessageEmbedOptions {
return {
color: Colors.PlayerBanRevoked,
title: 'Ban revoked',
fields: [
{
name: 'Player',
value: `[${fields.player}](${fields.playerProfileUrl})`,
},
{
name: 'Reason',
value: fields.reason,
},
],
timestamp: new Date(),
};
}
27 changes: 27 additions & 0 deletions src/discord/notifications/player-name-changed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { MessageEmbedOptions } from 'discord.js';
import { Colors } from './colors';

interface PlayerNameChangedFields {
oldName: string;
newName: string;
profileUrl: string;
}

export function playerNameChanged(fields: PlayerNameChangedFields): MessageEmbedOptions {
return {
color: Colors.PlayerNameChanged,
title: 'Player name changed',
fields: [
{
name: 'Old name',
value: fields.oldName,
},
{
name: 'New name',
value: fields.newName,
},
],
url: fields.profileUrl,
timestamp: new Date(),
};
}
34 changes: 34 additions & 0 deletions src/discord/notifications/skill-changed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { MessageEmbedOptions } from 'discord.js';
import { Colors } from './colors';

interface SkillChangedFields {
playerName: string;
oldSkill: Map<string, number>;
newSkill: Map<string, number>;
playerProfileUrl: string;
}

export function skillChanged(fields: SkillChangedFields): MessageEmbedOptions {
const embed: MessageEmbedOptions = {
color: Colors.SkillChanged,
title: 'Player\s skill has been updated',
fields: [
{
name: 'Player name',
value: `[${fields.playerName}](${fields.playerProfileUrl})`,
},
],
timestamp: new Date(),
};

for (const key of fields.newSkill.keys()) {
const newSkillValue = fields.newSkill.get(key);
const oldSkillValue = fields.oldSkill.get(key) || 1;

if (newSkillValue !== oldSkillValue) {
embed.fields.push({ name: key, value: `${oldSkillValue} => ${newSkillValue}` });
}
}

return embed;
}
32 changes: 32 additions & 0 deletions src/discord/notifications/substitute-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { MessageEmbedOptions } from 'discord.js';
import { Colors } from './colors';

interface SubstituteRequestFields {
gameNumber: number;
gameClass: string;
team: string;
gameUrl: string;
}

export function substituteRequest(fields: SubstituteRequestFields): MessageEmbedOptions {
return {
color: Colors.SubstituteRequest,
title: 'A substitute is needed',
fields: [
{
name: 'Game no.',
value: `#${fields.gameNumber}`,
},
{
name: 'Class',
value: fields.gameClass,
},
{
name: 'Team',
value: fields.team,
},
],
url: fields.gameUrl,
timestamp: new Date(),
};
}
26 changes: 26 additions & 0 deletions src/discord/services/__mocks__/discord.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class DiscordService {

playersChannel = {
send: () => Promise.resolve(),
};

adminsChannel = {
send: () => Promise.resolve(),
};

getPlayersChannel() {
return this.playersChannel;
}

getAdminsChannel() {
return this.adminsChannel;
}

findRole(role: string) {
return `&<${role}>`;
}

}
Loading

0 comments on commit acc4d1c

Please sign in to comment.