Skip to content

Commit

Permalink
v2.0.0-beta.0-fix.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Abadima committed Feb 4, 2024
1 parent e420006 commit eec3eeb
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 46 deletions.
30 changes: 15 additions & 15 deletions Tests/test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ async function test(dbType) {

await xp.roleSetup.add("0987654321", { level: 1, role: "01" });

await xp.roleSetup.list("0987654321").then(console.log);

await xp.roleSetup.remove("0987654321", 1);

await xp.rankCard(
Expand Down Expand Up @@ -98,20 +100,18 @@ async function test(dbType) {
}
);

await xp.leaderboard().then(async (users) => {
await xp.leaderboardCard(users, {
// artworkImage: "https://th.bing.com/th/id/R.8cd8594560bd9cf4b042833a4acefaa5?rik=A6B1qYN%2b5GQAcA&riu=http%3a%2f%2fwallpaperswide.com%2fdownload%2fdesert_sky-wallpaper-2560x720.jpg&ehk=rE5VYZy8njd5ZeNT2p4sP7C5psjSf%2bxLZmV%2bvlQCffs%3d&risl=&pid=ImgRaw&r=0",
//backgroundImage: new URL("https://static.vecteezy.com/system/resources/previews/000/962/809/original/abstract-gradient-background-with-colorful-and-modern-style-vector.jpg"),
fallbackFont: "https://cdn.jsdelivr.net/fontsource/fonts/mochiy-pop-one@latest/japanese-400-normal.woff2",
light: false,
rowOpacity: 1
}, {
name: "Development Hub",
imageURL: "https://cdn.discordapp.com/icons/950190034852646912/5a800bf4caf28bfcaccc214446b461c4.webp",
//memberCount: 20
}, {}).then(results => {
require("fs").writeFileSync("Tests/leaderboard.png", results.attachment);
});
await xp.leaderboardCard(await xp.leaderboard(), {
// artworkImage: "https://th.bing.com/th/id/R.8cd8594560bd9cf4b042833a4acefaa5?rik=A6B1qYN%2b5GQAcA&riu=http%3a%2f%2fwallpaperswide.com%2fdownload%2fdesert_sky-wallpaper-2560x720.jpg&ehk=rE5VYZy8njd5ZeNT2p4sP7C5psjSf%2bxLZmV%2bvlQCffs%3d&risl=&pid=ImgRaw&r=0",
//backgroundImage: new URL("https://static.vecteezy.com/system/resources/previews/000/962/809/original/abstract-gradient-background-with-colorful-and-modern-style-vector.jpg"),
fallbackFont: "https://cdn.jsdelivr.net/fontsource/fonts/mochiy-pop-one@latest/japanese-400-normal.woff2",
light: false,
rowOpacity: 1
}, {
name: "Development Hub",
imageURL: "https://cdn.discordapp.com/icons/950190034852646912/5a800bf4caf28bfcaccc214446b461c4.webp",
//memberCount: 20
}, {}).then(results => {
require("fs").writeFileSync("Tests/leaderboard.png", results.attachment);
});

await xp.charts("0987654321", {
Expand All @@ -128,7 +128,7 @@ async function test(dbType) {
});

await xp.db.deleteMany({
collection: "simply-xps-levelroles", data: {
collection: "simply-xp-levelroles", data: {
guild: "0987654321"
}
});
Expand Down
Binary file modified Tests/test.sqlite
Binary file not shown.
18 changes: 13 additions & 5 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# VERSION 2 CHANGELOGS

## [BETA 0 FIX 1](https://github.com/Abadima/simply-xp/releases/tag/v2.0.0-beta.0-fix.1)

### ⚠️ Breaking Changes

- `db.find()` now takes new parameters, `(collection, guildId)` for better syntax.

### Bug Fixes

- Fix `fetch()` rare error when user doesn't exist, and `auto_create` is true.
- Fix `roleSetup.list()` not returning roles when using MongoDB (Thanks itz_hyp3r on Discord for the report)

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

### Additions

- Add `fallbackFont` option to `charts()`, `compareCard()`, `leaderboardCard()` and `rankCard()`, to use a fallback font, as part of
our bug fix.

### Improvements

- `rank()` gets minor code improvements.

### Bug Fixes

- Fix typings for `updateOptions()`
Expand All @@ -19,6 +26,7 @@

### Improvements

- `rank()` gets minor code improvements.
- Remove URL ending checks (`comapreCard()` and `rankCard()`), to allow for better image/gif support, while still
elegantly handling errors.

Expand Down Expand Up @@ -87,7 +95,7 @@
### 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 `simply-xp-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)
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simply-xp",
"version": "2.0.0-beta.0-fix.0",
"version": "2.0.0-beta.0-fix.1",
"description": "The easiest way to implement xp system",
"main": "lib/xp.js",
"scripts": {
Expand Down Expand Up @@ -46,11 +46,11 @@
},
"devDependencies": {
"@eslint/eslintrc": "^3.0.0",
"@types/better-sqlite3": "^7.6.8",
"@types/node": "^20.11.5",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"better-sqlite3": "^9.3.0",
"@types/better-sqlite3": "^7.6.9",
"@types/node": "^20.11.16",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"better-sqlite3": "^9.4.0",
"eslint": "^8.56.0",
"mongodb": "^6.3.0",
"typescript": "^5.3.3",
Expand Down
2 changes: 1 addition & 1 deletion src/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ export async function rankCard(guild: {
else throw new XpFatal({ function: "rankCard()", message: "User not found in database" });
}

const users = await db.find({ collection: "simply-xps", data: { guild: guild.id } }) as User[];
const users = await db.find("simply-xps", guild.id) as User[];

dbUser.position = 1 + users.sort((a, b) => b.xp - a.xp).findIndex((u) => u.user === user.id) || 1;

Expand Down
10 changes: 5 additions & 5 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export async function fetch(userId: string, guildId: string, username?: string):
if (!guildId) throw new XpFatal({ function: "create()", message: "Guild ID was not provided" });
clean({ db: true });

const users: User[] = await (await import("./functions/database")).db.find({
collection: "simply-xps", data: { guild: guildId }
}) as User[];
const users: User[] = await (await import("./functions/database")).db.find("simply-xps", guildId) as User[];

let user: User | UserResult | undefined = users.find((u) => u.user === userId);

if (!user) {
if (xp.auto_create && username) user = await (await import("./create")).create(guildId, userId, username);
else throw new XpFatal({ function: "fetch()", message: "User data not found" });
if (xp.auto_create && username) {
user = await (await import("./create")).create(guildId, userId, username);
users.push(user as User);
} else throw new XpFatal({ function: "fetch()", message: "User data not found" });
}

const position = users.sort((a, b) => b.xp - a.xp).findIndex((u) => u.user === userId) + 1;
Expand Down
15 changes: 8 additions & 7 deletions src/functions/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class db {
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 = await this.find(query);
result = await this.find(query.collection, query.data.guild);
result = result.filter((row: LevelRoleResult) => 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, JSON.stringify(result.lvlrole)) as Document;
}
Expand Down Expand Up @@ -212,7 +212,7 @@ export class db {
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 = await this.find(query);
result = await this.find(query.collection, query.data.guild);
result = result.filter((row: LevelRoleResult) => row.lvlrole.lvl === query.data.lvlrole.lvl)[0];
}
break;
Expand All @@ -224,24 +224,25 @@ export class db {
* Finds multiple documents in the database.
*
* @async
* @param {UserOptions | LevelRoleOptions} query - The query to search for multiple documents.
* @param {"simply-xps" | "simply-xp-levelroles"} collection - The collection to search for multiple documents.
* @param {string} guild - The guild ID to search for.
* @link https://simplyxp.js.org/docs/next/handlers/database#find Documentation
* @returns {Promise<UserResult[] | LevelRoleResult[]>} An array of found documents.
* @throws {XpFatal} Throws an error if there is no database connection.
*/
static async find(query: UserOptions | LevelRoleOptions): Promise<UserResult[] | LevelRoleResult[]> {
static async find(collection: "simply-xps" | "simply-xp-levelroles", guild: string): Promise<UserResult[] | LevelRoleResult[]> {
if (!xp.database) throw new XpFatal({ function: "find()", message: "No database connection" });
let result: Document;

switch (xp.dbType) {
case "mongodb":
result = (xp.database as MongoClient).db().collection(query.collection).find(query.data).toArray().catch(error => handleError(error, "find()")) as Document;
result = (xp.database as MongoClient).db().collection(collection).find({guild}).toArray().catch(error => handleError(error, "find()")) as Document;
break;

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;
if (collection === "simply-xps") result = (xp.database as Database).prepare("SELECT * FROM \"simply-xps\" WHERE guild = ?").all(guild) as Document;
else {
result = (xp.database as Database).prepare("SELECT * FROM \"simply-xp-levelroles\" WHERE gid = ?").all(query.data.guild) as Document;
result = (xp.database as Database).prepare("SELECT * FROM \"simply-xp-levelroles\" WHERE gid = ?").all(guild) as Document;
if (result.length) result = await Promise.all(result.map(async (row: {
gid: string; lvlrole: string; lastUpdated: string;
}) => {
Expand Down
4 changes: 1 addition & 3 deletions src/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ export async function leaderboard(guildId?: string, limit?: number): Promise<Use
let users: Array<User>;
const userIds: Set<string> = new Set<string>();

if (guildId) users = (await db.find({
collection: "simply-xps", data: { guild: guildId }
}) as Array<User>).sort((a, b) => b.xp - a.xp);
if (guildId) users = (await db.find("simply-xps", guildId) as Array<User>).sort((a, b) => b.xp - a.xp);
else users = (await db.findAll("simply-xps") as Array<User>).sort((a, b) => b.xp - a.xp).filter((user) => {
if (userIds.has(user.user)) return false;
userIds.add(user.user);
Expand Down
5 changes: 1 addition & 4 deletions src/roleSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ export class roleSetup {
static async list(guildId: string): Promise<LevelRoleResult[]> {
if (!guildId) throw new XpFatal({ function: "roleSetup.list()", message: "Guild ID was not provided" });

return await db.find({
collection: "simply-xp-levelroles",
data: { guild: guildId, lvlrole: { lvl: null, role: null } }
}) as LevelRoleResult[];
return await db.find("simply-xp-levelroles", guildId) as LevelRoleResult[];
}

/**
Expand Down

0 comments on commit eec3eeb

Please sign in to comment.