58 lines
2.4 KiB
JavaScript
58 lines
2.4 KiB
JavaScript
require("dotenv").config(); // import environment variables
|
|
const colors = require("colors");
|
|
const Discord = require("discord.js");
|
|
const client = new Discord.Client({
|
|
intents: [
|
|
"DirectMessages"
|
|
]});
|
|
const sqlite3 = require("sqlite3").verbose();
|
|
const db = new sqlite3.Database(process.env.DB_PATH || "./database.db", (err) => {
|
|
if (err) {
|
|
console.error(`${colors.red(`[ERROR]`)} ${colors.yellow(`Failed to connect to the database:`)} ${err.message}`);
|
|
} else {
|
|
console.log(`${colors.cyan(`[INFO]`)} ${colors.green(`Connected to the database successfully.`)}`);
|
|
db.run(`CREATE TABLE IF NOT EXISTS glucose_log (
|
|
id TEXT PRIMARY KEY NOT NULL,
|
|
user_id TEXT NOT NULL,
|
|
timestamp TEXT NOT NULL,
|
|
reading INTEGER NOT NULL,
|
|
notes TEXT DEFAULT '',
|
|
msg_id TEXT DEFAULT NULL
|
|
)`, (err) => {
|
|
if (err) {
|
|
console.error(`${colors.red(`[ERROR]`)} ${colors.yellow(`Failed to create glucose_log table:`)} ${err.message}`);
|
|
} else {
|
|
console.log(`${colors.cyan(`[INFO]`)} ${colors.green(`glucose_log table is ready.`)}`);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
client.once("ready", () => {
|
|
console.log(`${colors.cyan(`[INFO]`)} ${colors.green(`Bot is online! Logged in as ${client.user.displayName} (${client.user.id})`)}!`);
|
|
require("./modules/registerCommands.js")(client);
|
|
});
|
|
|
|
client.on("interactionCreate", async (interaction) => {
|
|
if (!interaction.isCommand()) return;
|
|
|
|
const commandName = interaction.commandName;
|
|
const command = require(`./modules/commands/${commandName}.js`);
|
|
|
|
if (command && command.execute) {
|
|
try {
|
|
await command.execute(interaction, client, db);
|
|
} catch (error) {
|
|
console.error(`${colors.red(`[ERROR]`)} ${colors.yellow(`Error executing command ${commandName}:`)} ${error}`);
|
|
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
|
|
}
|
|
} else {
|
|
console.warn(`${colors.yellow(`[WARN]`)} ${colors.red(`Command ${commandName} not found or not executable.`)}`);
|
|
}
|
|
});
|
|
|
|
client.login(process.env.TOKEN).then(() => {
|
|
console.log(`${colors.cyan(`[INFO]`)} ${colors.green(`Logged in successfully!`)} Bot is ready to receive commands.`);
|
|
}).catch(err => {
|
|
console.error(`${colors.red(`[ERROR]`)} ${colors.yellow(`Failed to log in:`)} ${err}`);
|
|
}); |