From a34c50c92fd11914d5e48cb8c66feb3189899f23 Mon Sep 17 00:00:00 2001 From: Milka64 Date: Tue, 1 Oct 2019 14:54:27 +0200 Subject: [PATCH] add kaamelott command with API --- kabot/kabot/kabot.py | 37 ++++++++++++++++++++++++++++++++++++- kabot/setup.py | 2 +- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/kabot/kabot/kabot.py b/kabot/kabot/kabot.py index 85e4c5d..3665c25 100644 --- a/kabot/kabot/kabot.py +++ b/kabot/kabot/kabot.py @@ -7,12 +7,13 @@ import discord from discord.ext import commands import os +import requests class Mybot(commands.Cog): def __init__(self, bot): self.bot = bot - @commands.command() + @commands.command(help='test') async def join_channel(self, ctx): channel = [x for x in self.bot.get_all_channels() if x.name == "Général"][0] await channel.connect() @@ -21,6 +22,40 @@ class Mybot(commands.Cog): async def ping(self, message): 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() async def kadoc(self, ctx): quotes = [ diff --git a/kabot/setup.py b/kabot/setup.py index 71625db..4c6047b 100644 --- a/kabot/setup.py +++ b/kabot/setup.py @@ -11,7 +11,7 @@ with open('README.rst') as readme_file: with open('HISTORY.rst') as history_file: history = history_file.read() -requirements = ['discord.py' ] +requirements = ['discord.py','requests' ] setup_requirements = [ ]