Skip to content

Commit

Permalink
Use allowedMentions to avoid mentions in messages
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Jan 25, 2022
1 parent d42d30d commit db4d4b3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 41 deletions.
24 changes: 13 additions & 11 deletions src/__tests__/GameBoardBuilder.test.ts
Expand Up @@ -17,26 +17,28 @@ describe('GameBoardBuilder', () => {
});

it('should send empty message by default', () => {
expect(builder.toMessageOptions()).toEqual({ content: '', components: [] });
expect(builder.toMessageOptions()).toEqual({
allowedMentions: { parse: ['users'] },
content: '',
components: []
});
});

it('should compute title based on entity names', () => {
builder.withTitle({ id: '1', displayName: 'entity1' }, { id: '2', displayName: 'entity2' });
expect(builder.toMessageOptions()).toEqual({
content: ':game_die: `entity1` **VS** `entity2`\n\n',
components: []
});
expect(builder.toMessageOptions()).toEqual(
expect.objectContaining({ content: ':game_die: `entity1` **VS** `entity2`\n\n' })
);
});

it('should compute board using custom emojies', () => {
builder
.withEmojies(':dog:', ':cat:')
.withBoard(2, [Player.First, Player.Second, Player.Second, Player.First]);

expect(builder.toMessageOptions()).toEqual({
content: ':dog: :cat: \n:cat: :dog: \n',
components: []
});
expect(builder.toMessageOptions()).toEqual(
expect.objectContaining({ content: ':dog: :cat: \n:cat: :dog: \n' })
);
});

it('should add an empty line between board and state if both defined', () => {
Expand All @@ -51,7 +53,7 @@ describe('GameBoardBuilder', () => {
${{ toString: () => 'fake' }} | ${'fake, select your move:'}
`('should set state based if playing entity is $entity', ({ entity, state }) => {
builder.withEntityPlaying(entity);
expect(builder.toMessageOptions()).toEqual({ content: state, components: [] });
expect(builder.toMessageOptions()).toEqual(expect.objectContaining({ content: state }));
});

it.each`
Expand All @@ -60,6 +62,6 @@ describe('GameBoardBuilder', () => {
${{ toString: () => 'fake' }} | ${':tada: fake has won the game!'}
`('should set state based if winning entity is $entity', ({ entity, state }) => {
builder.withEndingMessage(entity);
expect(builder.toMessageOptions()).toEqual({ content: state, components: [] });
expect(builder.toMessageOptions()).toEqual(expect.objectContaining({ content: state }));
});
});
32 changes: 17 additions & 15 deletions src/bot/entity/DuelRequest.ts
@@ -1,12 +1,11 @@
import MessagingTunnel from '@bot/messaging/MessagingTunnel';
import GameStateManager from '@bot/state/GameStateManager';
import { formatDiscordName } from '@bot/util';
import localize from '@i18n/localize';
import {
Collection,
GuildMember,
Message,
MessageEmbedOptions,
MessageOptions,
MessageReaction,
Snowflake
} from 'discord.js';
Expand Down Expand Up @@ -61,23 +60,28 @@ export default class DuelRequest {
}

/**
* Creates the embed options to send the duel request.
* Creates the message options to send the duel request.
*
* @returns embed options object for the duel message
* @returns message options object for the duel request
*/
public get embed(): MessageEmbedOptions {
public get content(): MessageOptions {
const content =
localize.__('duel.challenge', {
invited: this.invited.toString(),
initier: formatDiscordName(this.tunnel.author.displayName ?? '')
initier: this.tunnel.author.displayName
}) +
'\n' +
localize.__('duel.action');

return {
color: 2719929, // #2980B9
title: localize.__('duel.title'),
description: content
allowedMentions: { parse: [] },
embeds: [
{
color: 2719929, // #2980B9
title: localize.__('duel.title'),
description: content
}
]
};
}

Expand Down Expand Up @@ -119,9 +123,8 @@ export default class DuelRequest {
await this.manager.createGame(this.tunnel, this.invited);
} else {
await this.tunnel.end({
content: localize.__('duel.reject', {
invited: formatDiscordName(this.invited.displayName)
})
allowedMentions: { parse: [] },
content: localize.__('duel.reject', { invited: this.invited.displayName })
});
}
}
Expand All @@ -131,9 +134,8 @@ export default class DuelRequest {
*/
private async challengeExpired(): Promise<void> {
await this.tunnel.end({
content: localize.__('duel.expire', {
invited: formatDiscordName(this.invited.displayName)
})
allowedMentions: { parse: [] },
content: localize.__('duel.expire', { invited: this.invited.displayName })
});
}
}
11 changes: 7 additions & 4 deletions src/bot/entity/GameBoardBuilder.ts
@@ -1,4 +1,3 @@
import { formatDiscordName } from '@bot/util';
import localize from '@i18n/localize';
import AI from '@tictactoe/AI';
import Entity from '@tictactoe/Entity';
Expand Down Expand Up @@ -63,8 +62,8 @@ export default class GameBoardBuilder {
withTitle(player1: Entity, player2: Entity): GameBoardBuilder {
this.title =
localize.__('game.title', {
player1: formatDiscordName(player1.displayName),
player2: formatDiscordName(player2.displayName)
player1: player1.displayName,
player2: player2.displayName
}) + '\n\n';
return this;
}
Expand Down Expand Up @@ -145,6 +144,10 @@ export default class GameBoardBuilder {

// Generate final string
const state = this.state && board ? '\n' + this.state : this.state;
return { content: this.title + board + state, components: [] };
return {
allowedMentions: { parse: ['users'] },
content: this.title + board + state,
components: []
};
}
}
2 changes: 1 addition & 1 deletion src/bot/state/GameStateManager.ts
Expand Up @@ -60,7 +60,7 @@ export default class GameStateManager {
);

// Reply with the duel request and attach the created message
const message = await tunnel.replyWith({ embeds: [duel.embed] });
const message = await tunnel.replyWith(duel.content);
await duel.attachTo(message);

// Setup user cooldown
Expand Down
10 changes: 0 additions & 10 deletions src/bot/util.ts

This file was deleted.

0 comments on commit db4d4b3

Please sign in to comment.