* Use a primary autoincrementing primary key because primary keys need to be unique * Cache whitelist on bot startup / only read from database once * "Externalize" whitelist checking to the messageCreate function.
This commit is contained in:
parent
c9d7a54e25
commit
a066fd0662
6 changed files with 81 additions and 71 deletions
|
@ -2,15 +2,22 @@ const sqlite3 = require('sqlite3').verbose();
|
|||
|
||||
module.exports = {
|
||||
name: 'whitelist',
|
||||
description: 'Whitelist different commands that need whitelisting.',
|
||||
description: 'Whitelist a command in a specific server.',
|
||||
admin: true,
|
||||
async execute({message, args, prefix}) {
|
||||
if(args < 2){
|
||||
async execute({message, args, prefix, client}) {
|
||||
if(args.length < 2){
|
||||
message.channel.send(`You need to supply the command and channel that you want to whitelist\n\`${prefix}whitelist <command> <server_id>\``);
|
||||
return;
|
||||
}
|
||||
let command = args[0];
|
||||
let guild = args[1];
|
||||
if(guild === "this") guild = message.guild.id;
|
||||
// TODO: Add ability to remove server from whitelist.
|
||||
const whitelistedCommands = client.whitelist.get(guild);
|
||||
if(whitelistedCommands && whitelistedCommands.includes(command)){
|
||||
message.channel.send("Command is already whitelisted in that server.")
|
||||
return;
|
||||
}
|
||||
// TODO: First check if the bot has access to that guild before whitelisting.
|
||||
const databasePath = 'data/database.db'
|
||||
const db = new sqlite3.Database(databasePath)
|
||||
|
@ -35,8 +42,15 @@ module.exports = {
|
|||
}
|
||||
});
|
||||
})
|
||||
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.")
|
||||
if(!err){
|
||||
if(whitelistedCommands){
|
||||
whitelistedCommands.push(command);
|
||||
client.whitelist.set(guild, whitelistedCommands);
|
||||
}else {
|
||||
client.whitelist.set(guild, command);
|
||||
}
|
||||
message.channel.send("Command has been whitelisted in that server.")
|
||||
|
||||
} else message.channel.send("Could not whitelist the server with that command. Check the logs.")
|
||||
}
|
||||
};
|
|
@ -1,36 +1,11 @@
|
|||
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)',
|
||||
needsWhitelist: true,
|
||||
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;
|
||||
|
@ -39,6 +14,7 @@ module.exports = {
|
|||
let answer = "";
|
||||
const initialMessage = await message.channel.send("Generating response... This may take a moment.")
|
||||
message.channel.sendTyping();
|
||||
// TODO: More configuration. Have a basic setup but allow setting system prompt, max tokens and model.
|
||||
await fetch(`https://openrouter.ai/api/v1/chat/completions`, {
|
||||
method: `POST`,
|
||||
headers: {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue