This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): add new /tricks command to pull from a Bag of Tricks
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
], | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |