Send the user the key, then remove it from the pool, catch any errors.Īwait (`Here's your key: ||$||`) įs.writeFileSync('./keys.json', JSON.stringify(keys)) Grab the first key (no need to randomize). Return await (':x: Sorry, no keys are available to claim right now.') If (keys) return await (':x: You already claimed a key.') Ensure the user hasn't claimed a key already. Inside command: /* Async context needed for 'await' (meaning within an async function). If you want to allow only one key per user (or implement a cooldown) you can store users and their key.Ī real database is a much better option than a JSON file, but your possible choices are too much to cover in one simple example. This works because a user can receive a key from the unused stash, and once it's removed, it can't be given out again. Maintain a pool of unsent keys and a table of users with the keys they got in a database.