-
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: improve coin command randomness and response
- Loading branch information
1 parent
586983f
commit 61b083f
Showing
3 changed files
with
100 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,103 @@ | ||
import { SlashCommandBuilder, AttachmentBuilder } from 'discord.js'; | ||
import { | ||
SlashCommandBuilder, | ||
AttachmentBuilder, | ||
Embed, | ||
EmbedBuilder, | ||
} from 'discord.js'; | ||
|
||
import { SlashCommand } from '@marquinhos/types'; | ||
import { FlipCoinResult, SlashCommand } from '@marquinhos/types'; | ||
import { sleep } from '@marquinhos/utils/sleep'; | ||
|
||
export const coin: SlashCommand = { | ||
command: new SlashCommandBuilder() | ||
.setName('moeda') | ||
.setDescription('Tiro cara ou coroa numa moeda semi-viciada.'), | ||
execute: async (interaction) => { | ||
const result = ['Cara', 'Coroa'][Math.floor(Math.random() * 2)]; | ||
await interaction.deferReply(); | ||
await interaction.followUp('Lançando a moeda... 🪙'); | ||
|
||
const flipCoinResult = await flipCoin(); | ||
|
||
if (flipCoinResult.result === null) { | ||
return interaction.reply({ | ||
content: 'A moeda caiu no ralo e não consegui ver o resultado.', | ||
ephemeral: true, | ||
}); | ||
} | ||
|
||
const attachment = new AttachmentBuilder( | ||
`./src/resources/images/coin_${result}.png`, | ||
{ name: 'result.png' } | ||
`./src/resources/images/coin_${flipCoinResult.result}.png` | ||
); | ||
|
||
const coinEmbed = buildEmbed( | ||
interaction.client.baseEmbed(), | ||
flipCoinResult | ||
); | ||
|
||
interaction.reply({ | ||
await interaction.editReply({ | ||
content: '', | ||
embeds: [coinEmbed], | ||
files: [attachment], | ||
content: `${result}!`, | ||
}); | ||
}, | ||
cooldown: 10, | ||
}; | ||
|
||
async function flipCoin(): Promise<FlipCoinResult> { | ||
const randomTime = Math.floor(Math.random() * 5000); | ||
let finalResult = null; | ||
let heads = 0; | ||
let tails = 0; | ||
let count = 0; | ||
let timeEnd = 0; | ||
|
||
const timeStart = Date.now(); | ||
while (true) { | ||
const result = ['Cara', 'Coroa'][Math.floor(Math.random() * 2)]; | ||
result === 'Cara' ? heads++ : tails++; | ||
|
||
count++; | ||
if (Date.now() - timeStart > randomTime) { | ||
timeEnd = Date.now(); | ||
finalResult = result; | ||
break; | ||
} | ||
await sleep(0); | ||
} | ||
const elapsedTime = timeEnd - timeStart; | ||
|
||
return { result: finalResult, heads, tails, count, elapsedTime }; | ||
} | ||
|
||
function buildEmbed( | ||
baseEmbed: EmbedBuilder, | ||
flipCoinResult: FlipCoinResult | ||
): EmbedBuilder { | ||
return baseEmbed | ||
.setTimestamp() | ||
.setTitle(`Deu ${flipCoinResult.result}!`) | ||
.setDescription( | ||
`A moeda caiu após ${(flipCoinResult.elapsedTime / 1000).toFixed( | ||
2 | ||
)}s. Foram feitas ${flipCoinResult.count} tentativas.` | ||
) | ||
.addFields( | ||
{ | ||
name: 'Cara', | ||
value: `${flipCoinResult.heads}x | ${( | ||
(flipCoinResult.heads / flipCoinResult.count) * | ||
100 | ||
).toFixed(2)}%`, | ||
inline: true, | ||
}, | ||
{ | ||
name: 'Coroa', | ||
value: `${flipCoinResult.tails}x | ${( | ||
(flipCoinResult.tails / flipCoinResult.count) * | ||
100 | ||
).toFixed(2)}%`, | ||
inline: true, | ||
} | ||
) | ||
.setThumbnail(`attachment://coin_${flipCoinResult.result}.png`); | ||
} |
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,3 @@ | ||
export function sleep(ms: number): Promise<void> { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} |