2022-12-16 18:54:06 -06:00
/ *
* Konpeki Discord Bot - Slash Command Definition File
* lottery . js - Rolls X numbers from 1 - Y and returns them
* /
const { SlashCommandBuilder } = require ( 'discord.js' ) ;
const { randomInt } = require ( 'node:crypto' ) ;
module . exports = {
2023-09-13 01:17:31 -05:00
data : new SlashCommandBuilder ( )
. setName ( 'lottery' )
. setDescription ( 'Rolls a set of numbers you can use for whatever' )
2022-12-16 18:54:06 -06:00
2023-09-13 01:17:31 -05:00
// Get number of numbers to roll
. addNumberOption ( option =>
option . setName ( 'count' )
. setDescription ( 'How many numbers to roll' ) ,
)
2022-12-16 18:54:06 -06:00
2023-09-13 01:17:31 -05:00
// Get max number to roll
. addNumberOption ( option =>
option . setName ( 'max' )
. setDescription ( 'Set the maximum roll' ) ,
)
2022-12-16 18:54:06 -06:00
2023-09-13 01:17:31 -05:00
// Optional Ephemeral check to allow user to choose command results to be shared publicly or private; send to self only if no selection.
. addStringOption ( option =>
option . setName ( 'ephemeral' )
. setDescription ( 'Post the avatar in the current channel' )
. addChoices (
{ name : 'Send to me only' , value : 'true' } ,
{ name : 'Send in channel' , value : 'false' } ,
) ) ,
2022-12-16 18:54:06 -06:00
2023-09-13 01:17:31 -05:00
async execute ( interaction ) {
const isEphemeral = interaction . options . getString ( 'ephemeral' ) ? ? 'true' ;
const rollCount = interaction . options . getNumber ( 'count' ) ? ? 6 ;
const maxRoll = interaction . options . getNumber ( 'max' ) ? ? 99 ;
2022-12-16 18:54:06 -06:00
2023-09-13 01:17:31 -05:00
// Limit max number rollable to prevent spam and API issues
if ( maxRoll > 999 ) {
await interaction . reply ( { content : ` Big roller! Unfortunatly ${ maxRoll } is too big a number for me. Try somewhere below 1000! ` , ephemeral : true } ) ;
return ;
}
// Prevent 0 or less numbers
else if ( maxRoll < 1 ) {
await interaction . reply ( { content : ` The fewer numbers, the higher the odds! Unfortunatly ${ maxRoll } is too low a number... Try a positive number under 1000! ` , ephemeral : true } ) ;
return ;
}
2022-12-16 18:54:06 -06:00
2023-09-13 01:17:31 -05:00
// Limit numbers rollable to prevent spam and API issues
if ( rollCount > 9 ) {
await interaction . reply ( { content : ` You want ${ rollCount } numbers?! That's a bit much, even for me... \n Try 9 or less please! ` , ephemeral : true } ) ;
return ;
}
// Prevent 0 or less numbers
else if ( rollCount < 1 ) {
await interaction . reply ( { content : ` It's kinda difficult to roll ${ rollCount } numbers... \n Try a positive single digit number! ` , ephemeral : true } ) ;
return ;
}
2022-12-16 18:54:06 -06:00
2023-09-13 01:17:31 -05:00
const lotteryNumbers = [ ] ;
2022-12-16 18:54:06 -06:00
2023-09-13 01:17:31 -05:00
for ( let i = 0 ; i < rollCount ; i ++ ) {
lotteryNumbers . push ( randomInt ( 1 , maxRoll ) ) ;
}
2022-12-16 18:54:06 -06:00
2023-09-13 01:17:31 -05:00
if ( rollCount === 1 ) {
await interaction . reply ( { content : ` Your lucky number is... \n ${ lotteryNumbers . toString ( ) . split ( ',' ) . join ( ', ' ) } . ` , ephemeral : ( isEphemeral === 'true' ) } ) ;
}
else {
await interaction . reply ( { content : ` Your lucky numbers are... \n ${ lotteryNumbers . toString ( ) . split ( ',' ) . join ( ', ' ) } . ` , ephemeral : ( isEphemeral === 'true' ) } ) ;
}
} ,
2022-12-16 18:54:06 -06:00
} ;