Skip to content

Commit

Permalink
fix: leaderboard (unfinished)
Browse files Browse the repository at this point in the history
  • Loading branch information
forgetfulskybro committed Dec 1, 2023
1 parent 27c12e0 commit 5781e06
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/buttons/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const button: Button = {
user: interaction.user.id,
timeout: 180000,
client,
image: null,
});

page.add(
Expand All @@ -41,6 +40,7 @@ const button: Button = {
? "#0091ff"
: "#f00404",
),
null,
);

let data: any;
Expand Down Expand Up @@ -68,6 +68,7 @@ const button: Button = {
.setTitle(`Voted for Option 1`)
.setDescription(e.slice(0, 10).join("\n").toString())
.setColor("#0598F6"),
null,
),
);

Expand Down Expand Up @@ -96,6 +97,7 @@ const button: Button = {
.setTitle(`Voted for Option 2`)
.setDescription(e.slice(0, 10).join("\n").toString())
.setColor("#F00605"),
null,
),
);

Expand Down
112 changes: 96 additions & 16 deletions src/commands/game/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
const { Leaderboard, write } = require("canvabase");
import {
EmbedBuilder,
SlashCommandBuilder,
ActionRowBuilder,
ButtonBuilder,
MessageActionRowComponentBuilder,
bold,
AttachmentBuilder,
} from "discord.js";
import { captureException } from "@sentry/node";
import { ChatInputCommand } from "../../models";
import Paginator from "../../util/pagination";
import { UserModel } from "../../util/Models/userModel";
import Canvas from "canvabase";

const command: ChatInputCommand = {
requireGuild: true,
Expand Down Expand Up @@ -49,19 +46,102 @@ const command: ChatInputCommand = {
execute: async (interaction, client, guildDb) => {
switch (interaction.options.getString("game")) {
case "higherlower":
const page = new Paginator({
user: interaction.user.id,
timeout: 180000,
client,
image: null,
});
try {
const page = new Paginator({
user: interaction.user.id,
timeout: 180000,
client,
});

const users = await UserModel.find({
"higherlower.highscore": { $gt: 1 },
});
let data2: any;
data2 = await UserModel.find({
"higherlower.highscore": { $gt: 1 },
}).sort({ "higherlower.highscore": -1 });


break;
(async function loop(z) {
let users: {
top: number;
tag: string;
score: number;
avatar: string;
}[] = [];
for (var i = 0; i < 10; i++) {
z++;

let data = data2[z];
let user = await client.users.fetch(data.userID);

users.push({
top: z,
avatar:
user.avatarURL() ||
"https://cdn.discordapp.com/embed/avatars/0.png",
tag: user.username,
score: data.higherlower.highscore,
});

if (i === 9) {
const leaderboard = new Leaderboard()
.setOpacity(0.7)
.setScoreMessage("Highest Score:")
.setColors({
box: "#212121",
username: "#ffffff",
score: "#ffffff",
firstRank: "#FFD700",
secondRank: "#C0C0C0",
thirdRank: "#CD7F32",
})
.addUserData(users);

let imga;
leaderboard.build().then((img: any) => {
//write("./src/data/Images/leaderboard.png", img);
imga = new AttachmentBuilder(img, { name: "leaderboard.png"});
});

page.add(
new EmbedBuilder()
.setTitle(`Leaderboard`)
.setImage("attachment://leaderboard.png")
.setColor("#F00605"),
imga,
);
}

if (z === 19) {
return page.start(interaction, null);
}
}
setTimeout(function () {
users = [];
loop(z);
}, 500);
})(0);
} catch (e) {
console.log(e);
}
// data2 = data2.map((e: any) =>
// page.add(
// new EmbedBuilder()
// .setTitle(`Voted for Option 2`)
// .setDescription(e.slice(0, 10).join("\n").toString())
// .setColor("#F00605"),
// null,
// ),
// );

// leaderboard.build().then((img: any) => {
// write("./src/data/Images/leaderboard.png", img);
// });

// const a = new AttachmentBuilder("./src/data/Images/leaderboard.png");
// const e = new EmbedBuilder()
// .setColor("#0598F6")
// .setImage("attachment://leaderboard.png");
// interaction.reply({ embeds: [e], files: [a] });

break;
}
},
};
Expand Down
6 changes: 5 additions & 1 deletion src/commands/settings/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ const command: ChatInputCommand = {
user: interaction.user.id,
timeout: 180000,
client,
image: null,
});

if (
Expand Down Expand Up @@ -427,6 +426,7 @@ const command: ChatInputCommand = {
)}\n\n${e.slice(0, 5).join("\n\n").toString()}`,
)
.setColor("#0795F6"),
null,
),
);
}
Expand Down Expand Up @@ -472,6 +472,7 @@ const command: ChatInputCommand = {
)}\n\n${e.slice(0, 5).join("\n\n").toString()}`,
)
.setColor("#0795F6"),
null,
),
);
}
Expand Down Expand Up @@ -516,6 +517,7 @@ const command: ChatInputCommand = {
)}\n\n${e.slice(0, 5).join("\n\n").toString()}`,
)
.setColor("#0795F6"),
null,
),
);
}
Expand Down Expand Up @@ -560,6 +562,7 @@ const command: ChatInputCommand = {
)}\n\n${e.slice(0, 5).join("\n\n").toString()}`,
)
.setColor("#0795F6"),
null,
),
);
}
Expand Down Expand Up @@ -604,6 +607,7 @@ const command: ChatInputCommand = {
)}\n\n${e.slice(0, 5).join("\n\n").toString()}`,
)
.setColor("#0795F6"),
null,
),
);
}
Expand Down
Binary file added src/data/Images/leaderboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { CacheType, Interaction } from "discord.js";
import {
CacheType,
Interaction,
InteractionDeferUpdateOptions,
} from "discord.js";
import { Event } from "../models";
import { UserModel } from "../util/Models/userModel";
import WouldYou from "../util/wouldYou";
Expand Down Expand Up @@ -186,6 +190,7 @@ const event: Event = {
.catch((err) => {
captureException(err);
});
return;
} else {
if (
guildDb.replayType === "Guild" &&
Expand All @@ -201,6 +206,7 @@ const event: Event = {
.catch((err) => {
captureException(err);
});
return;
} else if (
guildDb.replayType === "Channels" &&
client.used.has(
Expand All @@ -222,6 +228,7 @@ const event: Event = {
.catch((err) => {
captureException(err);
});
return;
}
}

Expand Down Expand Up @@ -262,6 +269,7 @@ const event: Event = {
interaction.user.id,
Date.now() + guildDb.replayCooldown,
);

setTimeout(
() => client.used.delete(interaction.user.id),
guildDb.replayCooldown,
Expand All @@ -272,6 +280,7 @@ const event: Event = {
interaction.guild?.id,
Date.now() + guildDb.replayCooldown,
);

setTimeout(
() => client.used.delete(interaction?.guild!.id),
guildDb.replayCooldown,
Expand Down
13 changes: 8 additions & 5 deletions src/util/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,33 @@ export default class Paginator {
private user: any;
private page: number;
private timeout: number;
private image: string | null;
private images: any[];

constructor({
user,
client,
timeout,
image,
}: {
user: any;
client: WouldYou;
timeout: number;
image: string | null;
}) {
this.pages = [];
this.client = client;
this.user = user;
this.page = 0;
this.timeout = timeout;
this.image = image;
this.images = [];
}

add(page: EmbedBuilder) {
add(page: EmbedBuilder, image: any | null) {
if (page.length) {
this.images.push(image);
this.pages.push(page);
return this;
}

this.images.push(image);
this.pages.push(page);
return this;
}
Expand Down Expand Up @@ -95,13 +96,15 @@ export default class Paginator {
};
}
const message = await interaction.reply({
files: this.images.length > 0 ? [this.images[0]] : null,
embeds: [this.pages[0]],
components: [buttons],
ephemeral: true,
});
this.client.paginate.set(
`${this.user}-${type ? type : interaction.message.id}`,
{
images: this.images,
pages: this.pages,
page: this.page,
message: message.id,
Expand Down

0 comments on commit 5781e06

Please sign in to comment.