Last active
September 21, 2022 08:16
-
-
Save EvolutionX-10/9c26411f678507d939038804d9fd402e to your computer and use it in GitHub Desktop.
Highlow game for discord bot, following my discordbot template
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
import { CommandType } from '#lib/enums'; | |
import { Command } from '#lib/structures'; | |
import { | |
ActionRowBuilder, | |
APIButtonComponentWithCustomId, | |
ButtonBuilder, | |
ButtonInteraction, | |
ButtonStyle, | |
ComponentType, | |
} from 'discord.js'; | |
export default new Command({ | |
type: CommandType.ChatInput, | |
description: 'high low or same?', | |
async commandRun(interaction) { | |
// create a random number | |
const randomNumber = Math.floor(Math.random() * 100) + 1; | |
// create a hint random number | |
const hintNumber = Math.floor(Math.random() * 100) + 1; | |
const buttons = ['High', 'Same', 'Low'].map((b) => | |
new ButtonBuilder() | |
.setCustomId(b.toLowerCase()) | |
.setLabel(b) | |
.setStyle(ButtonStyle.Secondary) | |
); | |
const sent = await interaction.reply({ | |
content: `Is my number higher or lower than ${hintNumber}?`, | |
components: [ | |
new ActionRowBuilder<ButtonBuilder>().addComponents(buttons), | |
], | |
}); | |
const collector = sent.createMessageComponentCollector({ | |
filter: (i) => i.user.id === interaction.user.id, | |
time: 30000, | |
max: 1, | |
componentType: ComponentType.Button, | |
}); | |
const content = (correct: boolean) => | |
correct | |
? `You guessed the number! It was ${randomNumber}` | |
: `Sadge, you got it wrong! It was ${randomNumber}`; | |
const colorButton = (id: string, win: boolean) => { | |
buttons.forEach((b) => { | |
if ((b.data as APIButtonComponentWithCustomId).custom_id !== id) return; | |
b.setStyle(win ? ButtonStyle.Success : ButtonStyle.Danger); | |
}); | |
}; | |
const computeResults = (i: ButtonInteraction) => { | |
const winConditions = | |
(randomNumber > hintNumber && i.customId === 'high') || | |
(randomNumber === hintNumber && i.customId === 'same') || | |
(randomNumber < hintNumber && i.customId === 'low'); | |
colorButton(i.customId, winConditions); | |
return i.update({ | |
content: content(winConditions), | |
components: [ | |
new ActionRowBuilder<ButtonBuilder>().addComponents(buttons), | |
], | |
}); | |
}; | |
buttons.forEach((b) => b.setDisabled(true)); | |
collector.on('collect', async (i) => { | |
await computeResults(i); | |
}); | |
collector.on('end', async (collected) => { | |
if (collected.size !== 0) return; | |
await interaction.editReply({ | |
content: `You didn't guess the number! It was ${randomNumber}`, | |
components: [ | |
new ActionRowBuilder<ButtonBuilder>().addComponents(buttons), | |
], | |
}); | |
}); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment