Skip to content

Commit

Permalink
V2.0.0 DEV 5 FIX 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Abadima authored Nov 8, 2023
1 parent 7e83ebf commit 83aad64
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 41 deletions.
12 changes: 11 additions & 1 deletion [email protected]
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# VERSION 2@DEV CHANGELOGS

## [DEV 5](https://github.com/Abadima/simply-xp/releases/tag/v2.0.0-dev.5)
## [DEV 5 FIX 0](https://github.com/Abadima/simply-xp/releases/tag/v2.0.0-dev.5-fix.0)

### ⚠️ Breaking Changes

- `roleSetup()` now takes `role` instead of `roles` again, to make migration from V1 less painful

### Bug Fixes

- Complete overhaul of `simply-xp-levelroles` in SQLite, making it actually functional
- Fix `simply-xps-levelroles` not having `timestamp` property in SQLite & MongoDB
- Fix `roleSetup.add()` not actually returning `true/false`

## [DEV 5](https://github.com/Abadima/simply-xp/releases/tag/v2.0.0-dev.5)

### Additions

Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@
"@napi-rs/canvas": "^0.1.44"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.5",
"@types/node": "^20.8.6",
"@typescript-eslint/eslint-plugin": "^6.7.5",
"@typescript-eslint/parser": "^6.7.5",
"@types/better-sqlite3": "^7.6.7",
"@types/node": "^20.9.0",
"@typescript-eslint/eslint-plugin": "^6.10.0",
"@typescript-eslint/parser": "^6.10.0",
"better-sqlite3": "^9.0.0",
"discord.js": "^14.13.0",
"eslint": "^8.51.0",
"eslint": "^8.53.0",
"jsdoc-to-markdown": "^8.0.0",
"mongodb": "^6.1.0",
"mongodb": "^6.2.0",
"typescript": "^5.2.2",
"uglify-js": "^3.17.4"
},
Expand Down
5 changes: 3 additions & 2 deletions src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ export async function connect(uri: string, options: ConnectionOptions = {type: u
);
xp.database.exec(`CREATE TABLE IF NOT EXISTS "simply-xp-levelroles"
(
gid TEXT UNIQUE,
lvlrole TEXT NOT NULL
gid TEXT NOT NULL,
lvlrole TEXT NOT NULL,
timestamp TEXT NOT NULL
)`
);
} catch (error: unknown) {
Expand Down
44 changes: 25 additions & 19 deletions src/functions/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,25 @@ export interface UserResult {
* @property {string} collection - The collection to create the document in.
* @property {object} data - The data to create the document with.
* @property {string} data.guild - The guild ID.
* @property {number} [data.level] - The level to assign the role at.
* @property {string | Array<string>} [data.roles] - The role(s) to assign.
* @property {string} data.timestamp - The timestamp of when the document was created.
* @property {object} data.lvlrole - The level role data.
* @property {number} data.lvlrole.lvl - The level.
* @property {string | Array<string>} [data.lvlrole.role] - The role ID(s).
*/
export interface LevelRoleOptions {
collection: "simply-xp-levelroles";
data: {
guild: string;
level: number;
roles?: string | Array<string>;
timestamp: string;
lvlrole: {
lvl: number,
role?: string | Array<string>;
};
};
}

export type LevelRoleResult = {
_id?: string,
guild: string;
level: number;
roles: Array<string>;
lvlrole: string;
timestamp: string;
}

Expand Down Expand Up @@ -99,20 +99,18 @@ export class db {
*/
static async createOne(query: UserOptions | LevelRoleOptions): Promise<UserResult | LevelRoleResult> {
if (!xp.database) throw new XpFatal({function: "createOne()", message: "No database connection"});
let result: Document;

switch (xp.dbType) {
case "mongodb":
(xp.database as MongoClient).db().collection(query.collection).insertOne(query.data).catch(error => handleError(error, "createOne()"));
result = db.findOne(query);
break;

case "sqlite":
if (query.collection === "simply-xps") result = (xp.database as Database).prepare("INSERT INTO \"simply-xps\" (user, guild, name, xp, level) VALUES (?, ?, ?, ?, ?)").run(query.data.user, query.data.guild, query.data?.name, query.data.xp, query.data.level);
else result = (xp.database as Database).prepare("INSERT INTO \"simply-xp-levelroles\" (guild, level, role) VALUES (?, ?, ?)").run(query.data.guild, query.data.level, query.data.roles);
if (query.collection === "simply-xps") (xp.database as Database).prepare("INSERT INTO \"simply-xps\" (user, guild, name, xp, level) VALUES (?, ?, ?, ?, ?)").run(query.data.user, query.data.guild, query.data?.name, query.data.xp, query.data.level);
else (xp.database as Database).prepare("INSERT INTO \"simply-xp-levelroles\" (gid, lvlrole, timestamp) VALUES (?, ?, ?)").run(query.data.guild, JSON.stringify(query.data.lvlrole), new Date().toISOString());
break;
}
return result as UserResult | LevelRoleResult;
return await db.findOne(query) as UserResult | LevelRoleResult;
}

/**
Expand All @@ -133,7 +131,7 @@ export class db {
break;
case "sqlite":
if (query.collection === "simply-xps") result = (xp.database as Database).prepare("DELETE FROM \"simply-xps\" WHERE guild = ?").run(query.data.guild);
else result = (xp.database as Database).prepare("DELETE FROM \"simply-xp-levelroles\" WHERE guild = ?").run(query.data.guild);
else result = (xp.database as Database).prepare("DELETE FROM \"simply-xp-levelroles\" WHERE gid = ?").run(query.data.guild);
}
return !!result;
}
Expand All @@ -157,7 +155,11 @@ export class db {
break;
case "sqlite":
if (query.collection === "simply-xps") result = (xp.database as Database).prepare("DELETE FROM \"simply-xps\" WHERE guild = ? AND user = ?").run(query.data.guild, query.data.user);
else result = (xp.database as Database).prepare("DELETE FROM \"simply-xp-levelroles\" WHERE guild = ? AND level = ?").run(query.data.guild, query.data.level);
else {
result = await this.find(query);
result = result.filter((row: LevelRoleResult) => JSON.parse(row.lvlrole).lvl === query.data.lvlrole.lvl)[0] as Document;
result = (xp.database as Database).prepare("DELETE FROM \"simply-xp-levelroles\" WHERE gid = ? AND lvlrole = ?").run(query.data.guild, result.lvlrole) as Document;
}
}
return !!result;
}
Expand All @@ -177,12 +179,16 @@ export class db {

switch (xp.dbType) {
case "mongodb":
result = (xp.database as MongoClient).db().collection(query.collection).findOne(query.data).catch(error => handleError(error, "findOne()")) as Document;
if (query.collection === "simply-xps") result = await (xp.database as MongoClient).db().collection(query.collection).findOne(query.data).catch(error => handleError(error, "findOne()")) as Document;
else result = await (xp.database as MongoClient).db().collection(query.collection).findOne({guild: query.data.guild, "lvlrole.lvl": query.data.lvlrole.lvl}).catch(error => handleError(error, "findOne()")) as Document;
break;

case "sqlite":
if (query.collection === "simply-xps") result = (xp.database as Database).prepare("SELECT * FROM \"simply-xps\" WHERE guild = ? AND user = ?").get(query.data.guild, query.data.user) as Document;
else result = (xp.database as Database).prepare("SELECT * FROM \"simply-xp-levelroles\" WHERE guild = ? AND level = ?").get(query.data.guild, query.data.level) as Document;
else {
result = await this.find(query);
result = result.filter((row: LevelRoleResult) => JSON.parse(row.lvlrole).lvl === query.data.lvlrole.lvl)[0];
}
break;
}
return result as UserResult | LevelRoleResult;
Expand All @@ -208,7 +214,7 @@ export class db {

case "sqlite":
if (query.collection === "simply-xps") result = (xp.database as Database).prepare("SELECT * FROM \"simply-xps\" WHERE guild = ?").all(query.data.guild) as Document;
else result = (xp.database as Database).prepare("SELECT * FROM \"simply-xp-levelroles\" WHERE guild = ?").all(query.data.guild) as Document;
else result = (xp.database as Database).prepare("SELECT * FROM \"simply-xp-levelroles\" WHERE gid = ?").all(query.data.guild) as Document;
break;
}
return result as UserResult[] | LevelRoleResult[];
Expand All @@ -235,7 +241,7 @@ export class db {

case "sqlite":
if (filter.collection === "simply-xps" && update.collection === "simply-xps") (xp.database as Database).prepare("UPDATE \"simply-xps\" SET xp = ?, level = ?"+ (update.data?.name ? ", name = ?" : "") +" WHERE guild = ? AND user = ?").run(update.data?.name ? [update.data.xp, update.data.level, update.data.name, filter.data.guild, filter.data.user] : [update.data.xp, update.data.level, filter.data.guild, filter.data.user]);
else if (filter.collection === "simply-xp-levelroles" && update.collection === "simply-xp-levelroles") (xp.database as Database).prepare("UPDATE \"simply-xp-levelroles\" SET role = ? WHERE guild = ? AND level = ?").run(update.data.roles, filter.data.guild, filter.data.level);
else if (filter.collection === "simply-xp-levelroles" && update.collection === "simply-xp-levelroles") (xp.database as Database).prepare("UPDATE \"simply-xp-levelroles\" SET lvlrole = ? WHERE gid = ?").run(JSON.stringify(update.data.lvlrole), filter.data.guild);
else throw new XpFatal({
function: "updateOne()", message: "Collection mismatch, expected same collection on both filter and update."
});
Expand Down
30 changes: 18 additions & 12 deletions src/roleSetup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {db} from "../xp";
import { LevelRoleResult } from "./functions/database";
import {XpFatal} from "./functions/xplogs";

/**
Expand All @@ -9,7 +10,7 @@ import {XpFatal} from "./functions/xplogs";
*/
export interface RoleSetupObject {
level: number;
roles: string[] | string;
role: string[] | string;
}

/**
Expand All @@ -35,17 +36,16 @@ export class roleSetup {
message: "Level must be a number"
});

if (!options?.roles) throw new XpFatal({
if (!options?.role) throw new XpFatal({
function: "roleSetup.add()", message: "Role was not provided"
});

if (typeof options?.roles === "string") options.roles = [options.roles];
if (typeof options?.role === "string") options.role = [options.role];

return await db.createOne({
// make a new ISO date
collection: "simply-xp-levelroles",
data: {guild: guildId, level: options.level, roles: options.roles, timestamp: new Date().toISOString()}
}) as unknown as boolean;
data: {guild: guildId, lvlrole: {lvl: options.level, role: options.role}}
}).then(() => true).catch(() => false);
}

/**
Expand All @@ -54,19 +54,25 @@ export class roleSetup {
* @param {string} guildId - The guild ID
* @param {number} levelNumber - The level number
* @link `Documentation:` https://simplyxp.js.org/docs/next/classes/roleSetup#roleSetupfind
* @returns {Promise<RoleSetupObject>} - The level role object
* @returns {Promise<LevelRoleResult>} - The level role object
* @throws {XpFatal} If an invalid type is provided or value is not provided.
*/
static async find(guildId: string, levelNumber: number): Promise<RoleSetupObject> {
static async find(guildId: string, levelNumber: number): Promise<LevelRoleResult> {
if (!guildId) throw new XpFatal({function: "roleSetup.find()", message: "Guild ID was not provided"});
if (isNaN(levelNumber)) throw new XpFatal({
function: "roleSetup.find()", message: "Level Number was not provided"
});

return await db.findOne({
const results = await db.findOne({
collection: "simply-xp-levelroles",
data: {guild: guildId, level: levelNumber, timestamp: new Date().toISOString()}
}) as RoleSetupObject;
data: {guild: guildId, lvlrole: {lvl: levelNumber}}
}) as LevelRoleResult;

if (!results) return results;
else {
results.lvlrole = typeof(results.lvlrole) === "string" ? JSON.parse(results.lvlrole) : results.lvlrole;
return results;
}
}

/**
Expand All @@ -86,7 +92,7 @@ export class roleSetup {

return await db.deleteOne({
collection: "simply-xp-levelroles",
data: {guild: guildId, level: levelNumber, timestamp: new Date().toISOString()}
data: {guild: guildId, lvlrole: {lvl: levelNumber}}
});
}
}
2 changes: 1 addition & 1 deletion src/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function setXP(userId: string, guildId: string, xpData: number, use
if (!guildId) throw new XpFatal({function: "setXP()", message: "Guild ID was not provided"});
if (isNaN(xpData)) throw new XpFatal({function: "setXP()", message: "XP was not provided"});

const user = await db.findOne({collection: "simply-xps", data: {user: userId, guild: guildId}});
const user = await db.findOne({collection: "simply-xps", data: {user: userId, guild: guildId}}) as UserResult;
let data;

if (!user) {
Expand Down

0 comments on commit 83aad64

Please sign in to comment.