This is an example on how you can make a cooldown for a command without using ttl! It will work for any database, googlespread airtable etc. Just replace kv with that database! Change the cooldown in line 9 to any number of seconds you want!
/*This is an example on how you can make a cooldown for a command without using ttl!
It will work for any database, googlespread airtable etc.
Just replace kv with that database!
Change the cooldown in line 9 to any number of seconds you want!
*/
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const convertMS = require('ms-convert');//convert timestamp to normal time
let cooldown = 60; //cooldown (in seconds)
function convertSnowflakeToDate(snowflake) {
return new Date(snowflake / 4194304 + 1420070400000);
}
let snowflake = context.params.event.id;
if (!snowflake) {
return;
}
let timestamp = convertSnowflakeToDate(snowflake);
if (isNaN(timestamp.getTime())) {
return;
}
let timestamp1 = Math.floor(timestamp.getTime() / 1000);
let time = await lib.utils.kv['@0.1.16'].get({
key: `rate_limit_${context.params.event.author.id}`,
defaultValue: timestamp1 + cooldown,
});
if (timestamp1 - time > cooldown) {//if does not have cooldown
await lib.utils.kv['@0.1.16'].set({
key: `rate_limit_${context.params.event.author.id}`,
value: timestamp1,
});
await lib.discord.channels['@0.3.1'].messages.create({
channel_id: context.params.event.channel_id,
content: `You don't have cooldown! π`,
});
} else {//if has cooldown
let time_remaining = `${convertMS((time + cooldown - timestamp1) * 1000)}`;
let remaining = time_remaining.split(':');
await lib.discord.channels['@0.3.1'].messages.create({
channel_id: context.params.event.channel_id,
content: `Woah there! Slow down, you can use the command after **${remaining[0]}h ${remaining[1]}m ${remaining[2]}s**`,
});
}