import asyncio
import discord
import string
import random
from   urllib.parse import quote
from   discord.ext import commands
from   Cogs import Settings, PickList
from   Cogs import Message
from   Cogs import Nullify
from   Cogs import DL

def setup(bot):
	# Add the bot and deps
	settings = bot.get_cog("Settings")
	bot.add_cog(UrbanDict(bot, settings))

# This module grabs Urban Dictionary definitions

class UrbanDict(commands.Cog):

	# Init with the bot reference, and a reference to the settings var and xp var
	def __init__(self, bot, settings):
		self.bot = bot
		self.settings = settings
		self.ua = 'CorpNewt DeepThoughtBot'
		self.random = True

	@commands.command(pass_context=True)
	async def define(self, ctx, *, word : str = None):
		"""Gives the definition of the word passed."""

		# Check if we're suppressing @here and @everyone mentions
		if self.settings.getServerStat(ctx.message.guild, "SuppressMentions"):
			suppress = True
		else:
			suppress = False

		if not word:
			msg = 'Usage: `{}define [word]`'.format(ctx.prefix)
			await ctx.channel.send(msg)
			return
		url = "http://api.urbandictionary.com/v0/define?term={}".format(quote(word))
		msg = 'I couldn\'t find a definition for "{}"...'.format(word)
		title = permalink = None
		theJSON = await DL.async_json(url, headers = {'User-agent': self.ua})
		theJSON = theJSON["list"]
		if len(theJSON):
			# Got it - let's build our response
			words = []
			for x in theJSON:
				value = x["definition"]
				if x["example"]:
					value += "\n\n__Example(s):__\n\n*{}*".format(x["example"])
				words.append({
					"name":"{} - by {} ({} 👍 / {} 👎)".format(string.capwords(x["word"]),x["author"],x["thumbs_up"],x["thumbs_down"]),
					"value":value
				})
			return await PickList.PagePicker(title="Results For: {}".format(string.capwords(word)),list=words,ctx=ctx,max=1,url=theJSON[0]["permalink"]).pick()
		await ctx.send(Nullify.clean(msg))

	@commands.command(pass_context=True)
	async def randefine(self, ctx):
		"""Gives a random word and its definition."""

		# Check if we're suppressing @here and @everyone mentions
		if self.settings.getServerStat(ctx.message.guild, "SuppressMentions"):
			suppress = True
		else:
			suppress = False

		url = "http://api.urbandictionary.com/v0/random"
		title = permalink = None
		theJSON = await DL.async_json(url, headers = {'User-agent': self.ua})
		theJSON = theJSON["list"]
		if len(theJSON):
			# Got it - let's build our response
			x = random.choice(theJSON)
			value = x["definition"]
			if x["example"]:
				value += "\n\n__Example(s):__\n\n*{}*".format(x["example"])
			words = [{
				"name":"{} - by {} ({} 👍 / {} 👎)".format(string.capwords(x["word"]),x["author"],x["thumbs_up"],x["thumbs_down"]),
				"value":value
			}]
			return await PickList.PagePicker(title="Results For: {}".format(string.capwords(x["word"])),list=words,ctx=ctx,max=1,url=x["permalink"]).pick()
		await ctx.send(Nullify.clean(msg))

@@ -1544,24 +1544,155 @@
 example%22%5D:%0D%0A
+%09%09%09%09%09ex = x%5B%22example%22%5D.replace(%22*%22,%22%22)%0D%0A%09%09%09%09%09lines = %5B%22*%7B%7D*%22.format(y.strip()) if len(y.strip()) else %22%22 for y in ex.split(%22%5Cn%22)%5D%0D%0A
 %09%09%09%09%09value +
@@ -1709,36 +1709,34 @@
 xample(s):__%5Cn%5Cn
-*
 %7B%7D
-*
 %22.format(x%5B%22exam
@@ -1724,36 +1724,40 @@
 n%7B%7D%22.format(
-x%5B%22example%22%5D
+%22%5Cn%22.join(lines)
 )%0D%0A%09%09%09%09words
@@ -2736,24 +2736,153 @@
 example%22%5D:%0D%0A
+%09%09%09%09ex = x%5B%22example%22%5D.replace(%22*%22,%22%22)%0D%0A%09%09%09%09lines = %5B%22*%7B%7D*%22.format(y.strip()) if len(y.strip()) else %22%22 for y in ex.split(%22%5Cn%22)%5D%0D%0A
 %09%09%09%09value +=
@@ -2906,20 +2906,18 @@
 ):__%5Cn%5Cn
-*
 %7B%7D
-*
 %22.format
@@ -2917,28 +2917,32 @@
 .format(
-x%5B%22example%22%5D
+%22%5Cn%22.join(lines)
 )%0D%0A%09%09%09wo

Fix example formatting