Support for definition # in !urban, info > userinfo, server > serverinfo

This commit is contained in:
Twentysix 2016-06-08 21:01:49 +02:00
parent 153f272164
commit 881a1d3212

View File

@ -155,7 +155,7 @@ class General:
await self.bot.say(msg) await self.bot.say(msg)
@commands.command(pass_context=True, no_pm=True) @commands.command(pass_context=True, no_pm=True)
async def info(self, ctx, user : discord.Member = None): async def userinfo(self, ctx, user : discord.Member = None):
"""Shows users's informations""" """Shows users's informations"""
author = ctx.message.author author = ctx.message.author
if not user: if not user:
@ -175,7 +175,7 @@ class General:
await self.bot.say(data) await self.bot.say(data)
@commands.command(pass_context=True, no_pm=True) @commands.command(pass_context=True, no_pm=True)
async def server(self, ctx): async def serverinfo(self, ctx):
"""Shows server's informations""" """Shows server's informations"""
server = ctx.message.server server = ctx.message.server
online = str(len([m.status for m in server.members if str(m.status) == "online" or str(m.status) == "idle"])) online = str(len([m.status for m in server.members if str(m.status) == "online" or str(m.status) == "idle"]))
@ -199,20 +199,38 @@ class General:
await self.bot.say(data) await self.bot.say(data)
@commands.command() @commands.command()
async def urban(self, *, search_terms : str): async def urban(self, *, search_terms : str, definition_number : int=1):
"""Urban Dictionary search""" """Urban Dictionary search
Definition number must be between 1 and 10"""
# definition_number is just there to show up in the help
# all this mess is to avoid forcing double quotes on the user
search_terms = search_terms.split(" ") search_terms = search_terms.split(" ")
search_terms = "+".join(search_terms)
search = "http://api.urbandictionary.com/v0/define?term=" + search_terms
try: try:
async with aiohttp.get(search) as r: if len(search_terms) > 1:
pos = int(search_terms[-1]) - 1
search_terms = search_terms[:-1]
else:
pos = 0
if pos not in range(0, 11): # API only provides the
pos = 0 # top 10 definitions
except ValueError:
pos = 0
search_terms = "+".join(search_terms)
url = "http://api.urbandictionary.com/v0/define?term=" + search_terms
try:
async with aiohttp.get(url) as r:
result = await r.json() result = await r.json()
if result["list"] != []: if result["list"]:
definition = result['list'][0]['definition'] definition = result['list'][pos]['definition']
example = result['list'][0]['example'] example = result['list'][pos]['example']
await self.bot.say("**Definition:** " + definition + "\n\n" + "**Example:** " + example ) defs = len(result['list'])
await self.bot.say("**Definition #{} out of {}:\n**{}\n\n"
"**Example:\n**{}".format(pos+1, defs, definition, example))
else: else:
await self.bot.say("Your search terms gave no results.") await self.bot.say("Your search terms gave no results.")
except IndexError:
await self.bot.say("There is no definition #{}".format(pos+1))
except: except:
await self.bot.say("Error.") await self.bot.say("Error.")