Skip to content

Commit

Permalink
Add: Game Leaderboard (#384)
Browse files Browse the repository at this point in the history
* fix & add: pagination & leaderboard

* fix: pagination & leaderboard

* add: wycustom decline button

* add: translations

* fix: game scores

* fix: description

* fix: remove unused imports

* add: formatting

* fix: add translations

---------

Co-authored-by: ForGetFulSkyBro <forgetfulskybro9@gmail.com>
  • Loading branch information
mezotv and forgetfulskybro authored Dec 27, 2023
1 parent b2c6b68 commit 709aa73
Show file tree
Hide file tree
Showing 21 changed files with 661 additions and 130 deletions.
2 changes: 1 addition & 1 deletion src/buttons/higher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const button: Button = {
const gameImage = new LOSE();
gameImage.setGame(game);

gameImage.build(game?.score || 0).then((image) => {
gameImage.build(game?.score || 0, client).then((image) => {
interaction.editReply({
embeds: [loseEmbed],
files: [
Expand Down
2 changes: 1 addition & 1 deletion src/buttons/lower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const button: Button = {
const gameImage = new LOSE();
gameImage.setGame(game);

gameImage.build(game?.score || 0).then((image) => {
gameImage.build(game?.score || 0, client).then((image) => {
interaction.editReply({
embeds: [loseEmbed],
files: [
Expand Down
11 changes: 8 additions & 3 deletions src/buttons/paginateFirst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ const button: Button = {
name: "paginateFirst",
execute: async (interaction, client, guildDb) => {
let paginate = client.paginate.get(
`${interaction.user.id}-${interaction.message.reference?.messageId}`,
`${interaction.user.id}-${interaction.message.interaction?.id}`,
);

if (!paginate)
paginate = client.paginate.get(
`${interaction.user.id}-leaderboard-${interaction.message.interaction?.id}`,
);

if (!paginate)
paginate = client.paginate.get(`${interaction.user.id}-custom`);

Expand Down Expand Up @@ -72,11 +77,11 @@ const button: Button = {
const time = setTimeout(() => {
if (
client.paginate.get(
`${interaction.user.id}-${interaction.message.reference?.messageId}`,
`${interaction.user.id}-${interaction.message.interaction?.id}`,
)
)
client.paginate.delete(
`${interaction.user.id}-${interaction.message.reference?.messageId}`,
`${interaction.user.id}-${interaction.message.interaction?.id}`,
);
}, paginate.time);
paginate.timeout = time;
Expand Down
102 changes: 87 additions & 15 deletions src/buttons/paginateLast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ import {
MessageActionRowComponentBuilder,
} from "discord.js";
import { Button } from "../models";
import { UserModel } from "../util/Models/userModel";

const button: Button = {
name: "paginateLast",
execute: async (interaction, client, guildDb) => {
let type: any = null;
let paginate = client.paginate.get(
`${interaction.user.id}-${interaction.message.reference?.messageId}`,
`${interaction.user.id}-${interaction.message.interaction?.id}`,
);

if (!paginate)
paginate = client.paginate.get(`${interaction.user.id}-custom`);
(paginate = client.paginate.get(
`${interaction.user.id}-leaderboard-${interaction.message.interaction?.id}`,
)),
(type = "leaderboard");

if (!paginate)
(paginate = client.paginate.get(`${interaction.user.id}-custom`)),
(type = "custom");

if (!paginate) {
interaction.reply({
Expand All @@ -38,6 +47,82 @@ const button: Button = {
return;
}

clearTimeout(paginate.timeout);
const time = setTimeout(() => {
if (
type === "custom" &&
client.paginate.get(`${interaction.user.id}-custom`)
) {
client.paginate.delete(`${interaction.user.id}-custom`);
} else if (
type === "leaderboard" &&
client.paginate.get(
`${interaction.user.id}-leaderboard-${interaction.message.interaction?.id}`,
)
) {
client.paginate.delete(
`${interaction.user.id}-leaderboard-${interaction.message.interaction?.id}`,
);
} else if (
client.paginate.get(
`${interaction.user.id}-${interaction.message.interaction?.id}`,
)
) {
client.paginate.delete(
`${interaction.user.id}-${interaction.message.interaction?.id}`,
);
}
}, paginate.time);
paginate.timeout = time;

let embed = paginate.pages[paginate.pages.length - 1];
let data;

if (
type === "leaderboard" &&
!paginate.countedPages.includes(paginate.page)
) {
paginate.countedPages.push(paginate.page);

data = await UserModel.find({
"higherlower.highscore": { $gt: 1 },
})
.sort({ "higherlower.highscore": -1 })
.skip(paginate.page * 10)
.limit(10);

data = await Promise.all(
data.map(async (u: any) => {
const user = await client.database.getUser(u.userID, true);
return user?.votePrivacy
? {
user: "Anonymous",
score: u.higherlower.highscore,
}
: {
user: u.userID,
score: u.higherlower.highscore,
};
}),
);
data = data.map(
(s: any, i) =>
`${(paginate.pages.length - 1) * 10 + i++}. ${
s.user === "Anonymous"
? `${s.user} • **${s.score}** ${client.translation.get(
guildDb?.language,
"Leaderboard.points",
)}`
: `<@${s.user}> • **${s.score}** ${client.translation.get(
guildDb?.language,
"Leaderboard.points",
)}`
}`,
);

embed.data.description = data?.join("\n");
}

const buttons =
new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents(
new ButtonBuilder()
Expand Down Expand Up @@ -68,19 +153,6 @@ const button: Button = {
},
});

clearTimeout(paginate.timeout);
const time = setTimeout(() => {
if (
client.paginate.get(
`${interaction.user.id}-${interaction.message.reference?.messageId}`,
)
)
client.paginate.delete(
`${interaction.user.id}-${interaction.message.reference?.messageId}`,
);
}, paginate.time);
paginate.timeout = time;

paginate.page = paginate.pages.length - 1;
},
};
Expand Down
149 changes: 102 additions & 47 deletions src/buttons/paginateNext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ import {
ButtonStyle,
} from "discord.js";
import { Button } from "../models";
import { UserModel } from "../util/Models/userModel";

const button: Button = {
name: "paginateNext",
execute: async (interaction, client, guildDb) => {
let type: any = null;
let paginate = client.paginate.get(
`${interaction.user.id}-${interaction.message.reference?.messageId}`,
`${interaction.user.id}-${interaction.message.interaction?.id}`,
);

if (!paginate)
paginate = client.paginate.get(`${interaction.user.id}-custom`);
(paginate = client.paginate.get(
`${interaction.user.id}-leaderboard-${interaction.message.interaction?.id}`,
)),
(type = "leaderboard");

if (!paginate)
(paginate = client.paginate.get(`${interaction.user.id}-custom`)),
(type = "custom");

if (!paginate) {
interaction.reply({
Expand All @@ -38,7 +47,83 @@ const button: Button = {
return;
}

if (paginate.page + 1 === paginate.pages.length - 1) {
clearTimeout(paginate.timeout);
const time = setTimeout(() => {
if (
type === "custom" &&
client.paginate.get(`${interaction.user.id}-custom`)
) {
client.paginate.delete(`${interaction.user.id}-custom`);
} else if (
type === "leaderboard" &&
client.paginate.get(
`${interaction.user.id}-leaderboard-${interaction.message.interaction?.id}`,
)
) {
client.paginate.delete(
`${interaction.user.id}-leaderboard-${interaction.message.interaction?.id}`,
);
} else if (
client.paginate.get(
`${interaction.user.id}-${interaction.message.interaction?.id}`,
)
) {
client.paginate.delete(
`${interaction.user.id}-${interaction.message.interaction?.id}`,
);
}
}, paginate.time);
paginate.timeout = time;

let embed = paginate.pages[++paginate.page];
let data;
if (
type === "leaderboard" &&
!paginate.countedPages.includes(paginate.page)
) {
paginate.countedPages.push(paginate.page);

data = await UserModel.find({
"higherlower.highscore": { $gt: 1 },
})
.sort({ "higherlower.highscore": -1 })
.skip(paginate.page * 10)
.limit(10);

data = await Promise.all(
data.map(async (u: any) => {
const user = await client.database.getUser(u.userID, true);
return user?.votePrivacy
? {
user: "Anonymous",
score: u.higherlower.highscore,
}
: {
user: u.userID,
score: u.higherlower.highscore,
};
}),
);

data = data.map(
(s: any, i) =>
`${paginate.page * 10 + i++}. ${
s.user === "Anonymous"
? `${s.user} • **${s.score}** ${client.translation.get(
guildDb?.language,
"Leaderboard.points",
)}`
: `<@${s.user}> • **${s.score}** ${client.translation.get(
guildDb?.language,
"Leaderboard.points",
)}`
}`,
);

embed.data.description = data?.join("\n");
}

if (paginate.page + 1 === paginate.pages.length) {
const buttons =
new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents(
new ButtonBuilder()
Expand All @@ -61,28 +146,13 @@ const button: Button = {
.setStyle(ButtonStyle.Secondary),
);

clearTimeout(paginate.timeout);
const time = setTimeout(() => {
if (
client.paginate.get(
`${interaction.user.id}-${interaction.message.reference?.messageId}`,
)
)
client.paginate.delete(
`${interaction.user.id}-${interaction.message.reference?.messageId}`,
);
}, paginate.time);
paginate.timeout = time;

await interaction
.update({
embeds: [paginate.pages[++paginate.page]],
components: [buttons],
options: {
ephemeral: true,
},
})
.catch(() => {});
await interaction.update({
embeds: [embed],
components: [buttons],
options: {
ephemeral: true,
},
});
return;
} else {
const buttons =
Expand All @@ -105,28 +175,13 @@ const button: Button = {
.setStyle(ButtonStyle.Secondary),
);

clearTimeout(paginate.timeout);
const time = setTimeout(() => {
if (
client.paginate.get(
`${interaction.user.id}-${interaction.message.reference?.messageId}`,
)
)
client.paginate.delete(
`${interaction.user.id}-${interaction.message.reference?.messageId}`,
);
}, paginate.time);
paginate.timeout = time;

await interaction
.update({
embeds: [paginate.pages[++paginate.page]],
components: [buttons],
options: {
ephemeral: true,
},
})
.catch(() => {});
await interaction.update({
embeds: [embed],
components: [buttons],
options: {
ephemeral: true,
},
});
return;
}
},
Expand Down
Loading

0 comments on commit 709aa73

Please sign in to comment.