Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Commit

Permalink
feat(command): add new /tricks command to pull from a Bag of Tricks
Browse files Browse the repository at this point in the history
  • Loading branch information
mloberg committed Sep 8, 2021
1 parent 43e2f95 commit ee2d3ad
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Dwight is a Discord bot to help dungeon masters with their 5e games.
* `/spell` - Return a random spell
* `/table` - Roll on the magic item tables
* `/treasure` - Generate random treasure from the treasure tables
* `/tricks` - Pull from a Bag of Tricks
* `/wildmagic` - Roll on the wild magic table

## Running
Expand Down
38 changes: 38 additions & 0 deletions src/commands/__snapshots__/tricks.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`/tricks is a slash command 1`] = `
Object {
"default_permission": undefined,
"description": "Pull from a Bag of Tricks",
"name": "tricks",
"options": Array [
Object {
"choices": Array [
Object {
"name": "Gray",
"value": "gray",
},
Object {
"name": "Rust",
"value": "rust",
},
Object {
"name": "Tan",
"value": "tan",
},
],
"description": "The color of the Bag of Tricks",
"name": "color",
"required": true,
"type": 3,
},
Object {
"choices": undefined,
"description": "Value of a d8 roll.",
"name": "roll",
"required": false,
"type": 4,
},
],
}
`;
48 changes: 48 additions & 0 deletions src/commands/tricks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { CommandInteraction } from 'discord.js';
import { mocked } from 'ts-jest/utils';

import { FriendlyError } from '../error';
import tricks from './tricks';

jest.mock('discord.js', () => ({
CommandInteraction: jest.fn().mockImplementation(() => ({
reply: jest.fn(),
options: {
getString: jest.fn(),
getInteger: jest.fn(),
},
})),
}));

describe('/tricks', () => {
it('is a slash command', () => {
expect(tricks).toMatchSnapshot();
});

it('returns a creature', async () => {
const command = mocked(new CommandInteraction({} as never, {} as never), true);
command.options.getString.mockReturnValue('rust');

await tricks.handler(command);
expect(command.reply).toHaveBeenCalledWith(expect.any(String));
});

it('returns a create for a dice roll', async () => {
const command = mocked(new CommandInteraction({} as never, {} as never), true);
command.options.getString.mockReturnValue('gray');
command.options.getInteger.mockReturnValue(3);

await tricks.handler(command);
expect(command.reply).toHaveBeenCalledWith('Badger');
});

it('throws an error if no creature is found', async () => {
const command = mocked(new CommandInteraction({} as never, {} as never), true);
command.options.getString.mockReturnValue('tan');
command.options.getInteger.mockReturnValue(20);

await expect(tricks.handler(command)).rejects.toMatchError(
new FriendlyError('Unable to pull from the Bag of Tricks.'),
);
});
});
33 changes: 33 additions & 0 deletions src/commands/tricks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { CommandBuilder, rollOption } from '../command';
import { FriendlyError } from '../error';
import { roll } from '../utils';

const bags = {
gray: ['Weasel', 'Giant rat', 'Badger', 'Boar', 'Panther', 'Giant badger', 'Dire wolf', 'Giant elk'],
rust: ['Rat', 'Owl', 'Mastiff', 'Goat', 'Giant goat', 'Giant boar', 'Lion', 'Brown bear'],
tan: ['Jackal', 'Ape', 'Baboon', 'Axe beak', 'Black bear', 'Giant weasel', 'Giant hyena', 'Tiger'],
};

export default new CommandBuilder(async (command) => {
const color = command.options.getString('color', true) as keyof typeof bags;
const dice = command.options.getInteger('roll') || roll('d8');
const result = bags[color][dice - 1];

if (!result) {
throw new FriendlyError('Unable to pull from the Bag of Tricks.');
}

await command.reply(result);
})
.setName('tricks')
.setDescription('Pull from a Bag of Tricks')
.addStringOption((option) =>
option
.setName('color')
.setDescription('The color of the Bag of Tricks')
.setRequired(true)
.addChoice('Gray', 'gray')
.addChoice('Rust', 'rust')
.addChoice('Tan', 'tan'),
)
.addIntegerOption(rollOption('d8'));

0 comments on commit ee2d3ad

Please sign in to comment.