초보개발자의 초보블로그
Hello World

Coding/discord

디스코드 봇 개발현황 #1 튜토리얼

Complish_dev 2021. 6. 1. 09:56
반응형

이 글은 파이썬 기본문법을 익히고 온 분들에게 권한다

discord.py는 파이썬용 디스코드 봇 API이다. 

개발 진입장벽이 낮은 discord.py를 입문으로서 많이 선택한다. 

하지만 다른 언어에 비해 상대적으로 쉽다는 것이지 

기본적인 파이썬 사용 방법을 모른다면 아무것도 할 수 없으므로 꼭 기초적인 문법을 익히고 공부하자.

 

당연하지만 먼저 Python을 설치해야 한다. 

 

Welcome to Python.org

The official home of the Python Programming Language

www.python.org

위 링크에서 '파이썬'을 먼저 다운 받자. (이미 다운받은 분들은 아래로 스크롤)

본인의 OS에 맞게 설치하자. 

편집기는 IDLE이라는 프로그램이 알아서 깔리니 걱정하지 않아도 된다.

파이썬 버전은 3.5.3 이상을 사용해야하며, 개발하기 전 파이썬 버전은 3.6 이상을 사용하는 것을 추천한다. 

 

우선 크게 두가지로 나눌수 있다.

신식과 구식 신식이 아무래도 세분 및 체계화가 잘 되어있다.

 

[구버전 코드 Client() 예제]

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

@client.event
async def on_message(message):
    if message.content.startswith('!ping'):
        await message.channel.send('pong')

client.run('token')

이러한 방식은 코드량이 많아질 수록 지저분해지고 세분 및 체계화가 어렵다. 

이러한 점을 해결하기 위해 추가된게 discord.ext 폴더 내에 있는 discord.ext.commands.Bot 객체 방식이다.

 

[신버전 코드 github에 소개 되어있는 commands.Bot 예제]

import discord
from discord.ext import commands

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

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.command()
async def ping(ctx):
    await ctx.send('pong')

bot.run('token')
728x90
반응형
loading