diff --git a/config/options.json b/config/options.json index 85706f4..be7adb4 100644 --- a/config/options.json +++ b/config/options.json @@ -8,5 +8,6 @@ "hypixelGuildID": "5a353a170cf2e529044f2935", "onlineLogChannel": "1101144489306886226", "botLogChannel": "1174403585149243472", + "guildLogChannel": "1183733282534326322", "instructionsgif": "https://cdn.discordapp.com/attachments/838716950723952640/1188211176300089384/4DMu513uNxbM.gif?ex=6599b2e4&is=65873de4&hm=e727c7a39aacbc47d6a5453f4b5f792a45679983c30d662cd258a311381b6df0&" } diff --git a/src/events/cron/test.ts b/src/events/cron/test.ts deleted file mode 100644 index 84db73d..0000000 --- a/src/events/cron/test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Cron } from "../../interfaces"; - -export = { - time: { - seconds: 0, - minutes: 0, - hours: 12, - dayOfMonth: 10, - month: 5, - dayOfWeek: 5 - }, - execute: () => { - console.log("Test cron executed!") - }, - onComplete: null, - start: true, - timeZone: "Europe/Zagreb" -} as Cron \ No newline at end of file diff --git a/src/events/cron/weeklyGexp.ts b/src/events/cron/weeklyGexp.ts new file mode 100644 index 0000000..74d70b8 --- /dev/null +++ b/src/events/cron/weeklyGexp.ts @@ -0,0 +1,137 @@ +import { + hypixelGuildID, + guildLogChannel, + color, + devMessage +} from "../../../config/options.json" +import { getGuild, getIGN } from "../../utils/Hypixel" +import { Cron, GuildData } from "../../interfaces" +import Illegitimate from "../../utils/Illegitimate" +import { GuildTextBasedChannel } from "discord.js" +const client = Illegitimate.client + +async function guildWeekly() { + const channel = client.channels.cache.get( + guildLogChannel, + ) as GuildTextBasedChannel + + if (!channel) { + console.log("Guild log channel not found") + return + } + + const embedColor = Number(color.replace("#", "0x")) + + const message = await channel.send({ + embeds: [{ + description: "Starting to fetch guild data...", + color: embedColor + }] + }) + + const guild = (await getGuild(hypixelGuildID, "id")) as GuildData + const guildMembersList: string[] = [] + const topWeeklyMembers: { name: string; value: string; inline: boolean }[] = + [] + + const guildName = guild.name + const guildMembers = guild.members + + const sliceSize = guildMembers.length / 4 + + // top weekly members + const allMembersGexpSorted = guildMembers + .map(member => { + return { + uuid: member.uuid, + exp: Object.values(member.expHistory).reduce( + (a, b) => a + b, + 0, + ), + } + }) + .sort((a, b) => b.exp - a.exp) + + for (let i = 0; i < allMembersGexpSorted.length; i++) { + const ign = await getIGN(allMembersGexpSorted[i].uuid) + const gexp = new Intl.NumberFormat("en-US").format( + allMembersGexpSorted[i].exp, + ) + const position = i + 1 + guildMembersList.push( + "**#" + position + " " + ign + ":** `" + gexp + "`", + ) + } + + const list = Array.from({ length: sliceSize }, (_, i) => + guildMembersList.slice(i * sliceSize, (i + 1) * sliceSize), + ) + + list.forEach((item, index) => { + if (item.length === 0) return + + topWeeklyMembers[index] = { + name: "", + value: item.join("\n"), + inline: false, + } + }) + + // combined weekly gexp + const allGuildMembersWeeklyXP = guildMembers.map( + member => member.expHistory, + ) + const guildMembersWeeklyXP = allGuildMembersWeeklyXP.map(member => { + return Object.values(member).reduce((a, b) => a + b, 0) + }) + const totalGuildMembersWeeklyXPUnformatted = guildMembersWeeklyXP.reduce( + (a, b) => a + b, + 0, + ) + const averageGuildMembersDailyXPUnformatted = + totalGuildMembersWeeklyXPUnformatted / 7 + + // final values + const totalGuildMembersWeeklyXP = new Intl.NumberFormat("en-US").format( + totalGuildMembersWeeklyXPUnformatted, + ) + const averageGuildMembersWeeklyXP = new Intl.NumberFormat("en-US").format( + averageGuildMembersDailyXPUnformatted, + ) + + await message.edit({ + embeds: [ + { + title: "**Weekly Guild XP for " + guildName + "**", + description: + "**Total weekly guild XP:** `" + + totalGuildMembersWeeklyXP + + "`\n**Average daily guild XP:** `" + + averageGuildMembersWeeklyXP + + "`", + color: embedColor, + fields: topWeeklyMembers, + timestamp: new Date().toISOString(), + footer: { + text: channel.guild.name + " | " + devMessage, + icon_url: channel.guild.iconURL({ forceStatic: false })!, + }, + }, + ], + }) +} + +export = { + time: { + seconds: 0, + minutes: 0, + hours: 21, + dayOfWeek: 7, + dayOfMonth: "*", + month: "*", + }, + execute: guildWeekly, + onComplete: null, + start: true, + timeZone: "Europe/Zagreb", +} as Cron diff --git a/src/index.ts b/src/index.ts index eefdd3f..0f9f149 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ import Illegitimate from "./utils/Illegitimate" -const illegitimate = new Illegitimate() +const illegitimate = new Illegitimate.Bot() illegitimate.start() diff --git a/src/utils/Autodeploy.ts b/src/utils/Autodeploy.ts index 0c152bd..4de5179 100644 --- a/src/utils/Autodeploy.ts +++ b/src/utils/Autodeploy.ts @@ -75,7 +75,7 @@ async function autoDeployCommands() { console.log( color.colorize("Commands are the same, skipping deploy.", "green"), ) - console.log(color.colorize(currentCmds, "green")) + console.log(color.colorize(newCmds, "green")) return } diff --git a/src/utils/Illegitimate.ts b/src/utils/Illegitimate.ts index 6927de1..360bf18 100644 --- a/src/utils/Illegitimate.ts +++ b/src/utils/Illegitimate.ts @@ -6,7 +6,7 @@ import init from "./Init" import { loadCronEvents } from "./Cron" const client = new Client() -export default class Illegitimate { +class Bot { constructor() {} async start() { @@ -21,3 +21,5 @@ export default class Illegitimate { }) } } + +export default { Bot, client } \ No newline at end of file diff --git a/src/utils/functions/account.ts b/src/utils/functions/account.ts index 2fc5913..58f1dd6 100644 --- a/src/utils/functions/account.ts +++ b/src/utils/functions/account.ts @@ -9,6 +9,7 @@ const mojanguuid = "https://sessionserver.mojang.com/session/minecraft/profile/" const hypixel = "https://api.hypixel.net/player" const guild = "https://api.hypixel.net/guild" const minotar = "https://minotar.net/helm/" +type GuildQuerqType = "player" | "name" | "id" async function getUUID(ign: string): Promise { try { @@ -45,7 +46,7 @@ async function getPlayer(uuid: string): Promise { async function getGuild( query: string, - type?: string, + type?: GuildQuerqType, ): Promise { const reqType = type ? type : "player"