import { Command, ChatInputCommand } from '@sapphire/framework'; import { sapphire, mineflayer } from '../index'; import { MessageButton, MessageEmbed, MessageActionRow } from 'discord.js'; import request from 'request-promise'; import cheerio from 'cheerio'; const buttonPages = async(interaction: any, pages: any, buttonList: any, timeout = 120000 ) => { let page = 0; const row = new MessageActionRow().addComponents(buttonList); if (interaction.deferred == false) { await interaction.deferReply(); }; const curPage = await interaction.editReply({ embeds: [pages[page].setFooter({ text: `Page ${page + 1} / ${pages.length}` })], components: [row], fetchReply: true, }); const filter = (i: any) => i.customId === buttonList[0].customId || i.customId === buttonList[1].customId; const collector = await curPage.createMessageComponentCollector({ filter, time: timeout }); collector.on("collect", async (i: any) => { switch (i.customId) { case buttonList[0].customId: page = page > 0 ? --page : pages.length - 1; break; case buttonList[1].customId: page = page + 1 < pages.length ? ++page : 0; break; default: break; }; await i.deferUpdate(); await i.editReply({ embeds: [pages[page].setFooter({ text: `Page ${page + 1} / ${pages.length}` })], components: [row], }); collector.resetTimer(); }); collector.on("end", (placeholder: any, reason: any) => { if (reason !== "messageDelete") { const disabledRow = new MessageActionRow().addComponents( buttonList[0].setDisabled(true), buttonList[1].setDisabled(true) ); curPage.edit({ embeds: [pages[page].setFooter({ text: `Page ${page + 1} / ${pages.length}` })], components: [disabledRow], }); }; }); return curPage; }; export class PingCommand extends Command { public constructor(context: Command.Context, options: Command.Options) { super(context, { ...options }); }; public override registerApplicationCommands(registry: ChatInputCommand.Registry) { registry.registerChatInputCommand(async (builder) => builder.setName('leaderboard').setDescription('Fetch hypixel leaderboards').addStringOption(option => option.setName('type').setDescription('The type of leaderboard you would like to see').setRequired(true).addChoices( { name: 'guild', value: 'guild' } )) ); }; public async chatInputRun(interaction: Command.ChatInputInteraction) { const option = interaction.options.getString('type') if (option === "guild") { const result = await request.get("https://sk1er.club/leaderboards/newdata/GUILD_LEVEL"); const $ = cheerio.load(result); function name(num: any) { if (!num) throw new TypeError("Invalid number given"); if (num === 0) throw new TypeError("Invalid number given"); return $(`#leaderboard-table-table > tbody > tr:nth-child(${new String(num)}) > td:nth-child(3)`).text(); }; function level(num: any) { if (!num) throw new TypeError("Invalid number given"); if (num === 0) throw new TypeError("Nobody can have the 0 placement on the leaderboard"); return $(`#leaderboard-table-table > tbody > tr:nth-child(${new String(num)}) > td:nth-child(4)`).text(); }; function date(num: any) { if (!num) throw new TypeError("Invalid number given"); if (num === 0) throw new TypeError("Nobody can have the 0 placement on the leaderboard"); const unformatted1 = $(`#leaderboard-table-table > tbody > tr:nth-child(${new String(num)}) > td:nth-child(8)`).text(); const unformatted = unformatted1.split('-'); return unformatted[1] + '/' + unformatted[0] + '/' + unformatted[2]; }; const top10 = `\`\`\` 1. ${name(1)} (${level(1)}) [${date(1)}] 2. ${name(2)} (${level(2)}) [${date(2)}] 3. ${name(3)} (${level(3)}) [${date(3)}] 4. ${name(4)} (${level(4)}) [${date(4)}] 5. ${name(5)} (${level(5)}) [${date(5)}] 6. ${name(6)} (${level(6)}) [${date(6)}] 7. ${name(7)} (${level(7)}) [${date(7)}] 8. ${name(8)} (${level(8)}) [${date(8)}] 9. ${name(9)} (${level(9)}) [${date(9)}] 10. ${name(10)} (${level(10)}) [${date(10)}] \`\`\`` const top20 = `\`\`\` 11. ${name(11)} (${level(11)}) [${date(11)}] 12. ${name(12)} (${level(12)}) [${date(12)}] 13. ${name(13)} (${level(13)}) [${date(13)}] 14. ${name(14)} (${level(14)}) [${date(14)}] 15. ${name(15)} (${level(15)}) [${date(15)}] 16. ${name(16)} (${level(16)}) [${date(16)}] 17. ${name(17)} (${level(17)}) [${date(17)}] 18. ${name(18)} (${level(18)}) [${date(18)}] 19. ${name(19)} (${level(19)}) [${date(19)}] 20. ${name(20)} (${level(20)}) [${date(20)}] \`\`\`` const top30 = `\`\`\` 21. ${name(21)} (${level(21)}) [${date(21)}] 22. ${name(22)} (${level(22)}) [${date(22)}] 23. ${name(23)} (${level(23)}) [${date(23)}] 24. ${name(24)} (${level(24)}) [${date(24)}] 25. ${name(25)} (${level(25)}) [${date(25)}] 26. ${name(26)} (${level(26)}) [${date(26)}] 27. ${name(27)} (${level(27)}) [${date(27)}] 28. ${name(28)} (${level(28)}) [${date(28)}] 29. ${name(29)} (${level(29)}) [${date(29)}] 30. ${name(30)} (${level(30)}) [${date(30)}] \`\`\`` const top40 = `\`\`\` 31. ${name(31)} (${level(31)}) [${date(31)}] 32. ${name(32)} (${level(32)}) [${date(32)}] 33. ${name(33)} (${level(33)}) [${date(33)}] 34. ${name(34)} (${level(34)}) [${date(34)}] 35. ${name(35)} (${level(35)}) [${date(35)}] 36. ${name(36)} (${level(36)}) [${date(36)}] 37. ${name(37)} (${level(37)}) [${date(37)}] 38. ${name(38)} (${level(38)}) [${date(38)}] 39. ${name(39)} (${level(39)}) [${date(39)}] 40. ${name(40)} (${level(40)}) [${date(40)}] \`\`\`` const top50 = `\`\`\` 41. ${name(41)} (${level(41)}) [${date(41)}] 42. ${name(42)} (${level(42)}) [${date(42)}] 43. ${name(43)} (${level(43)}) [${date(43)}] 44. ${name(44)} (${level(44)}) [${date(44)}] 45. ${name(45)} (${level(45)}) [${date(45)}] 46. ${name(46)} (${level(46)}) [${date(46)}] 47. ${name(47)} (${level(47)}) [${date(47)}] 48. ${name(48)} (${level(48)}) [${date(48)}] 49. ${name(49)} (${level(49)}) [${date(49)}] 50. ${name(50)} (${level(50)}) [${date(50)}] \`\`\`` const embed = new MessageEmbed() .setTitle('Guild Level Leaderboard') .setDescription(top10) .setColor("RED") .setTimestamp() const embed1 = new MessageEmbed() .setTitle('Guild Level Leaderboard') .setDescription(top20) .setColor('RED') .setTimestamp() const embed2 = new MessageEmbed() .setTitle('Guild Level Leaderboard') .setDescription(top30) .setColor("RED") .setTimestamp() const embed3 = new MessageEmbed() .setTitle('Guild Level Leaderboard') .setDescription(top40) .setColor("RED") .setTimestamp() const embed4 = new MessageEmbed() .setTitle('Guild Level Leaderboard') .setDescription(top50) .setColor("RED") .setTimestamp() const back = new MessageButton() .setCustomId("previousbtn") .setStyle("DANGER") .setLabel("Back") const foward = new MessageButton() .setCustomId("nextbtn") .setStyle("SUCCESS") .setLabel("Foward") const buttons = [ back, foward ]; const embeds = [ embed, embed1, embed2, embed3, embed4 ]; await buttonPages(interaction, embeds, buttons, 120000); }; }; };