이것저것/디스코드 봇

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

pental 2020. 3. 29. 11:32
const request = require('request-promise');
const cheerio = require('cheerio');
const { RichEmbed } = require('discord.js');

async function getAddress(args) {
    var url = 'http://ncov.mohw.go.kr/index_main.jsp';
    let virus, virus_text;
    let virus_list = [];
    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); // 파싱
    $('.num').each(function() {
        virus = $(this);
        virus_text = virus.text();
        virus_list.push(virus_text);
    })
    const virus_result = virus_list.join(' / ');
    const title = ('국내 코로나 실시간 현황판');
    let description = '`확진자 / 사망자 / 완치자` \n`' + virus_result + '`';
    return new RichEmbed() // 리턴 끝
        .setTitle(title)
        .setDescription(description.split('\n'));
}
// ------------------------------------------------------------
module.exports = {
    name: '!코로나',
    description: '',
    async execute(message, args) {
        let result = null;
        if (!args.length) {
            result = new RichEmbed()
                .setTitle('국내 코로나 현황판')
                .setDescription('!코로나 k or !코로나 korea');
        } else {
            switch (args[0]) {
                case 'k':
                case 'korea':
                    result = await getAddress(args);
                    break;
                default:
                    result = `\`${args[0]}\` 은(는) 알 수 없는 명령이다`;
                    break;
            }
        }
        await message.channel.send(result);
    },
};