Skip to content

Commit

Permalink
feat: add bet to coin command
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilhermeasper committed Mar 30, 2024
1 parent 61b083f commit 32e580c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
57 changes: 52 additions & 5 deletions src/bot/commands/slashCommands/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,43 @@ import {
AttachmentBuilder,
Embed,
EmbedBuilder,
CommandInteractionOptionResolver,
} from 'discord.js';

import { FlipCoinResult, SlashCommand } from '@marquinhos/types';
import { sleep } from '@marquinhos/utils/sleep';
import { join } from 'path';

export const coin: SlashCommand = {
command: new SlashCommandBuilder()
.setName('moeda')
.setDescription('Tiro cara ou coroa numa moeda semi-viciada.'),
.setDescription('Tiro cara ou coroa numa moeda semi-viciada.')
.addSubcommand((subcommand) =>
subcommand
.setName('apostar')
.setDescription('Aposta no cara ou coroa')
.addStringOption((option) =>
option
.setName('escolha')
.setDescription('Escolha entre cara ou coroa')
.setRequired(true)
.addChoices(
{ name: 'Cara', value: 'Cara' },
{ name: 'Coroa', value: 'Coroa' }
)
)
)
.addSubcommand((subcommand) =>
subcommand.setName('lançar').setDescription('Apenas lança a moeda')
),
execute: async (interaction) => {
await interaction.deferReply();
await interaction.followUp('Lançando a moeda... 🪙');

const flipCoinResult = await flipCoin();
const subcommand = (
interaction.options as CommandInteractionOptionResolver
).getSubcommand();

if (flipCoinResult.result === null) {
return interaction.reply({
Expand All @@ -26,14 +49,38 @@ export const coin: SlashCommand = {
}

const attachment = new AttachmentBuilder(
`./src/resources/images/coin_${flipCoinResult.result}.png`
join(
process.env?.ROOT_DIR ?? '.',
`/resources/images/coin_${flipCoinResult.result}.png`
)
);

const coinEmbed = buildEmbed(
interaction.client.baseEmbed(),
flipCoinResult
);

if (subcommand === 'apostar') {
const choice = interaction.options.get('escolha')?.value as string;
if (choice === flipCoinResult.result) {
coinEmbed.setDescription(
`Boa! Você acertou!\n\nForam feitas ${
flipCoinResult.count
} tentativas durante ${(flipCoinResult.elapsedTime / 1000).toFixed(
2
)}s.`
);
} else {
coinEmbed.setDescription(
`Ih! Você errou!\n\nForam feitas ${
flipCoinResult.count
} tentativas durante ${(flipCoinResult.elapsedTime / 1000).toFixed(
2
)}s.`
);
}
}

await interaction.editReply({
content: '',
embeds: [coinEmbed],
Expand Down Expand Up @@ -77,9 +124,9 @@ function buildEmbed(
.setTimestamp()
.setTitle(`Deu ${flipCoinResult.result}!`)
.setDescription(
`A moeda caiu após ${(flipCoinResult.elapsedTime / 1000).toFixed(
2
)}s. Foram feitas ${flipCoinResult.count} tentativas.`
`Foram feitas ${flipCoinResult.count} tentativas durante ${(
flipCoinResult.elapsedTime / 1000
).toFixed(2)}s.`
)
.addFields(
{
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { config } from 'dotenv';
import { Bot } from '@marquinhos/bot';
process.env.ROOT_DIR = __dirname;
config();

const marquinhos = new Bot();
Expand Down

0 comments on commit 32e580c

Please sign in to comment.