add kaamelott command with API

This commit is contained in:
Milka64 2019-10-01 14:54:27 +02:00
parent f38e77a9fa
commit a34c50c92f
2 changed files with 37 additions and 2 deletions

View file

@ -7,12 +7,13 @@ import discord
from discord.ext import commands from discord.ext import commands
import os import os
import requests
class Mybot(commands.Cog): class Mybot(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
@commands.command() @commands.command(help='test')
async def join_channel(self, ctx): async def join_channel(self, ctx):
channel = [x for x in self.bot.get_all_channels() if x.name == "Général"][0] channel = [x for x in self.bot.get_all_channels() if x.name == "Général"][0]
await channel.connect() await channel.connect()
@ -21,6 +22,40 @@ class Mybot(commands.Cog):
async def ping(self, message): async def ping(self, message):
await message.channel.send('pong') await message.channel.send('pong')
@commands.command()
async def kaamelott(self, ctx, *args):
response = None
if not args:
url = 'https://kaamelott.chaudie.re/api/random'
elif args[0] in 'help':
response = "!kaamelott [livre] [character]"
await ctx.send(response)
return
else:
args = list(args)
if args[0].isdigit():
livre = int(args[0])
args.pop(0)
elif args[-1].isdigit():
livre = int(args[-1])
args.pop(-1)
else:
livre = None
if args:
perso = ' '.join(args)
if perso and livre:
url = 'https://kaamelott.chaudie.re/api/random/livre/%s/personnage/%s' % (livre,perso)
elif perso:
url = 'https://kaamelott.chaudie.re/api/random/personnage/%s' % perso
else:
url = 'https://kaamelott.chaudie.re/api/random/livre/%s' % livre
try:
citation = requests.get(url).json()['citation']
response = "%s : %s" % (citation['infos']['personnage'], citation['citation'])
except:
response = "Unknow error, try: !kaamelott [livre] [character]"
await ctx.send(response)
@commands.command() @commands.command()
async def kadoc(self, ctx): async def kadoc(self, ctx):
quotes = [ quotes = [

View file

@ -11,7 +11,7 @@ with open('README.rst') as readme_file:
with open('HISTORY.rst') as history_file: with open('HISTORY.rst') as history_file:
history = history_file.read() history = history_file.read()
requirements = ['discord.py' ] requirements = ['discord.py','requests' ]
setup_requirements = [ ] setup_requirements = [ ]