diff --git a/commands/admin/whitelist.js b/commands/admin/whitelist.js new file mode 100644 index 0000000..1ac8255 --- /dev/null +++ b/commands/admin/whitelist.js @@ -0,0 +1,41 @@ +const sqlite3 = require('sqlite3').verbose(); + +module.exports = { + name: 'whitelist', + description: 'Whitelist different commands that need whitelisting.', + admin: true, + async execute({message, args, prefix}) { + if(args < 2){ + message.channel.send(`You need to supply the command and channel that you want to whitelist\n\`${prefix}whitelist \``); + } + let command = args[0]; + let guild = args[1]; + // TODO: First check if the bot has access to that guild before whitelisting. + const databasePath = 'data/database.db' + const db = new sqlite3.Database(databasePath) + let err = false; + await new Promise((resolve, reject)=>{ + db.run(`INSERT INTO whitelist ( + serverId, + command, + date + ) VALUES (?, ?, ?)`, + [ + guild, + command, + new Date().getTime() + ], function (error) { + if (error) { + console.error(error); + err = true; + resolve(); + }else{ + resolve(); + } + }); + }) + if(!err) + message.channel.send("Command has been whitelisted in this server.") + else message.channel.send("Could not whitelist the server with that command. Check the logs.") + } +}; \ No newline at end of file diff --git a/commands/misc/chat.js b/commands/misc/chat.js new file mode 100644 index 0000000..215890f --- /dev/null +++ b/commands/misc/chat.js @@ -0,0 +1,72 @@ +require("dotenv").config(); +const sqlite3 = require('sqlite3').verbose(); + +module.exports = { + name: 'chat', + description: 'A chat command that uses an LLM to answer your prompts (server must be whitelisted)', + async execute({ message, args }) { + // TODO: Externalize the whitelist checking into a the message function with a variable export. And cache it. + // Have a global collection that gets created on launch from the database, the collection uses the guild ID as a key and has an array of commands that are whitelisted. + // Just write to both the database and this collection when a new command gets whitelisted. + const db = new sqlite3.Database('data/database.db'); + let isWhitelisted = false; + await new Promise((resolve, reject) => { + db.get(`SELECT * FROM whitelist WHERE serverId = ? AND command = ?`, [message.guild.id, this.name], + function (error, row){ + if(error){ + console.error(error); + resolve(""); + }else{ + if(row === undefined){ + resolve(); + }else { + isWhitelisted = true; + resolve(); + } + } + }) + }) + + if(!isWhitelisted){ + message.channel.send("This server is not whitelisted. The bot admin needs to whitelist the server for this command to work."); + return; + } + if(args.length === 0){ + message.channel.send("You have to set your prompt in the arguments"); + return; + } + const prompt = args.join(" "); + let answer = ""; + const initialMessage = await message.channel.send("Generating response... This may take a moment.") + message.channel.sendTyping(); + await fetch(`https://openrouter.ai/api/v1/chat/completions`, { + method: `POST`, + headers: { + "Authorization": `Bearer ${process.env.OPENROUTER_API_KEY}`, + "Content-Type": `application/json` + }, + body: JSON.stringify({ + "model": `deepseek/deepseek-chat-v3-0324:free`, + "messages": [ + { + "role": `user`, + "content": prompt + } + ], + "max_tokens": 100 + }) + }).then(response => response.json()).then(data => { + answer = data.choices[0].message.content; + }).catch(error => { + console.error(error); + }); + if(answer.length > 0){ + if(answer.length > 1999){ + initialMessage.edit("Unfortunately the length of the message was longer than what discord allows.") + } + initialMessage.edit(answer) + }else { + initialMessage.edit("Something went wrong. The owner should check the console.") + } + } +}; \ No newline at end of file diff --git a/server.js b/server.js index 3a4e316..51d2f55 100644 --- a/server.js +++ b/server.js @@ -18,6 +18,8 @@ async function checkAndConvertJSONToSQL(){ } const createDatabaseTables = require('./server/createDatabaseTables'); const createLastfmTable = require('./server/createLastfmTable'); +const createWhitelistTable = require('./server/createWhitelistTable.js') +createWhitelistTable(); createLastfmTable(); checkAndConvertJSONToSQL(); const { Collection, Client, GatewayIntentBits, Partials } = require('discord.js'); diff --git a/server/createWhitelistTable.js b/server/createWhitelistTable.js new file mode 100644 index 0000000..70a37c6 --- /dev/null +++ b/server/createWhitelistTable.js @@ -0,0 +1,22 @@ +const sqlite3 = require('sqlite3').verbose(); +module.exports = async function () { + const db = new sqlite3.Database('data/database.db'); + return new Promise ((resolve, reject)=>{ + db.run( + `CREATE TABLE IF NOT EXISTS whitelist ( + serverId TEXT PRIMARY KEY, + command TEXT), + dateAdded INTEGER`, + (err) => { + if (err) { + console.error(`Error while creating table 'whitelist': ${err}`); + reject(err); + } else { + console.log("Table 'whitelist' created successfully."); + resolve(); + } + db.close(); + } + ); + }) +} \ No newline at end of file