Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #503

Merged
merged 21 commits into from
Dec 1, 2023
Merged

fixes #503

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
466 changes: 252 additions & 214 deletions src/classes/AoiError.js

Large diffs are not rendered by default.

128 changes: 70 additions & 58 deletions src/classes/Database.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,84 @@
const aoidb = require("@akarui/aoi.db");
const AoiError = require("../classes/AoiError.js");

class Database {
/**
* @type {aoidb.KeyValue | aoidb.Transmitter} db
* @type {boolean} ready
* @type {number} readyAt
* @type {"aoi.db"} type
* @type {"KeyValue" | "Transmitter"} moduleType
* @type {string[]} tables
*/
db;
ready = false;
readyAt = 0;
type;
tables = [];
moduleType = "KeyValue";
/**
* Description
* @param {any} module
* @param {"KeyValue" | "Transmitter"} type
* @param {aoidb.KeyValueOptions | aoidb.TransmitterOptions} config
* @returns {any}
*/
constructor(moduleType, module, type, config) {
this.moduleType = type;
this.db = new module[type](config);
this.tables = config.dataConfig.tables;
this.type = moduleType;
this.db.on(aoidb.DatabaseEvents.Connect, () => {
console.log(`[@akarui/aoi.db] Connected ${type} database`);
this.ready = true;
this.readyAt = Date.now();
});
/**
* @type {aoidb.KeyValue | aoidb.Transmitter} db
* @type {boolean} ready
* @type {number} readyAt
* @type {"aoi.db"} type
* @type {"KeyValue" | "Transmitter"} moduleType
* @type {string[]} tables
*/
db;
ready = false;
readyAt = 0;
type;
tables = [];
moduleType = "KeyValue";
/**
* Description
* @param {any} module
* @param {"KeyValue" | "Transmitter"} type
* @param {aoidb.KeyValueOptions | aoidb.TransmitterOptions} config
* @returns {any}
*/
constructor(moduleType, module, type, config) {
this.moduleType = type;
this.db = new module[type](config);
this.tables = config.dataConfig.tables;
this.type = moduleType;
this.db.on(aoidb.DatabaseEvents.Connect, () => {
AoiError.createCustomBoxedMessage(
[
{
text: `Successfully connected ${type} database`,
textColor: "white",
},
],
"white",
{ text: "@akarui/db ", textColor: "cyan" }
);

this.db.connect();
}
// console.log(`[@akarui/aoi.db] Connected ${type} database`);
this.ready = true;
this.readyAt = Date.now();
});

async set(table, key, id, value) {
return await this.db.set(table, id ? `${key}_${id}` : key, { value });
}
this.db.connect();
}

async get(table, key, id) {
return await this.db.get(table, id ? `${key}_${id}` : key);
}
async set(table, key, id, value) {
return await this.db.set(table, id ? `${key}_${id}` : key, { value });
}

async delete(table, key, id) {
return await this.db.delete(table, id ? `${key}_${id}` : key);
}
async get(table, key, id) {
return await this.db.get(table, id ? `${key}_${id}` : key);
}

async all(table, query, limit) {
return await this.db.all(table, query, limit);
}
async delete(table, key, id) {
return await this.db.delete(table, id ? `${key}_${id}` : key);
}

async has(table, key, id) {
return await this.db.has(table, id ? `${key}_${id}` : key);
}
async all(table, query, limit) {
return await this.db.all(table, query, limit);
}

async deleteMany(table, query) {
return await this.db.deleteMany(table, query);
}
async has(table, key, id) {
return await this.db.has(table, id ? `${key}_${id}` : key);
}

async findOne(table, query) {
return await this.db.findOne(table, query);
}
async deleteMany(table, query) {
return await this.db.deleteMany(table, query);
}

async findMany(table, query, limit) {
return await this.db.findMany(table, query, limit);
}
async findOne(table, query) {
return await this.db.findOne(table, query);
}

async findMany(table, query, limit) {
return await this.db.findMany(table, query, limit);
}
}

module.exports = Database;
module.exports = Database;
2 changes: 1 addition & 1 deletion src/classes/LoadCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class LoadCommands {

try {
if (await fs.promises.stat(path).then((f) => !f.isDirectory()))
console.error("Error!");
throw new TypeError("Path is not a valid directory!");
} catch (e) {
throw new TypeError("Path is not a valid directory! ErrorMessage: " + e);
}
Expand Down
4 changes: 2 additions & 2 deletions src/classes/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class Util {

static get threadTypes() {
return {
public: "GUILD_PUBLIC_THREAD",
private: "GUILD_PRIVATE_THREAD",
public: Discord.ChannelType.PublicThread,
private: Discord.ChannelType.PrivateThread,
};
}

Expand Down
28 changes: 16 additions & 12 deletions src/functions/client/broadcastEval.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
module.exports = async (d) => {
const data = d.util.aoiFunc(d);
if (data.err) return d.error(data.err);
const data = d.util.aoiFunc(d);
if (data.err) return d.error(data.err);

const [func] = data.inside.splits;
const [func] = data.inside.splits;

function evalfunc(client, {func}) {
return eval(func)
}
function evalfunc(client, { func }) {
return eval(func);
}

data.result = await d.client.shard.broadcastEval(evalfunc, {context: {func: func}});
if (!d.client.shard) return d.aoiError.fnError(d,"custom", {}, "ClientShard Class is Not Initialised");

data.result = data.result.join(" , ");
data.result = await d.client.shard.broadcastEval(evalfunc, {
context: { func: func },
});

return {
code: d.util.setCode(data),
};
};
data.result = data.result.join(" , ");

return {
code: d.util.setCode(data),
};
};
4 changes: 2 additions & 2 deletions src/functions/client/fetchClientValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module.exports = async d => {
if (data.err) return d.error(data.err);

const [func] = data.inside.splits;
if (!d.client.clientShard) return d.aoiError.fnError(d, 'custom', {}, 'ClientShard Class is Not Initialised');
if (!d.client.shard) return d.aoiError.fnError(d, 'custom', {}, 'ClientShard Class is Not Initialised');

data.result = await d.client.clientShard.fetchClientValues(func);
data.result = await d.client.shard.fetchClientValues(func);

return {
code: d.util.setCode(data),
Expand Down
20 changes: 10 additions & 10 deletions src/functions/event/bulk.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const {BulkData} = require("../../utils/EventUtil.js");
const { BulkData } = require("../../utils/EventUtil.js");

module.exports = d => {
const data = d.util.aoiFunc(d);
if (data.err) return d.error(data.err);
module.exports = (d) => {
const data = d.util.aoiFunc(d);
if (data.err) return d.error(data.err);

const [option] = data.inside.splits;
const [option, sep = " , "] = data.inside.splits;

data.result = BulkData(d, option).deleteBrackets();
data.result = BulkData(d, sep, option).deleteBrackets();

return {
code: d.util.setCode(data)
}
}
return {
code: d.util.setCode(data),
};
};
23 changes: 12 additions & 11 deletions src/functions/guild/createThread.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
module.exports = async d => {
const {code} = d.command;
const inside = d.unpack();
const err = d.inside(inside);
if (err) return d.error(err);
const data = d.util.aoiFunc(d);
const { code } = d.command;
if (data.err) return d.error(data.err);

let [channelID, name, archive = "MAX", type = "public", startMessage, returnID = "false"] = inside.splits;
let [channelID, name, archive = "MAX", type = "public", startMessage, returnID = "false"] = data.inside.splits;

const channel = await d.util.getChannel(d, channelID);
if (!channel) return d.aoiError.fnError(d, "channel", {inside});
if (!channel) return d.aoiError.fnError(d, "channel", { inside: data.inside });

type = d.util.threadTypes[type];
if (!type) return d.aoiError.fnError(d, "custom", {inside}, "Invalid Type Provided In");
if (!["60", "1440", "4320", "10080", "MAX"].includes(archive.toUpperCase())) d.aoiError.fnError(d, "custom", {inside}, "Invalid Archive Duration Provided In");
if (!type) return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Invalid Type Provided In");
if (!["60", "1440", "4320", "10080", "MAX"].includes(archive.toUpperCase())) return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Archive Duration Provided In");

const result = await channel.threads.create({
name,
autoArchiveDuration: archive,
autoArchiveDuration: archive.toUpperCase().replace("MAX", "10080"),
type,
startMessage: startMessage?.trim() === "" ? undefined : startMessage
}).catch(e => {
d.aoiError.fnError(d, "custom", {}, "Failed To Create Thread With Reason: " + e);
return d.aoiError.fnError(d, "custom", {}, "Failed To Create Thread With Reason: " + e);
});

data.result = returnID === "true" ? result?.id : undefined;

return {
code: d.util.setCode({function: d.func, code, inside, result: returnID === "true" ? result?.id : ""})
code: d.util.setCode(data)
}
}
2 changes: 1 addition & 1 deletion src/functions/guild/guildHighestRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ module.exports = async d => {
return {
code: d.util.setCode(data)
}
}
}
24 changes: 14 additions & 10 deletions src/functions/guild/guildLowestRole.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
module.exports = async d => {
const data = d.util.aoiFunc(d);
module.exports = async (d) => {
const data = d.util.aoiFunc(d);

const [guildID = d.guild?.id] = data.inside.splits;
const [guildID = d.guild?.id, option = "id"] = data.inside.splits;

const guild = await d.util.getGuild(d, guildID);
if (!guild) return d.aoiError.fnError(d, 'guild', {inside: data.inside});
const guild = await d.util.getGuild(d, guildID);
if (!guild) return d.aoiError.fnError(d, "guild", { inside: data.inside });

data.result = [...guild.roles.cache.sort((a, b) => a.position - b.position)][1]?.[0];
const role = guild.roles.cache.filter((role) => role.id !== guild.id).sort((a, b) => a.position - b.position) .first();

return {
code: d.util.setCode(data)
}
}
data.result = !role
? guild?.roles.everyone.id
: role === undefined
? guild.id
: role?.[option.toLowerCase()];

return { code: d.util.setCode(data) };
};
2 changes: 1 addition & 1 deletion src/functions/info/customEmoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = async d => {
let result;

if (id === "global") {
result = d.client.emojis.cache.find(x => x.name.toLowerCase() === emoji.toLowerCase() || x.toString() === emoji || x.id === emoji)?.toString()
result = (await d.util.getEmoji(d, emoji)).toString();
} else {
result = d.client.guilds.cache.get(id)?.emojis.cache.find(x => x.name.toLowerCase() === emoji.toLowerCase() || x.toString() === emoji || x.id === emoji)?.toString()
}
Expand Down
37 changes: 37 additions & 0 deletions src/functions/interaction/awaitExecute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const Interpreter = require("../../core/interpreter.js");

module.exports = async (d) => {
const data = d.util.aoiFunc(d);
if (data.err) return d.error(data.err);

const [awaitfunc] = data.inside.splits;

const cmd = d.client.cmd.awaited.find(
(x) => x.name.toLowerCase() === awaitfunc.addBrackets().toLowerCase()
);

if (!cmd)
return d.aoiError.fnError(
d,
"custom",
{},
`Invalid Awaited Command: '${awaitfunc.addBrackets()}' Provided`
);

await Interpreter(
d.client,
d.message,
d.args,
cmd,
d.client.db,
false,
undefined,
d.data
);

data.result = null;

return {
code: d.util.setCode(data),
};
};
19 changes: 19 additions & 0 deletions src/functions/message/sendSticker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = async (d) => {
const data = d.util.aoiFunc(d);

let [resolver] = data.inside.splits;

const guild = d.client.guilds.cache.get(d.guild?.id);
if (!guild) return d.aoiError.fnError(d, "guild", { inside: data.inside });

const sticker = await d.util.getSticker(guild, resolver);
if (!sticker) return d.aoiError.fnError(d, "custom", { inside: data.inside }, "sticker");

try {
await d.channel.send({ stickers: [sticker.id] });
} catch (err) {
return d.aoiError.fnError(d, "custom", { inside: data.inside }, `Failed to send resolver: ${err}`);
}

return { code: d.util.setCode(data) };
};
Loading
Loading