* 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
92dec35da4
6 changed files with 80 additions and 70 deletions
48
server/createAndLoadWhitelistTable.js
Normal file
48
server/createAndLoadWhitelistTable.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
const sqlite3 = require('sqlite3').verbose();
|
||||
module.exports = async function (clientWhitelist) {
|
||||
const db = new sqlite3.Database('data/database.db');
|
||||
await new Promise ((resolve, reject)=>{
|
||||
db.run(
|
||||
`CREATE TABLE IF NOT EXISTS whitelist (
|
||||
Id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
serverId TEXT,
|
||||
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();
|
||||
}
|
||||
);
|
||||
})
|
||||
loadWhitelist(clientWhitelist);
|
||||
}
|
||||
|
||||
async function loadWhitelist(clientWhitelist) {
|
||||
const db = new sqlite3.Database('data/database.db');
|
||||
let rows = await new Promise((resolve) => {
|
||||
db.all(`SELECT * FROM whitelist`, function (error, rows){
|
||||
if(error){
|
||||
console.error("Failed to read whitelist table")
|
||||
console.error(error);
|
||||
resolve([]);
|
||||
}else{
|
||||
resolve(rows);
|
||||
}
|
||||
});
|
||||
});
|
||||
rows.forEach(row => {
|
||||
if(clientWhitelist.has(row.serverId)){
|
||||
let oldEntry = clientWhitelist.get(row.serverId);
|
||||
oldEntry.push(row.command)
|
||||
clientWhitelist.set(row.serverId, oldEntry)
|
||||
}else {
|
||||
clientWhitelist.set(row.serverId, [row.command])
|
||||
}
|
||||
});
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
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();
|
||||
}
|
||||
);
|
||||
})
|
||||
}
|
|
@ -1,6 +1,3 @@
|
|||
const fs = require('fs');
|
||||
const customReplaceWithVariables = require('../util/custom_commands/customReplaceWithVariables');
|
||||
|
||||
module.exports = function(client, owners, message, globalPrefix){
|
||||
let prefix = globalPrefix;
|
||||
let serverPrefix = client.serverPrefixes.get(message.guild.id);
|
||||
|
@ -22,19 +19,12 @@ module.exports = function(client, owners, message, globalPrefix){
|
|||
|
||||
const commandName = args.shift().toLowerCase();
|
||||
const command = client.commands.get(commandName);
|
||||
if (!command){
|
||||
const customPath = './data/customCommands.json';
|
||||
if(fs.existsSync(customPath)){
|
||||
let json = fs.readFileSync(customPath, 'utf8');
|
||||
let customCommands = JSON.parse(json)
|
||||
customCommands.forEach(function (customCommand) {
|
||||
if (customCommand.customName === commandName) {
|
||||
let customMessage = customReplaceWithVariables(customCommand.customMessage, message, prefix, globalPrefix)
|
||||
message.channel.send(customMessage)
|
||||
}
|
||||
});
|
||||
if(command.needsWhitelist){
|
||||
let isWhitelisted = client.whitelist.get(message.guild.id)?.includes(command.name);
|
||||
if(!isWhitelisted){
|
||||
message.channel.send(`\`${command.name}\` is not whitelisted in this server. The bot admin needs to whitelist the command in this server for it to work`)
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (command.admin && owners.indexOf(message.author.id.toString()) == -1) return;
|
||||
try {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue