Skip to content

Commit

Permalink
implement roll command
Browse files Browse the repository at this point in the history
  • Loading branch information
soryy708 committed May 27, 2023
1 parent 4b91169 commit f41ba77
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/server/discord/command/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DrawCommand } from './draw';
import { RollCommand } from './roll';

export default [new DrawCommand()];
export default [new DrawCommand(), new RollCommand()];
68 changes: 68 additions & 0 deletions src/server/discord/command/roll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
ApplicationCommandOptionType,
ChatInputCommandInteraction,
} from 'discord.js';
import { DiscordCommand, DiscordCommandOption } from './interface';
import { randomInRange } from '../../entropy';

export class RollCommand implements DiscordCommand {
public name = 'roll';
public description = 'Rolls custom-sided dice';

public getOptions(): Promise<DiscordCommandOption[]> {
return Promise.resolve([
{
type: ApplicationCommandOptionType.Number,
name: 'count',
description: 'How many dice we roll?',
min_value: 1,
},
{
type: ApplicationCommandOptionType.Number,
name: 'sides',
description: 'How many sides to each dice?',
min_value: 1,
},
{
type: ApplicationCommandOptionType.Boolean,
name: 'paranoia',
description:
'If this is about Paranoia, auto-count successes/fails and add computer dice.',
},
]);
}

public async run(interaction: ChatInputCommandInteraction): Promise<void> {
const count = interaction.options.getNumber('count') || 1;
const sides = interaction.options.getNumber('sides') || 2;
const paranoia = interaction.options.getBoolean('paranoia') || false;
const results = [...Array(count).keys()].map(() =>
randomInRange(1, paranoia ? 6 + 1 : sides + 1),
);
if (paranoia) {
const computerDice = randomInRange(1, 6 + 1);
const successes = results.filter((d) => d >= 5);
const failures = results.filter((d) => d < 5);
await interaction.reply(
`${interaction.user.username}: \`${count}D6\` = \`[${results
.sort((a, b) => b - a)
.join(', ')}]\`\n\`${successes.length}\` success${
successes.length !== 1 ? 'es' : ''
} (\`${failures.length}\` failure${
failures.length !== 1 ? 's' : ''
})\nComputer: \`${computerDice}\` ${
computerDice === 6 ? '😈' : '😅'
}`,
);
} else {
const sum = results.reduce((su, cur) => su + cur, 0);
await interaction.reply(
`${
interaction.user.username
}: \`${count}D${sides}\` = \`[${results
.sort((a, b) => b - a)
.join(', ')}]\` sum \`${sum}\``,
);
}
}
}

0 comments on commit f41ba77

Please sign in to comment.