이 영역을 누르면 첫 페이지로 이동
포렌식 & 개발 이야기 - Forensics & Development 블로그의 첫 페이지로 이동

포렌식 & 개발 이야기 - Forensics & Development

페이지 맨 위로 올라가기

포렌식 & 개발 이야기 - Forensics & Development

Pental - Forensics / iOS / Windows / Android / Kakaotalk / Telegram / Etc

[Discord Bot] 롤 전적 검색기

  • 2020.02.08 14:25
  • 이것저것/디스코드 봇
글 작성자: pental
const request = require('request-promise');
const cheerio = require('cheerio');
const { RichEmbed } = require('discord.js');

async function getStat(args) {
    if (args.length < 2) {
        return '`!롤 <소환사명>` 형식으로 부탁';
    }
    // console.log(args);

    const search = args.slice(1, args.length).join(' ');
    // console.log(search);

    let url = 'https://www.op.gg/summoner/userName=' + encodeURIComponent(search);
    let options = {
        url: url,
        method: 'GET',
        headers: {
            'Accept-Language': 'ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) Gecko/20100101 Firefox/72.0'
        }
    }

    let html;
    try {
        html = await request(options); // html 받아옴
    } catch (e) {
        console.log("error");
        return "error";
    }

    var $ = cheerio.load(html); // 파싱
    let RankType, RankType_text;
    let TireRank, TireRank_text;
    let Wins, Wins_text;
    let Losses, Losses_text;
    let Winratio, Winratio_text;
    let Ranking, Ranking_text;
    let LeagueName, LeagueName_text;

    $('.RankType').each(function() { // 추출
        RankType = $(this);
        RankType_text = RankType.text();
        if (RankType_text == "Ranked Solo") {
            // console.log("솔로랭크");
            RankType_text = "솔랭";
        }
        //console.log(RankType_text);
    })
    $('.TierRank').each(function() {
        TireRank = $(this);
        TireRank_text = TireRank.text();
        // console.log(TireRank_text);
    })
    $('.wins').each(function() {
        Wins = $(this);
        Wins_text = Wins.text();
        //console.log(Wins_text);
    })
    $('.losses').each(function() {
        Losses = $(this);
        Losses_text = Losses.text();
        //console.log(Losses_text);
    })
    $('.winratio').each(function() {
        Winratio = $(this);
        Winratio_text = Winratio.text();
        //console.log(Winratio_text);
    })
    $('.ranking').each(function() {
        Ranking = $(this);
        Ranking_text = Ranking.text();
        //console.log(Ranking_text);
    })
    $('.LeagueName').each(function() {
        LeagueName = $(this);
        LeagueName_text = LeagueName.text();
        //console.log(LeagueName_text);
    })

    let lol_photo = $('.Medal.tip img').attr('src');
    if (lol_photo == undefined) {
        console.log("UnRank Err.");
        return (search + ' 유저 : `Unranked`');
    }
    //await RankType_text;
    var LOL_Result = RankType_text + " : `" + TireRank_text + "`\n" + "순위 : `" + Ranking_text + "위`\n" + Wins_text + " / " + Losses_text + ' / ' + Winratio_text;
    var LOL_Result_2 = (LOL_Result + LeagueName_text);
    const title = ('`' + search + '`의 `프로필`');
    let description = LOL_Result_2;
    let image = ('http:' + lol_photo);
    // console.log(image);
    // console.log(title);
    return new RichEmbed() // 리턴 끝
        .setTitle(title)
        .setDescription(description)
        .setThumbnail(image);
}

// --------------------------------------------------------------- 

async function Profile(args) {
    if (args.length < 2) {
        return '`!롤 <소환사명>` 형식으로 부탁';
    }
    // console.log(args);

    const search = args.slice(1, args.length).join(' ');
    // console.log(search);

    let url = 'https://www.op.gg/summoner/userName=' + encodeURIComponent(search);
    let options = {
        url: url,
        method: 'GET',
        headers: {
            'Accept-Language': 'ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) Gecko/20100101 Firefox/72.0'
        }
    }

    let html;
    try {
        html = await request(options); // html 받아옴
    } catch (e) {
        console.log("error");
        return "error";
    }

    var $ = cheerio.load(html); // 파싱
    let Level, Level_text;

    $('.Level.tip').each(function() {
        Level = $(this);
        Level_text = Level.text();
        // console.log(LeagueName_text);
    })

    let lol_profile = $('.ProfileIcon img').attr('src');
    if (lol_profile == undefined) {
        console.log("User Erorr.");
        return (search + ' `유저를 찾을수 없음.`');
    }
    //await RankType_text;
    var LOL_Result = "레벨 : " + Level_text + "Lv";
    const title = (search + '의 `프로필`');
    let description = LOL_Result;
    let image = ('http:' + lol_profile);
    const user_url = url;
    // console.log(image);
    // console.log(title);
    return new RichEmbed() // 리턴 끝
        .setTitle(title)
        .setDescription(description)
        .setThumbnail(image)
        .setURL(user_url);
}

// --------------------------------------------------------

async function Recent(args) {
    if (args.length < 2) {
        return '`!롤 <소환사명>` 형식으로 부탁';
    }
    // console.log(args);

    const search = args.slice(1, args.length).join(' ');
    // console.log(search);

    let url = 'https://www.op.gg/summoner/userName=' + encodeURIComponent(search);
    let options = {
        url: url,
        method: 'GET',
        headers: {
            'Accept-Language': 'ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) Gecko/20100101 Firefox/72.0'
        }
    }

    let html;
    try {
        html = await request(options); // html 받아옴
    } catch (e) {
        console.log("error");
        return "error";
    }

    var $ = cheerio.load(html); // 파싱
    let WinRatioTitle, WinRatioTitle_text;
    let test1, test2, test3, test5, test6;
    let WinRatioTitle_Result;
    let Win_Count;

    $('.WinRatioTitle').each(function() {
        WinRatioTitle = $(this);
        WinRatioTitle_text = WinRatioTitle.text();
        test1 = WinRatioTitle_text.split('\n')[1];
        test2 = WinRatioTitle_text.split('\n')[2];
        test3 = WinRatioTitle_text.split('\n')[3];
        test5 = test1 + test2 + test3;
        Win_Count = test2.replace(/[^0-9]/g, "");
        //console.log(Win_Count);
        WinRatioTitle_Result = test5.replace(/(^\s*)|(\s*$)/g, "");
        //WinRatioTitle_Result = test.replace(/(\s*)/g, "");
    })

    let lol_profile = $('.ProfileIcon img').attr('src');
    if (lol_profile == undefined) {
        console.log("User Erorr.");
        return (search + ' `유저를 찾을수 없음.`');
    }
    //await RankType_text;
    var LOL_Result = "최근 20게임 : " + WinRatioTitle_Result;
    var LOL_Ratio_Result = LOL_Result + "\n" + "승률 : " + (Win_Count * 5) + "%";
    const title = (search + '의 `프로필`');
    let description = LOL_Ratio_Result;
    let image = ('http:' + lol_profile);
    const user_url = url;
    // console.log(image);
    // console.log(title);
    return new RichEmbed() // 리턴 끝
        .setTitle(title)
        .setDescription(description)
        .setThumbnail(image)
        .setURL(user_url);
}

// ------------------------------------------------------------

module.exports = {
    name: '!롤',
    description: '',

    async execute(message, args) {
        let result = null;

        // bs
        if (!args.length) {
            result = new RichEmbed()
                .setTitle('롤 전적 검색기')
                .setDescription('`!롤 <명령어>`')
                .addField('프로필 정보', '`!롤 프로필 <소환사명>` 또는 `!롤 p <소환사명>`')
                .addField('랭크 정보', '`!롤 솔랭 <소환사명>` 또는 `!롤 s <소환사명>`')
                .addField('최근 정보', '`!롤 최근 <소환사명>` 또는 `!롤 r <소환사명>`');
        }

        // lol <command>
        else {
            switch (args[0]) {
                // lol s
                // lol stat
                // lol stats
                case '솔랭':
                case 's':
                case 'stat':
                case 'stats':
                    result = await getStat(args);
                    break;

                case '프로필':
                case 'p':
                case 'profile':
                    result = await Profile(args);
                    break;

                case '최근':
                case 'r':
                case 'recent':
                    result = await Recent(args);
                    break;

                    // lol ...
                default:
                    result = `\`${args[0]}\` 은(는) 알 수 없는 명령이다`;
                    break;
            }
        }

        await message.channel.send(result);
    },
};
저작자표시 비영리 (새창열림)

'이것저것 > 디스코드 봇' 카테고리의 다른 글

[Discord Bot] 코로나 확진자 보여주기  (0) 2020.03.29
[Discord Bot] 미세먼지 시각화로 보여주기  (0) 2020.03.29
[Discord Bot] 네이버 실시간 급상승 Top20  (0) 2020.02.08

댓글

이 글 공유하기

  • 구독하기

    구독하기

  • 카카오톡

    카카오톡

  • 라인

    라인

  • 트위터

    트위터

  • Facebook

    Facebook

  • 카카오스토리

    카카오스토리

  • 밴드

    밴드

  • 네이버 블로그

    네이버 블로그

  • Pocket

    Pocket

  • Evernote

    Evernote

다른 글

  • [Discord Bot] 코로나 확진자 보여주기

    [Discord Bot] 코로나 확진자 보여주기

    2020.03.29
  • [Discord Bot] 미세먼지 시각화로 보여주기

    [Discord Bot] 미세먼지 시각화로 보여주기

    2020.03.29
  • [Discord Bot] 네이버 실시간 급상승 Top20

    [Discord Bot] 네이버 실시간 급상승 Top20

    2020.02.08
다른 글 더 둘러보기

정보

포렌식 & 개발 이야기 - Forensics & Development 블로그의 첫 페이지로 이동

포렌식 & 개발 이야기 - Forensics & Development

  • 포렌식 & 개발 이야기 - Forensics & Development의 첫 페이지로 이동

검색

메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

카테고리

  • Category (444) N
    • Forensics (104)
      • Magnet AXIOM (28)
      • Digital Forensics Informati.. (9)
      • Iphone Forensics (23)
      • DFC (7)
      • 디지털포렌식전문가2급 자격증 (10)
      • FTK ACE 자격증 (7)
    • 이것저것 (7)
      • Ubuntu (6)
      • 디스코드 봇 (4)
      • Volatility GUI (2)
    • CTF (32)
      • NEWSECU (14)
      • CTF-d (5)
      • Puzzel - Network Forensics (2)
      • Security Traps (2)
      • system32.kr (5)
      • HMCTF (4)
    • Programming (253) N
      • C (10)
      • Python (11)
      • 백준 (199) N
      • 프로그래머스 (32)
    • 그냥 개발 및 잡담 (16)
      • Docker (2)
      • Google Cloud (3)
      • OS 개발 (3)
    • Best of Best (20)

최근 글

인기 글

댓글

공지사항

아카이브

태그

  • axiom
  • 디지털포렌식
  • pental
  • 파이썬
  • 프로그래머스
  • 백준
  • 포렌식
  • Forensics
  • 전체 보기…

정보

pental의 포렌식 & 개발 이야기 - Forensics & Development

포렌식 & 개발 이야기 - Forensics & Development

pental

블로그 구독하기

  • 구독하기
  • RSS 피드

방문자

  • 전체 방문자
  • 오늘
  • 어제

티스토리

  • 티스토리 홈
  • 이 블로그 관리하기
  • 글쓰기
Powered by Tistory / Kakao. Copyright © pental.

티스토리툴바