this is a random number generator slash command that can generate single numbers, create pairs or assign random matches. useful for managing events where users get assigned to each other like secret santa for example.
/*
run this script in autocode once to create the slash command "/random"
this command allows you to specify the maximum of the random number generation.
if you make options 1 and 2 optional, it defaults to single number generation
from 0 to 100.
examples:
/random max:4 output:single
-> a random number between 0 and 4: 2
/random max:4 output:pairs
-> random pairs from 1 to 4:
1 <-> 3
2 <-> 4
/random max:4 output:assignments
-> random assignments from 1 to 4:
3 -> 4
1 -> 2
2 -> 3
4 -> 1
you can set the minimum for single output rng via the variable below.
negative inputs only work for single random numbers.
*/
let singleRngStart = 0
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let createRngCmd = await lib.keyvalue.store['@0.1.16'].get({
key: `createRngCmd`,
defaultValue: 1
});
if (createRngCmd) {
await lib.discord.commands['@0.0.0'].create({
"name": "random",
"description": "generate random numbers in various ways",
"options": [
{
"type": 4,
"name": "max",
"description": "maximum number for RNG",
"required": true
},
{
"type": 3,
"name": "output",
"description": "what to generate",
"choices": [
{
"name": "single",
"value": "single"
},
{
"name": "pairs",
"value": "pairs"
},
{
"name": "assignments",
"value": "assignments"
}
],
"required": true
}
]
});
console.log(`command created`);
return lib.keyvalue.store['@0.1.16'].set({
key: 'createRngCmd',
value: 0
});
}
let max = context.params.event.data.options[0].value
console.log('max: ' + max);
let type = context.params.event.data.options[1]?.value
let msg = ''
if (type == "assignments"){
let nums = []
for (let i = 0; i < max; i++) {
nums.push(i+1)
}
let repeat = 1
let random = ''
while (repeat == 1) {
repeat = 0
random = ''
let nums1 = nums.slice()
let nums2 = nums.slice()
while (nums1.length > 0) {
let rnd1 = Math.floor(Math.random() * nums1.length)
let rnd2 = Math.floor(Math.random() * nums2.length)
random += '\n' + nums1[rnd1] + ' -> ' + nums2[rnd2]
if (nums2[rnd2] == nums1[rnd1]){
//pairing has to be unique so the calculation starts over
repeat = 1
console.log(random);
console.log(`rerolling`);
break
}
nums1.splice(rnd1,1)
nums2.splice(rnd2,1)
}
}
console.log(random);
msg = `random assignments from 1 to ${max}:${random}`
}
else if (type == "pairs"){
let nums = []
for (let i = 0; i < max; i++) {
nums.push(i+1)
}
let random = ''
while (nums.length > 0) {
let rnd = Math.floor(Math.random() * nums.length)
random += '\n' + nums[rnd] + ' <-> '
nums.splice(rnd,1)
rnd = Math.floor(Math.random() * nums.length)
random += nums[rnd]
nums.splice(rnd,1)
}
console.log(random);
msg = `random pairs from 1 to ${max}:${random}`
}
else{
let rng = Math.random()
let a = singleRngStart
let range = max - a + (max < a ? -1 : (max > a ? 1 : 0))
let random = Math.floor(rng * range + a + (max < a ? 1 : 0))
// if you want a single ~MAGIC~ line of code for copying:
// b = end, a = start
// let random = Math.floor(Math.random()*(b-a+(ba?1:0)))+a+(b