Add a rate limit to discord prefix commands using utils.kv
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
if (context.params.event.content.startsWith('!example')) {
let rateLimit = await lib.utils.kv['@0.1.16'].get({
key: `rate_limit_example_${context.params.event.author.id}`
});
if (rateLimit) {
return lib.discord.channels['@0.1.1'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Sorry, you gotta wait!`
});
} else {
await lib.utils.kv['@0.1.16'].set({
key: `rate_limit_example_${context.params.event.author.id}`,
value: `true`,
ttl: 60 // The number of seconds to rate limit a person for.
});
return lib.discord.channels['@0.1.1'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `This is an example response! Now wait 60 seconds to use this command again. β²`
});
}
}