디스코드 봇 만들기2

디스코드 봇 만들기2

안녕하세요.

오늘은 파이썬을 사용해서 디스코드 봇에게 간단한 인사정도를 할수있게 만들어보겠습니다.

  • 환경.
  • 초기 설정.
  • 간단한 명령어.
  • 실행결과.

환경.

운영체제: 윈도우10

파이썬: 3.9.2

초기설정.

[프로그래밍/파이썬] - 디스코드 봇 만들기-1

 

디스코드 봇 만들기-1

디스코드 봇 만들기-1. 안녕하세요. 오늘은 디스코드 봇을 만들어보겠습니다. *이번 글에서는 디스코드 봇을 생성하고 디스코드 서버에 추가만 해보겠습니다. 디스코드 회원가입. 디스코드 앱

intunknown.tistory.com

명령어를 입력하기전에 일단 봇을 실행해보겠습니다.

일단 CMD(명령프롬프트를 열고 pip로 discord 모듈을 다운받아주세요.)

pip install discord

아래 코드는 봇을 실행하는 코드입니다.

#이 코드는 bot.py
import discord
from discord.ext import commands
from to import Token

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

@bot.event
async def on_ready():
    print('로그인중입니다. ')
    print(f"봇={bot.user.name}로 연결중")
    print('연결이 완료되었습니다.')
    await bot.change_presence(status=discord.Status.online, activity=None)
    
bot.run(Token)#보안을위해 다른 코드(to.py)에서 토큰값을 가져옴.

토큰값 보관을 위한 코드.

-위의 코드에 토큰값을 직접 입력해도 됩니다.-

#to.py입니다.
Token="이곳에 봇의 토큰을 입력하세요."

실행결과.

간단한 명령어.

이제 봇에게 간단한 명령어를 추가해보겠습니다.

@bot.command(aliases=['hi'])
async def 안녕(ctx):
    await ctx.send('안녕하세요.')
@bot.command()
async def 따라하기(ctx,*,text):
    await ctx.send(text)

전체 코드는 다음과 같습니다.

#bot.py파일입니다.
import discord
from discord.ext import commands
from to import Token

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

@bot.event
async def on_ready():
    print('로그인중입니다. ')
    print(f"봇={bot.user.name}로 연결중")
    print('연결이 완료되었습니다.')
    await bot.change_presence(status=discord.Status.online, activity=None)
@bot.command(aliases=['hi'])
async def 안녕(ctx):
    await ctx.send('안녕하세요.')
@bot.command()
async def 따라하기(ctx,*,text):
    await ctx.send(text)
bot.run(Token)


#-----------------------------
#to.py파일입니다.
Token="토큰값"

실행결과.

위의 코드를 실행한 결과입니다.

./hi를 입력하거나 ./안녕을 입력하면 봇이 인사를 합니다.

./따라가기뒤에 원하는 글을 입력하면 봇이 이후의 글을 따라합니다.

 

Designed by JB FACTORY