Skip to content

Commit

Permalink
GH-442: Remove mandatory reaction perm if not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Mar 1, 2023
1 parent 0ec165e commit d7a460c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
28 changes: 20 additions & 8 deletions src/__tests__/GameStateValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,35 @@ describe('GameStateValidator', () => {
});

it.each`
permissions | expected
${[]} | ${false}
${['ADD_REACTIONS']} | ${false}
${GameStateValidator['PERM_LIST']} | ${true}
permissions | expected
${[]} | ${false}
${['ADD_REACTIONS']} | ${false}
${GameStateValidator['PERM_LIST']} | ${false}
${[...GameStateValidator['PERM_LIST'], 'ADD_REACTIONS']} | ${true}
`('should check for member permissions $permissions', ({ permissions, expected }) => {
const spyError = jest.spyOn(global.console, 'error').mockImplementation();
jest.spyOn(tunnel.channel.guild.me!, 'permissionsIn').mockReturnValue(<
Readonly<Permissions>
>{
jest.spyOn(tunnel.channel.guild.me!, 'permissionsIn').mockReturnValue({
has: list => (list as Array<PermissionString>).every(k => permissions.includes(k))
});
} as Readonly<Permissions>);
manager.bot.configuration.gameBoardReactions = true;

expect(validator.isInteractionValid(tunnel)).toBe(expected);
expect(spyError).toHaveBeenCalledTimes(expected ? 0 : 1);
spyError.mockRestore();
});

it('should not need ADD_REACTION perm if do not use reactions', () => {
const hasPermissions = jest.fn();
jest.spyOn(tunnel.channel.guild.me!, 'permissionsIn').mockReturnValue({
has: hasPermissions
} as any);
manager.bot.configuration.gameBoardReactions = false;

validator.isInteractionValid(tunnel);
expect(hasPermissions).toHaveBeenCalledTimes(1);
expect(hasPermissions).toHaveBeenCalledWith(expect.not.arrayContaining(['ADD_REACTIONS']));
});

it.each`
allowedRoleIds | memberRoles | isAdmin | expected
${['R1']} | ${[]} | ${false} | ${false}
Expand Down
21 changes: 13 additions & 8 deletions src/bot/state/GameStateValidator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import MessagingTunnel from '@bot/messaging/MessagingTunnel';
import GameStateManager from '@bot/state/GameStateManager';
import InteractionConfig from '@config/InteractionConfig';
import Config from '@config/Config';
import { GuildMember, PermissionString } from 'discord.js';

/**
Expand All @@ -16,11 +16,15 @@ export default class GameStateValidator {
* @private
*/
private static readonly PERM_LIST: PermissionString[] = [
'ADD_REACTIONS',
'READ_MESSAGE_HISTORY',
'SEND_MESSAGES',
'VIEW_CHANNEL'
]; // bot doesn't need manage message to delete its own message
];
/**
* Permission that the bot needs to add reactions.
* @private
*/
private static readonly PERM_ADD_REACTIONS: PermissionString = 'ADD_REACTIONS';

/**
* Stores configuration of the module.
Expand All @@ -40,7 +44,7 @@ export default class GameStateValidator {
/**
* Retrieves the module configuration.
*/
public get config(): InteractionConfig {
public get config(): Config {
return this.manager.bot.configuration;
}

Expand Down Expand Up @@ -109,11 +113,12 @@ export default class GameStateValidator {
* @private
*/
private hasPermissionsInChannel(tunnel: MessagingTunnel): boolean {
const allowed =
tunnel.channel.guild.me
?.permissionsIn(tunnel.channel)
?.has(GameStateValidator.PERM_LIST) ?? false;
const perms = [...GameStateValidator.PERM_LIST];
if (this.config.gameBoardReactions) {
perms.push(GameStateValidator.PERM_ADD_REACTIONS);
}

const allowed = tunnel.channel.guild.me?.permissionsIn(tunnel.channel)?.has(perms) ?? false;
if (!allowed) {
console.error(
`Cannot operate because of a lack of permissions in the channel #${tunnel.channel.name}`
Expand Down

0 comments on commit d7a460c

Please sign in to comment.