디스코드 봇 주사위 -수정

디스코드 봇 주사위 -수정

안녕하세요.

오늘은 저번에 올린 디스코드 봇의 주사위 코드를 수정했습니다.

  • 이전 디스코드 봇 주사위 코드.
  • 수정한 내용.
  • 실행 결과.

이전 디스코드 봇 주사위 코드.

이전 디스코드 봇 주사위 코드는 아래 글에서 확인하세요.

[프로그래밍/파이썬] - 디스코드 봇 주사위

 

디스코드 봇 주사위

디스코드 봇 주사위 안녕하세요. 오늘은 저번에 만들었던 디스코드 봇에 간단하게 주사위를 추가해보겠습니다. 디스코드 봇 만들기. 디스코드 봇에 주사위 기능 추가. 디스코드 봇 만들기. 디스

intunknown.tistory.com

수정한 내용.

기존

-명령어를 입력하면 무조건 두개의 주사위만 굴러감.

수정후

-명령어를 입력했을때 뒤에 아무것도 입력하지 않으면 하나의 주사위만 굴러감.

-명령어 뒤에 숫자를 입력하면 개수만큼의 주사위가 굴러감.(but 한계가 있음.)

-명령어 뒤에 숫자가 있을땐 주사위의 합을 출력해줌.

-명령어 뒤에 숫자가 아닌 다른것이 입력되면 작동x

관리를 좀 더 편하게 하기위해서 파일을 나누었습니다.

#dicobot.py
import discord
import asyncio
from discord.ext import commands
import time
from to import Token #토큰을 위해 추가한 파일.
from plus import * #추가 기능을 편하게 수정하기 위해 추가한 파일 아래 코드 참고.
bot=commands.Bot(command_prefix='./')
@bot.event
async def on_ready():
    print('다음으로 로그인합니다: ')
    print(bot.user.name)
    print('bot connection was succesful')
    await bot.change_presence(status=discord.Status.online, activity=None)
@bot.command(aliases=['주사위'])
async def dice(ctx,text=None):
    if text==None:#추가 입력이 없으므로 하나만 굴림.
        embed = discord.Embed(title="주사위 굴리는중..", color=0x4432a8)
        dice1=dicedefine(1)
        embed.add_field(name=":game_die:",value=f"{dice1}",inline=True)
        await ctx.send(embed=embed)
    else:#추가입력이 있음.
        if text.isdigit():#숫자인지 확인.
            embed = discord.Embed(title="주사위 굴리는중..", color=0x4432a8)
            _sum=0 #주사위 합을 위해 변수초기화.
            text=int(text)
            for i in range(text):
                buf,dice1=dicedefine(2)
                _sum+=buf
                embed.add_field(name=f"{i+1}:game_die:",value=f"{dice1}",inline=True)
            embed.set_footer(text=f"주사위의 합= {_sum}")
            await ctx.send(embed=embed)
        else:
            await ctx.send("숫자만 가능합니다.")
bot.run(Token)

추가 기능을 편하게 수정하기 위한 파일.

#plus.py
import random
def dicedefine(i):
    dice0={1:'⚀=1',2:'⚁=2',3:'⚂=3',4:'⚃=4',5:'⚄=5',6:'⚅=6'}#딕셔너리
    dice=random.randrange(1,7)
    dice1=dice0[dice]
    if i==1:
        return dice1
    else:
        return dice,dice1

토큰을 위한 파일.

#to.py
Token="여기에 토큰을 넣으면 됩니다."

실행결과.

명령어를 그냥 입력했을때, 명령어 뒤에 숫자가 있을때, 명령어 뒤에 한글일때, 명령어 뒤에 영어일때.

Designed by JB FACTORY