๐Ÿค– ๋””์Šค์ฝ”๋“œ ๋ด‡ ๊ฐœ๋ฐœ์ผ์ง€ - 3

5647 ๋‹จ์–ด discorddiscord

๐Ÿ“š ๊ธฐ๋Šฅ์— ๋”ฐ๋ผ ์ฝ”๋“œ์ •๋ฆฌ, ์›นํฌ๋กค๋ง

์•„์ง์€ ๋ด‡์— ๋งŽ์€ ๊ธฐ๋Šฅ์ด ์ƒ๊ธด๊ฑด ์•„๋‹ˆ์ง€๋งŒ ๋‚˜์ค‘์„ ์œ„ํ•ด์„œ๋„ ์ฝ”๋“œ๋ฅผ ์ •๋ฆฌํ•˜๋Š”๊ฒŒ ๋‚˜์„๊ฑฐ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ์ฐพ์•„๋ณด๋‹ค๊ฐ€ Cog๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹คํ•ด์„œ ํ•ด๋ดค๋Š”๋ฐ

์–ด๋–ค ์‹์œผ๋กœ ํ• ์ง€ ์—ฌ๋Ÿฌ ์ž๋ฃŒ๋“ค์„ ์ฐพ๋‹ค๊ฐ€ event๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•œ์ •์ ์ธ ์ฝ”๋“œ๋ฐ–์— ๋ชป ์งœ๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋Š์ •๋„ ์ˆ™๋ จ๋œ๋‹ค๋ฉด event๋ฅผ ๋ฒ„๋ฆฌ๋ผ๋Š” ์กฐ์–ธ์˜ ๊ธ€์„ ๋ณด์•˜๋‹ค.

์ง€๊ธˆ๊นŒ์ง€ ์ง  ์ฝ”๋“œ์—๋Š” event๊ฐ€ ํˆฌ์„ฑ์ด์˜€๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋Šฅ์— ๋”ฐ๋ผ ์ •๋ฆฌํ•˜๋Š” ๊น€์— ๊ฐˆ์•„์—Ž๊ธฐ๋กœ ๊ฒฐ์ •ํ•˜์˜€๋‹ค.

#run.py

#run.py
import discord, asyncio, datetime, pytz, os
from discord.ext import commands
import os

client = commands.Bot(command_prefix="!") # ์ ‘๋‘์‚ฌ๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค.

for filename in os.listdir("basics"):
    if filename.endswith(".py"):
        client.load_extension(f"basics.{filename[:-3]}")

@client.event
async def on_ready():
    print("๊ณ ๋จ๋ฏธ ๋ด‡์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.")
    await client.change_presence(status=discord.Status.online, activity=discord.Game("๊ณ ๋จ๋ฏธ ๋ด‡์€ " + str(len(client.guilds)) + "๊ฐœ์˜ ์„œ๋ฒ„์— ์ ‘์†ํ•˜๊ณ  ์žˆ์–ด์š”!")) # ๋ด‡์˜ ์ƒํƒœ๋ฉ”์„ธ์ง€๋ฅผ ์„ค์ •ํ•œ๋‹ค.

# ๋ด‡์„ ์‹คํ–‰์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ๋ด‡์˜ ํ† ํฐ์„ ์ž‘์„ฑํ•˜๋Š” ๊ณณ
client.run("ํ† ํฐ ๊ฐ’")

๋ฉ”์ธ ์ฝ”๋“œ๋Š” ์ด๋Ÿฐ์‹์œผ๋กœ ์งฐ๋‹ค.

์–ด์ฐจํ”ผ ์ด๋ฏธ ์งœ๋†“์€ ์ฝ”๋“œ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธˆ๋ฐฉ ์งค ์ค„ ์•Œ์•˜๋Š”๋ฐ, ํŒŒ์ด์ฌ์„ ์•ˆํ•œ์ง€๋„ ์˜ค๋ž˜๋˜๊ณ  ์ž๋ฃŒ๋“ค์„ ์ฐพ๋Š”๋‹ค๊ณ  ๋‹ค์†Œ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋‹ค.

#simple.py

import discord, asyncio, datetime, pytz
from discord.ext import commands
from discord.utils import get
from discord import Member


client = commands.Bot(command_prefix='!')

class simple(commands.Cog): 

    def __init__(self, app):
        self.app = app 
    
    @commands.command(name="์ธ์‚ฌ") # '!์ธ์‚ฌ' ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค๋ฉด
    async def hi(self, ctx): 
        await ctx.send("{} | {} ๋‹˜ ์•ˆ๋…•ํ•˜์„ธ์š”!".format(ctx.author, ctx.author.mention)) # ์ž‘์„ฑ๋œ ์ฑ„๋„์— ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
        await ctx.author.send("{} | {} ๋‹˜ ์•ˆ๋…•ํ•˜์„ธ์š”!".format(ctx.author, ctx.author.mention)) # ์ž‘์„ฑํ•œ ์œ ์ €์—๊ฒŒ DM์œผ๋กœ ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

    @commands.command(name="์ •๋ณด") # '!์ •๋ณด'๋ฅผ ์ž…๋ ฅํ•œ๋‹ค๋ฉด
    async def information(self,ctx):
        embed = discord.Embed(title="๊ณ ๋จ๋ฏธ", description="ํ”„๋กœ์ ํŠธ์šฉ์œผ๋กœ ๊ฐœ๋ฐœ์ค‘์ธ ๋ด‡",timestamp=datetime.datetime.now(pytz.timezone('UTC')), color=0x4c2896)
        embed.add_field(name="๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ๋“ค ์ถ”๊ฐ€ ์˜ˆ์ •", value="์‹œ๊ฐ„๋‚  ๋–„๋งˆ๋‹ค ์ถ”๊ฐ€ํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.", inline=False)
        embed.add_field(name="GitHub", value="https://github.com/eastminn", inline=False)
        embed.set_footer(text="Bot Made by ๊น€๋™๋ฏผ#0026, ๋ฌธ์˜๋Š” DM์œผ๋กœ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค ๐Ÿ’ฌ", icon_url="https://cdn.discordapp.com/attachments/955360993729449987/955392163158569021/IMG_20220320_042000_013.jpg")
        embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/794866894615085087/956481187780644935/99pk9saw36t7q8t3o0gt.jpg")
        await ctx.send(embed=embed)

๋Œ€์ถฉ ์ด๋Ÿฐ์‹์œผ๋กœ ์ง€๊ธˆ์€ ๊ฐ„๋‹จํ•œ ๊ธฐ๋Šฅ๋งŒ ์žˆ์–ด์„œ ํ•œ ํŒŒ์ผ์— ์ฝ”๋“œ๋ฅผ ๋‹ค ์ ์—ˆ๋‹ค.

์ค‘๊ฐ„์— ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐ๋  ๋ฌธ์ œ์ธ๋ฐ ๊ณ„์† ์žก๊ณ  ์žˆ์–ด์„œ ๋Œ€๋žต 4์‹œ๊ฐ„ ์ •๋„ ๊ฑธ๋ฆฐ๊ฑฐ๊ฐ™๋‹ค.

๋‹ค๋ฅธ ๋ถ€๋ถ„์€ ๊ฑฐ์˜ ๋˜‘๊ฐ™์ด ๋งŒ๋“ค๊ณ , ์ธ์ฆ ๊ธฐ๋Šฅ ๋ถ€๋ถ„๋งŒ ์–ด๋Š์ •๋„ ์ˆ˜์ •์„ ํ–ˆ๋‹ค. ์›๋ž˜๋Š” ๊ถŒํ•œ์— ๋”ฐ๋ผ ์˜ค๋ฅ˜๋ฉ”์„ธ์ง€๋„ ์ถœ๋ ฅํ•˜๊ฒŒ ํ•˜๋ คํ–ˆ๋Š”๋ฐ
๊ณ„์† ๋ง‰ํ˜€์„œ ๋‹ค๋ฅธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋งŒ๋“ค๊ณ  ๋‚˜์ค‘์— ์†์„ ๋ด์•ผํ•  ๊ฑฐ ๊ฐ™๋‹ค.

์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค๊ณ ์‹ถ์–ด์„œ ์ตœ๊ทผ์— ๋งŽ์ด ๋ณธ ์›นํฌ๋กค๋ง์„ ํ™œ์šฉํ•ด์„œ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค๊ธฐ๋กœ ํ–ˆ๋‹ค.

๋ฌด๋‚œํ•œ ์ฃผ์ œ๋ฅผ ์ฐพ๋‹ค๊ฐ€ ์ฝ”๋กœ๋‚˜ 19์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํฌ๋กค๋งํ•ด์„œ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ๋ฐœ์ƒํ˜„ํ™ฉ์— ์žˆ๋Š” ์ผ์ผ ํ™•์ง„, ์žฌ์› ์œ„์ค‘์ฆ, ์‹ ๊ทœ ์ž…์›, ์ผ์ผ ์‚ฌ๋ง์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™”๋‹ค. ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์€ ์ •๋ณด์— ๋งˆ์šฐ์Šค๋ฅผ ๋Œ€๊ณ  ์šฐํด๋ฆญ์„ ํ•ด์„œ ๊ฒ€์‚ฌ๋ฅผ ๋ˆ„๋ฅด๊ณ  ๊ฒ€์‚ฌ์ฐฝ์„ ๋ณด๊ณ  ์ฝ”๋“œ๋ฅผ ์งœ๋ฉด ๋œ๋‹ค.

@commands.command(name="์ฝ”๋กœ๋‚˜")
    async def crawl(self, ctx):
        url = "https://search.naver.com/search.naver?where=nexearch&sm=tab_etc&qvt=0&query=%EC%BD%94%EB%A1%9C%EB%82%9819"
        response = requests.get(url)  # get ๋ฐฉ์‹์œผ๋กœ ์›น ์ •๋ณด ๋ฐ›์•„์˜ค๊ธฐ
        response_code = int(response.status_code)  # ์‘๋‹ต ์ฝ”๋“œ ๋ฐ›๊ธฐ
        
        if response_code == 200:  # ์ •์ƒ ์ž‘๋™(์ฝ”๋“œ 200 ๋ฐ˜ํ™˜) ์‹œ
            soup = BeautifulSoup(response.content, 'lxml')
            
        else:  # ์˜ค๋ฅ˜ ๋ฐœ์ƒ
            await ctx.send("์›น ํŽ˜์ด์ง€ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค.")
            
        # element
        new = soup.find("div", {"class":"status_info"}).findAll("p", {"class":"info_num"})
        today_infection = new[0].text # ์ผ์ผ ๊ฐ์—ผ์ž
        critical_condition = new[1].text # ์žฌ์› ์œ„์ค‘์ฆ
        new_Hospitalization = new[2].text # ์‹ ๊ทœ ์ž…์›
        today_died = new[3].text # ์ผ์ผ ์‚ฌ๋ง์ž

        # ๊ฐ€๋…์„ฑ์„ ์œ„ํ•œ ์ž„๋ฒ ๋“œ
        embed = discord.Embed(title="๐Ÿงฌ ์ฝ”๋กœ๋‚˜ 19", description="http://ncov.mohw.go.kr/ ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.",timestamp=datetime.datetime.now(pytz.timezone('UTC')), color=0x4c2896)
        embed.set_author(name="์ฝ”๋กœ๋‚˜ ๋ฐ”์ด๋Ÿฌ์Šค ๊ฐ์—ผ์ฆ - 19 (COVID-19)", url="http://ncov.mohw.go.kr/", icon_url="https://cdn.discordapp.com/attachments/779326874026377226/789094221533282304/Y5Tg6aur.png")
        embed.add_field(name="๋ˆ„์  ํ™•์ง„์ž", value="{}๋ช…".format(cumulative_infection), inline=True)
        embed.add_field(name="์ผ์ผ ํ™•์ง„์ž", value="{}๋ช…".format(today_infection), inline=True)
        embed.add_field(name="์žฌ์› ์œ„์ค‘์ฆ", value="{}๋ช…".format(critical_condition), inline=True)
        embed.add_field(name="์‹ ๊ทœ ์ž…์›", value="{}๋ช…".format(new_Hospitalization), inline=True)
        embed.add_field(name="์ผ์ผ ์‚ฌ๋ง์ž", value="{}๋ช…".format(today_died), inline=True)
        embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/956550003755741245/956610477184659486/9068ce115fb4499b9fc404664274e95e.png")
        embed.set_footer(text="์ž์„ธํ•œ ์ •๋ณด๋Š” ์œ„์˜ ํ”„๋กœํ•„ ๋ฒ„ํŠผ์˜ ์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•ด์ฃผ์„ธ์š”.", icon_url="https://cdn.discordapp.com/attachments/779326874026377226/789098688315654164/Flag_of_South_Korea.png")
        await ctx.send(embed=embed)

๋Œ€์ถฉ ์ฝ”๋“œ๋Š” BeautifulSoup ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด์„œ ์งฐ๊ณ  ๋„ˆ๋ฌด ์–ด์Šฌํผ์„œ ๋‚˜์ค‘์— ์ถ”๊ฐ€ํ• ๊ฒŒ ์žˆ์œผ๋ฉด ์ถ”๊ฐ€ํ•  ์˜ˆ์ •์ด๋‹ค.
ํฌ๋กค๋ง ์ฝ”๋“œ๋Š” ์›นํŽ˜์ด์ง€๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์ฝ”๋“œ๊ฐ€ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ง€์†์ ์œผ๋กœ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ํ•ด์ค˜์•ผํ•œ๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ