[Discord Bot] 마법의 소라고동 1편

김민창·2021년 10월 20일
0

discord bot

목록 보기
1/2
post-thumbnail
  • 파이썬이라는 언어도 모르고 학교선배를 놀리기위해 시작한 디스코드 봇 제작 회고록
  • 현재 가장 애착을 가지고 운영중인 프로젝트

Code

import discord
from discord.ext import commands

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

client = discord.Client()

token = (inputToken)

@bot.event
async def on_ready():
	print("김기담 뚱땡이!")
	game = discord.Game("pigman")
	await client.change_presence(status=discord.Status.online, activity=game)

@bot.command()
async def 김기담(ctx):
	await ctx.send("뚱땡이!")
@bot.command()
async def 이기훈(ctx):
	await ctx.send("알고리듬 플레티넘2!")
@bot.command()
async def 김민창(ctx):
	await ctx.send("너무잘생겨서 빛이납니다!")
@bot.command()
async def version(ctx):
	await ctx.send("ver 1.0")

bot.run(token)

Code Analysis

  • 지금 보면 너무나도 단순한 초기 코드다.

  • 단순 응답 핑퐁으로 구성되어 있다.

추가된 라이브러리

import discord
from discord.ext import commands
  • discord 라이브러리 하나가 추가되었다.

  • cmd를 열어 python -m pip install discord 를 통해 라이브러리 설치를 한다.

  • 파이썬을 활용하여 봇을 제작하기 위해 제공하는 가장 쉬운 라이브러리이다.


접두사 추가

bot = commands.Bot(command_prefix='!')
  • command_prefix='!' 코드를 통하여 사용자의 요청은 ! 로 시작해야 봇에서 읽는다는 의미다.

단순 응답 핑퐁

@bot.command()
async def 김기담(ctx):
	await ctx.send("뚱땡이!")
  • @bot.command() 로 함수임을 선언한다.
  • ! 다음에 온 사용자의 요청이 def 다음과 같다면 응답을 한다.
  • async, await 를 통해 비동기 처리를 했다.(순차적으로 진행되어야 하기 때문)

  • 놀랍게도 이정도만으로도 신기했었고, 잘놀았었다.

  • 다음은 embed 와 랜덤응답등을 가져오겠습니다.

profile
개발자 팡이

0개의 댓글