cgy12306

Riot API를 이용한 디스코드 봇 제작 본문

Programming/Python

Riot API를 이용한 디스코드 봇 제작

cgy12306 2021. 12. 4. 17:41

역시 시험기간엔 시험공부보다 딴짓하는게 더 재밌는듯 합니다.

 

평소에도 롤과 롤토체스를 자주 플레이 합니다.

 

롤토체스를 하다 문득 디스코드 봇을 제작하고 싶어져서 롤토체스 티어 검색해주는 디스코드 봇을 간단하게 만들어 봤습니다.

 

시험기간 + 롤토체스 + 코딩 = 못참지

 

Riot API

Riot API를 사용하기 위해서는 Riot Developer Portal에서 Riot API 키를 발급받을 수 있습니다.

https://developer.riotgames.com/

 

 

Riot Developer Portal

About the Riot Games API With this site we hope to provide the League of Legends developer community with access to game data in a secure and reliable way. This is just part of our ongoing effort to respond to players' and developers' requests for data and

developer.riotgames.com

 

 

 

API 키를 발급받기 위해서는 로그인을 해야하므로 우측 상단에 있는 LOGIN 버튼을 눌러 로그인 해줍니다.

 

로그인을 하게 되면 API 키를 받을 수 있습니다.

 

 

해당 API 키는 비밀스럽게 잘 간직해야 합니다.

 

이제 API를 사용하러 가봅니다.

 

https://developer.riotgames.com/apis

 

Riot Developer Portal

 

developer.riotgames.com

롤토체스 티어를 검색하기 위해 TFT-LEAGUE-V1를 사용해야합니다.

 

docs를 읽어보면 summornerId가 필요합니다.

 

summonerId는 암호화가 되어있다고 합니다.

 

해당 summonerId를 얻기 위해서는 SUMMONER-V4를 사용하면 됩니다.

 

 

SUMMONER-V4에서는 summerName을 입력하면 id를 반환해줍니다. 이걸 이용하여 TFT-LEAGUE-V1에 넘겨주면 됩니다.

 

디스코드 봇

우선 디스코드 봇을 제작하기 위해서는 아래 링크로 접속합니다.

 

https://discord.com/developers/applications

 

Discord Developer Portal — API Docs for Bots and Developers

Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.

discord.com

 

New Application을 눌러 이름을 지어주고 생성합니다.

 

Bot 탭에 들어가서 Add Bot을 눌러줍니다.

Yes, do it! 버튼을 눌러줍니다.

 

 

봇이 생성되면 OAuth2 탭에서 URL Generator을 눌러 bot과 Administrator에 체크를 한뒤 URL을 copy하고 접속합니다.

 

서버를 선택하여 봇을 해당 서버로 초대합니다.

 

 

이제 코딩을 하기 위해 봇의 Token을 복사합니다. 이 Token도 외부로 유출되면 안되기 때문에 비밀스럽게 잘 간직합니다.

 

도서관사서 봇은 이전에 만들어 두었던 봇입니다 ㅎㅎ.

 

서버로 초대하게 되면 봇이 오프라인으로 되어 있습니다. 현재 저는 봇을 켜놨기 때문에 온라인으로 되어 있습니다.

import requests, json, discord, asyncio, os
from discord.ext import commands
Token = ''
api_key = ''

game = discord.Game('로또체스')
bot = commands.Bot(command_prefix='.')

@bot.event
async def on_ready():
    await bot.change_presence(status=discord.Status.online, activity=game)

@bot.command(name='티어')
async def search(ctx, *, txt):
    print(txt)
    try:
        summonerName = txt.replace(" ", "")

        get_id_url = 'https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + summonerName + '?api_key=' + api_key
        res = requests.get(get_id_url)
        print(res)
        id = res.json()['id']

        get_info_summonors = 'https://kr.api.riotgames.com/tft/league/v1/entries/by-summoner/' + id + '?api_key=' + api_key
        res = requests.get(get_info_summonors)

        res = res.json()[0]
        print(res)
        tier = res['tier']
        rank = str(res['rank'])

        res = tier + rank
        await ctx.send(res)
    except:
        await ctx.send("쉬이이이이잇!!!!!!!!!정.숙!!!")

bot.run(Token)

 

성공적으로 디스코드 봇을 제작하였습니다.

 

참고 : https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#context

https://softvanilla.github.io/discordbot/discord_%EB%AA%85%EB%A0%B9%EC%96%B4_%EB%A7%8C%EB%93%A4%EA%B8%B0/

Comments