Prefix command with a cooldown. Shows how you can get the actual time remaining in until you can use the command again.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
if (context.params.event.content === '!command') {
let cooldown_time = 60 //Time in seconds
let cooldown = await lib.utils.kv['@0.1.16'].get({
key: `${context.params.event.author.id}_cooldown`,
});
if (cooldown) {
let remaining_time = Math.floor(
parseInt(cooldown + cooldown_time * 1000) - new Date().getTime()
);
let time_sec = Math.floor(remaining_time / 1000);
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: context.params.event.channel_id,
content: `Wait for another \`${time_sec}sec\` before using this command!`,
});
} else {
await lib.utils.kv['@0.1.16'].set({
key: `${context.params.event.author.id}_cooldown`,
value: new Date().getTime(),
ttl: cooldown_time,
});
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: context.params.event.channel_id,
content: `Command used!`,
});
}
}