stamp.js 5.04 KB
const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed } = require('discord.js');
const puppeteer = require('puppeteer');
const axios = require("axios");
const cheerio = require("cheerio");

module.exports ={
    data: new SlashCommandBuilder()
        .setName('각인')
        .setDescription('클래스별 로아와 상위 10명의 각인 분석 정보를 제공합니다.')
        .addStringOption((option) =>
            option.setName('클래스')
                .setDescription('각인 정보를 알고싶은 직업')
                .setRequired(true)),
    async execute(interaction){
        const class_name = interaction.options.getString('클래스'); 

        let job_number = 0;
        let class_number = 0;

        switch(class_name){
            case '디스트로이어' : job_number = 2; class_number = 2; break;
            case '버서커' : job_number = 2; class_number = 3; break;
            case '워로드' : job_number = 2; class_number = 4; break;
            case '홀리나이트' : job_number = 2; class_number = 5; break;
            case '기공사' : job_number = 3; class_number = 2; break;
            case '배틀마스터' : job_number = 3; class_number = 3; break;
            case '스트라이커' : job_number = 3; class_number = 4; break;
            case '인파이터' : job_number = 3; class_number = 5; break;
            case '창술사' : job_number = 3; class_number = 6; break;
            case '건슬링어' : job_number = 4; class_number = 2; break;
            case '데빌헌터' : job_number = 4; class_number = 3; break;
            case '블래스터' : job_number = 4; class_number = 4; break;
            case '스카우터' : job_number = 4; class_number = 5; break;
            case '호크아이' : job_number = 4; class_number = 6; break;
            case '바드' : job_number = 5; class_number = 2; break;
            case '서머너' : job_number = 5; class_number = 3; break;
            case '소서리스' : job_number = 5; class_number = 4; break;
            case '아르카나' : job_number = 5; class_number = 5; break;
            case '데모닉' : job_number = 6; class_number = 2; break;
            case '리퍼' : job_number = 6; class_number = 3; break;
            case '블레이드' : job_number = 6; class_number = 4; break;
            case '도화가' : job_number = 7; class_number = 2; break;
            default: interaction.reply('올바른 직업명을 입력해주세요!'); return;
        }
        
        await interaction.reply('작업중입니다!(1~2분 가량 소요될 수 있습니다)');

        await puppeteer.launch().then(async browser => {
            const page = await browser.newPage();
            await page.goto('https://loawa.com/rank');

            await page.click(`#contents > article > form > div > div:nth-child(${job_number}) > div > div:nth-child(2) > div > label:nth-child(${class_number}) > span`);
            await page.waitForSelector('#contents > article > table > tbody > tr:nth-child(20) > td:nth-child(11) > a > svg > path');
            await page.waitForTimeout(1000);

            let rank_number = 20;
            let all_name = [];

            for(var i = 1; i < rank_number + 1; i++){
                let name = await page.$eval(`#contents > article > table > tbody > tr:nth-child(${i}) > td:nth-child(2) > a`, element => {
                    return element.textContent;
                })
                all_name.push(name);
            }
            page.close();
            browser.close();
            
            let all_url = [];

            for(i = 0; i< rank_number; i++){
                url = 'https://lostark.game.onstove.com/Profile/Character/' + all_name[i];
                all_url.push(url);
            }

            var each_stamp = new Object;

            await axios.all(all_url.map((each) => axios.get(encodeURI(each)))).then(axios.spread((...responses) => {
                for(i = 0; i < all_url.length; i++){
                  let $ = cheerio.load(responses[i].data);
                  $('ul.swiper-slide').children('li').each(function(){
                      if($(this).find('span').text() in each_stamp){
                          each_stamp[$(this).find('span').text()] += 1;
                      } else{
                          each_stamp[$(this).find('span').text()] = 1;
                      }
                  })
                }
            })).catch(error => {
                console.error(error);
            })

            var sortary = [];
            for(var name in each_stamp){
                sortary.push([name, each_stamp[name] * 5]);
            }
            sortary.sort(function(a, b){
                return b[1] - a[1];
            })
            console.log(sortary);

/*             const stampembed = new MessageEmbed()
            .setColor('#F7819F')
            .setTitle(class_name)
            .setDescription(class_name + '의 상위 20명 각인 정보')
            .addField(
                {name: "각인 상세 정보", value: }
            ) */

            interaction.editReply('test');

        })
    }
}