discord-vardb
Version:
Imagine a module, that allows to you create vars in discord.js...
242 lines (233 loc) • 7.53 kB
JavaScript
const fs = require("node:fs");
const path = "./datas.json";
class GuildVars {
create(name, value, guildID) {
let data = {};
if (fs.existsSync(path)) {
data = JSON.parse(fs.readFileSync(path, 'utf8'));
}
data[guildID] = data[guildID] || {};
data[guildID][name] = value;
fs.writeFileSync(path, JSON.stringify(data, null, 3), "utf8");
}
get(name, guildID) {
if (!fs.existsSync(path)) {
return null;
}
const data = fs.readFileSync(path, 'utf8');
const data2 = JSON.parse(data);
return data2[guildID] ? data2[guildID][name] : null;
}
}
class UserVars {
create(name, value, userID) {
let data = {};
if (fs.existsSync(path)) {
data = JSON.parse(fs.readFileSync(path, 'utf8'));
}
data[userID] = data[userID] || {};
data[userID][name] = value;
fs.writeFileSync(path, JSON.stringify(data, null, 3), "utf8");
}
get(name, userID) {
if (!fs.existsSync(path)) {
return null;
}
const data = fs.readFileSync(path, 'utf8');
const data2 = JSON.parse(data);
return data2[userID] ? data2[userID][name] : null;
}
}
class ClientVars {
create(name, value, clientID) {
let data = {};
if (fs.existsSync(path)) {
data = JSON.parse(fs.readFileSync(path, 'utf8'));
}
data[clientID] = data[clientID] || {};
data[clientID][name] = value;
fs.writeFileSync(path, JSON.stringify(data, null, 3), "utf8");
}
get(name, clientID) {
if (!fs.existsSync(path)) {
return null;
}
const data = fs.readFileSync(path, 'utf8');
const data2 = JSON.parse(data);
return data2[clientID] ? data2[clientID][name] : null;
}
}
class CustomVars {
create(name, value, id) {
let data = {};
if (fs.existsSync(path)) {
data = JSON.parse(fs.readFileSync(path, 'utf8'));
}
data[id] = data[id] || {};
data[id][name] = value;
fs.writeFileSync(path, JSON.stringify(data, null, 3), "utf8");
}
get(name, id) {
if (!fs.existsSync(path)) {
return null;
}
const data = fs.readFileSync(path, 'utf8');
const data2 = JSON.parse(data);
return data2[id] ? data2[id][name] : null;
}
}
class UnCategorizedVars {
create(name, value) {
let data = {};
if (fs.existsSync(path)) {
data = JSON.parse(fs.readFileSync(path, 'utf8'));
}
data["uc"] = data["uc"] || {};
data["uc"][name] = value;
fs.writeFileSync(path, JSON.stringify(data, null, 3), "utf8");
}
get(name) {
if (!fs.existsSync(path)) {
return null;
}
const data = fs.readFileSync(path, 'utf8');
const data2 = JSON.parse(data);
return data2["uc"] ? data2["uc"][name] : null;
}
}
class ChannelVars {
create(name, value, channelID) {
let data = {};
if (fs.existsSync(path)) {
data = JSON.parse(fs.readFileSync(path, 'utf8'));
}
data[channelID] = data[channelID] || {};
data[channelID][name] = value;
fs.writeFileSync(path, JSON.stringify(data, null, 3), "utf8");
}
get(name, channelID) {
if (!fs.existsSync(path)) {
return null;
}
const data = fs.readFileSync(path, 'utf8');
const data2 = JSON.parse(data);
return data2[channelID] ? data2[channelID][name] : null;
}
}
class Required {
constructor(sendWarnMessage = true) {
if(sendWarnMessage) {
console.log("Thanks for using required console warn, good codes!");
console.warn("Required: This just for discord.js, you cannot use in aoi.js but you can use in another things, but if they dont have an variable setter, lmao!")
} else {
return console.warn("Selected: 'false', Code: null")
}
}
}
class UpdatedVariableFuncs {
create(name, value, id) {
let data = {};
if (fs.existsSync(path)) {
data = JSON.parse(fs.readFileSync(path, 'utf8'));
}
data[id] = data[id] || {};
data[id][name] = value;
fs.writeFileSync(path, JSON.stringify(data, null, 3), "utf8");
}
get(name, id) {
if (!fs.existsSync(path)) {
return null;
}
const data = fs.readFileSync(path, 'utf8');
const data2 = JSON.parse(data);
return data2[id] ? data2[id][name] : null;
}
add(number, name, id) {
if (!fs.existsSync(path)) {
return null;
}
const data = fs.readFileSync(path, 'utf8');
const data2 = JSON.parse(data);
const vvl = data2[id] ? data2[id][name] : null
if(typeof vvl === "number") {
return vvl + number;
} else {
throw new Error("Function used, but specified variable name's variable is not an number.")
}
}
have(name, id) {
if (!fs.existsSync(path)) {
return false;
}
const data = fs.readFileSync(path, 'utf8');
const data2 = JSON.parse(data);
if (!data2[id]) {
return false;
}
if (data2[id][name] !== undefined) {
return true;
} else {
return false;
}
}
reset(name, id) {
if (!fs.existsSync(path)) {
return false;
}
const data = fs.readFileSync(path, 'utf8');
let data2 = JSON.parse(data);
if (!data2[id]) {
return false;
}
if (typeof data2[id][name] === "number") {
data2[id][name] = 0;
fs.writeFileSync(path, JSON.stringify(data2, null, 3), "utf8");
} else {
return false;
}
}
deleteAll() {
fs.unlinkSync(path, (err) => {
throw new Error(`A error pupped up when running code: ${err}`)
})
}
typeof(name, id) {
if (!fs.existsSync(path)) {
return null;
}
const data = fs.readFileSync(path, 'utf8');
let data2 = JSON.parse(data);
return typeof data2[id][name];
}
math(number1, condition, number2, name, id) {
if (!fs.existsSync(path)) {
return null;
}
const data = fs.readFileSync(path, 'utf8');
const data2 = JSON.parse(data);
const vvl = data2[id] ? data2[id][name] : null
if(typeof vvl === "number") {
if(condition == "+") {
throw new Error(`Use 'add' function instead. - math(${number1}, ${condition}, ${number2}, ${name}, ${id})`)
} else if(condition == "-") {
return number1 - number2;
} else if(condition == "/") {
return number1 / number2
} else if(condition == "*") {
return number1 * number2
}
} else {
throw new Error("Function used, but specified variable name's variable is not an number.")
}
}
}
module.exports = {
GuildVars,
UserVars,
ClientVars,
UnCategorizedVars,
CustomVars,
ChannelVars,
Required,
UpdatedVariableFuncs
}