본문 바로가기
Discord Bot/discord.py

👨‍💻Discord.py | 3장 봇 기본 명령어

by 알 수 없는 사용자 2020. 5. 4.

안녕하세요. 바로 3강 시작하겠습니다.

 

오늘은 Bot의 기본적인 코딩을 진행을 할 예정입니다. Discord.py를 배우기 전에 Python을

미리 배우고 discord.py를 하시는걸 추천드립니다.

 

Python은 해당 블로그 타 카테고리에서 배울 수 있습니다. 여기 에서 확인 할 수 있습니다.

 

또한 Discord.py 문서를 참고하시면 도움이 될 수 있습니다.

import discord
from discord.ext import commands

prefix = '?'
bot = commands.Bot(command_prefix = prefix)

@bot.event
async def on_ready():
    await bot.change_presence(status=discord.Status.online) 
    await bot.change_presence(status=discord.Status.idle) 
    await bot.change_presence(status=discord.Status.do_not_distutb)
    await bot.change_presence(status=discord.Status.offline)
# 봇이 온라인이되면 봇의 상태(Status)를 설정하는것인데.
# 순서대로 설명을 드리면 온라인, 자리비움, 다른 용무 중, 오프라인으로 설정하는것 입니다.
    
bot.run('your_token')

위와 같이 하실 경우에는 상태중에서 한가지만 선택하여 사용하셔야 됩니다.

import discord
from discord.ext import commands

prefix = '?'
bot = commands.Bot(command_prefix = prefix)

@bot.event
async def on_ready():
    await bot.change_presence(activity=discord.Game(name="xx"))
    await bot.change_presence(activity=discord.Streaming(name = 'xx', url='xx'))
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="XX"))
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="xx"))
# 봇의 상태외에도 게임을 설정을 할 수 있습니다. 종류에는 게임, 스트리밍, 듣는 중, 보는 중 으로 구분할 수 있습니다.
# 스트리밍의 경우 Twitch link를 넣으면 될거 같습니다. 

bot.run('your_token')

이것 또한 한가지를 선택하여 사용을 하시면 됩니다. 이제 상태 및 게임을 설정을 하려면 다음과 같이 사용하셔야 됩니다.

import discord
from discord.ext import commands

prefix = '?'
bot = commands.Bot(command_prefix = prefix)

@bot.event
async def on_ready():
    await bot.change_presence(status=discord.Status.online, activity=discord.Game('Discord.py'))

bot.run('your_token')

위와 같이 실행을 시키면 아래와 같이 봇이 온라인으로 설정이되며, 게임은 Discord.py 하는 중이라고 출력이 될것입니다. 

 

이제 기본적으로 명령어를 만들어 볼 생각입니다.

@bot.command()
async def 안녕하세요(ctx): # prefix+명령어를 봇이 있는 채널에 사용을 하면
    await ctx.send(f'{ctx.author.mention}님 안녕하세요!')
    # 해당 명령어를 사용한 사름을 언급 한 후 + 님 안녕하세요 ! 라고 출력을 하는겁니다.
    
@bot.command()
async def 핑(ctx):
    await ctx.send('퐁!')

위와 코드를 작성하여 실행을 시켜 prefix + 명령어 를 합치면 아래와 같이 출력이 됩니다.

 

오늘은 여기까지 하겠습니다. 다음 4강에서는 새로운 멤버가 입퇴장 할 때 사용하는 명령어 및 Embed 에 대해 알아보겠습니다.

댓글