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 table command to roll on a magic item table
- Loading branch information
Showing
6 changed files
with
123 additions
and
4 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
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,85 @@ | ||
import { Client, Guild, Message, TextChannel } from 'discord.js'; | ||
|
||
import { FriendlyError } from '../error'; | ||
import command from './table'; | ||
|
||
const mocks = { | ||
delete: jest.fn(), | ||
reply: jest.fn(), | ||
}; | ||
|
||
jest.mock('discord.js', () => ({ | ||
Client: jest.fn(), | ||
Guild: jest.fn(), | ||
TextChannel: jest.fn(), | ||
Collection: jest.fn(), | ||
Message: jest.fn().mockImplementation(() => ({ | ||
delete: mocks.delete, | ||
reply: mocks.reply, | ||
})), | ||
})); | ||
jest.mock('../data/table', () => ({ | ||
a: new Array(100).fill('foo'), | ||
b: [...new Array(98).fill('bar'), 'test', 'foobar'], | ||
c: new Array(100).fill(() => 'baz'), | ||
})); | ||
|
||
describe('_table configuration', () => { | ||
it('should have basic command infomation', () => { | ||
expect(command.name).toEqual('table'); | ||
expect(command.description).toEqual('Roll on the magic item tables'); | ||
expect(command.usage).toEqual('TABLE [ROLL]'); | ||
}); | ||
|
||
it('should not have an alias', () => { | ||
expect(command.alias).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('_table', () => { | ||
let message: Message; | ||
|
||
beforeEach(() => { | ||
mocks.delete.mockClear(); | ||
mocks.reply.mockClear(); | ||
|
||
const client = new Client(); | ||
const guild = new Guild(client, {}); | ||
const channel = new TextChannel(guild, {}); | ||
message = new Message(client, {}, channel); | ||
}); | ||
|
||
it('returns a value from the given table', async () => { | ||
await command.run(message, { $0: 'table', _: ['a'] }); | ||
|
||
expect(mocks.delete).toBeCalledTimes(1); | ||
expect(mocks.reply).toBeCalledTimes(1); | ||
expect(mocks.reply).toHaveBeenCalledWith('foo'); | ||
}); | ||
|
||
it('returns a value for a dice roll', async () => { | ||
await command.run(message, { $0: 'table', _: ['b', '99'] }); | ||
|
||
expect(mocks.delete).toBeCalledTimes(1); | ||
expect(mocks.reply).toBeCalledTimes(1); | ||
expect(mocks.reply).toHaveBeenCalledWith('test'); | ||
}); | ||
|
||
it('returns a resolved value', async () => { | ||
await command.run(message, { $0: 'table', _: ['C'] }); | ||
|
||
expect(mocks.delete).toBeCalledTimes(1); | ||
expect(mocks.reply).toBeCalledTimes(1); | ||
expect(mocks.reply).toHaveBeenCalledWith('baz'); | ||
}); | ||
|
||
it('throws an error if invalid table given', async () => { | ||
try { | ||
await command.run(message, { $0: 'table', _: [] }); | ||
fail('expected error to be thrown'); | ||
} catch (err) { | ||
expect(err instanceof FriendlyError).toBe(true); | ||
expect(err.message).toEqual('Unknown table "-".'); | ||
} | ||
}); | ||
}); |
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,31 @@ | ||
import { Message } from 'discord.js'; | ||
import { isString } from 'lodash'; | ||
import { Arguments } from 'yargs'; | ||
|
||
import magicItemTable from '../data/table'; | ||
import { FriendlyError } from '../error'; | ||
import { Command } from '../types'; | ||
import { roll } from '../utils'; | ||
|
||
const command: Command = { | ||
name: 'table', | ||
description: 'Roll on the magic item tables', | ||
usage: 'TABLE [ROLL]', | ||
async run(message: Message, args: Arguments) { | ||
const index = (args._[0] || '-').toLowerCase(); | ||
const dice = Number(args._[1] || roll('d100')); | ||
|
||
const table = magicItemTable[index]; | ||
if (!table) { | ||
throw new FriendlyError(`Unknown table "${index}".`); | ||
} | ||
|
||
const item = table[dice - 1]; | ||
|
||
await message.delete(); | ||
|
||
return message.reply(isString(item) ? item : await item()); | ||
}, | ||
}; | ||
|
||
export default command; |
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