I want to try to store my command cooldown in any sort of storage, I think sqlite will be good, but I don’t know how to implement it for a cooldown.. I was checking one guide here https://anidiots.guide/coding-guides/sqlite-based-points-system/#you-get-points-and-you-get-points-and-everybody-gets-points, it’s for storage points and levels.. Unfortunately I’m not able edit this example into my needs, to store the cooldown for each user.
'use strict';
const SQLite = require("better-sqlite3");
const sql = new SQLite("./cooldowns.sqlite");
const humanizeDuration = require('humanize-duration');
// Require the necessary discord.js classes
const { Client, Intents, Collection } = require('discord.js');
const config = require("./config.json");
const { MessageEmbed } = require('discord.js');
const cooldowns = new Map();
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.on('ready', () => {
const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';").get();
if (!table['count(*)']) {
// If the table isn't there, create it and setup the database correctly.
sql.prepare("CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER);").run();
// Ensure that the "id" row is always unique and indexed.
sql.prepare("CREATE UNIQUE INDEX idx_scores_id ON scores (id);").run();
sql.pragma("synchronous = 1");
sql.pragma("journal_mode = wal");
}
// And then we have two prepared statements to get and set the score data.
client.getCooldown = sql.prepare("SELECT * FROM scores WHERE user = ? AND guild = ?");
client.setCooldown = sql.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points, level) VALUES (@id, @user, @guild, @points, @level);");
client.user.setActivity("thinking...", { type: 'PLAYING' });
console.log('Bot is online!')
});
client.on("messageCreate", message => {
if (message.author.bot) return;
// This is where we'll put our code.
if (message.content.indexOf(config.prefix) !== 0) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'mycommand') {
const cooldown = cooldowns.getCooldown(message.author.id);
if (!cooldown) {
cooldowns = {
id: `${message.author.id}`,
user: message.author.id,
cooldown: 0
}
}
if (cooldown) {
const remaining = humanizeDuration(cooldown - Date.now(), { round: true }, { units: ["d","h","m","s"] });
message.reply(`Your cooldown is. ${remaining}`)
return;
}
async function main() {
const messages = await message.channel.messages.fetch({ limit: 1 });
const lastMessage = messages.last();
const isValidDate = (dateString) => new Date(dateString).toString() !== 'Invalid Date'
if(isValidDate(lastMessage.content) == false && lastMessage.content !== 'mycommand')
message.reply("I need your date of birth!.")
if(lastMessage.content === 'mycommand')
message.reply("Please provide me your date of birth.")
if(isValidDate(lastMessage.content) == true && lastMessage.content !== 'mycommand') {
cooldowns.set(message.author.id, Date.now() + 604800000);
message.reply("Check your DMs.")
message.react("emoji"); //react with emoji to the issued command
const predictions = ["Prediction 1", "Prediction 2", "Prediction 3", "Prediction 4", "Prediction 5", "Prediction 6", "Prediction 7"]
const randomprediction = predictions[Math.floor(Math.random() * predictions.length)];
const prediction1 = new MessageEmbed()
.setColor('#ff7518')
.setAuthor(client.user.username, client.user.displayAvatarURL())
.setTitle(`Weekly message` + lastMessage.author.username + `#` + lastMessage.author.discriminator)
.setDescription(randomprediction);
message.author.send({ embeds: [prediction1] });
var random = Math.random()
if (random < 0.9) {
message.author.send("Congrats! You received this message " + '<@' + message.author.id + '>!')
message.channel.send('Congrats again! ' + '<@' + message.author.id + '>');
}
setTimeout(() => cooldowns.delete(message.author.id), 604800000);
client.setCooldown.run(cooldowns);
}
}
}
The problem is I don’t know how to edit the sql params, I never used sqlite/anything else before and have 0 experience with it. All I want is to store command in some sort of storage, but I don’t know where to start. Any help will be much appreciated!