random.ts

1import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js'
2import { Command } from '@/types/bot'
3
4export default {
5	data: new SlashCommandBuilder()
6		.setName('random')
7		.setDescription('Generate a random number')
8		.addIntegerOption((option) =>
9			option
10				.setName('min')
11				.setDescription('Minimum number (default: 1)')
12				.setRequired(false),
13		)
14		.addIntegerOption((option) =>
15			option
16				.setName('max')
17				.setDescription('Maximum number (default: 100)')
18				.setRequired(false),
19		),
20
21	async execute(interaction: ChatInputCommandInteraction) {
22		const min = interaction.options.getInteger('min') ?? 1
23		const max = interaction.options.getInteger('max') ?? 100
24
25		if (min >= max) {
26			await interaction.reply({
27				content: 'The minimum number must be less than the maximum number!',
28				ephemeral: true,
29			})
30			return
31		}
32
33		const result = Math.floor(Math.random() * (max - min + 1)) + min
34		await interaction.reply(
35			`🎲 Random number between ${min} and ${max}: **${result}**`,
36		)
37	},
38} as Command
39