from dotenv import load_dotenv
import discord, time, asyncio, math, random, traceback, sys
from async_timeout import timeout
from time import gmtime, strftime
from discord.ext import commands, tasks
from discord.utils import get
from discord.ext.commands import has_permissions
Cog = commands.Cog()

class Fun(Cog):
	def __init__(self, bot):
		self.bot = bot

	@command(name="hello", aliases=["hi"])
	async def say_hello(self, ctx):
		await ctx.send(f"{choice(('Hello', 'Hi', 'Hey', 'Hiya'))} {ctx.author.mention}!")

	@command(name="dice", aliases=["roll"])
	@cooldown(1, 60, BucketType.user)
	async def roll_dice(self, ctx, die_string: str):
		dice, value = (int(term) for term in die_string.split("d"))

		if dice <= 25:
			rolls = [randint(1, value) for i in range(dice)]

			await ctx.send(" + ".join([str(r) for r in rolls]) + f" = {sum(rolls)}")

		else:
			await ctx.send("I can't roll that many dice. Please try a lower number.")

	@command(name="slap", aliases=["hit"])
	async def slap_member(self, ctx, member: Member, *, reason: Optional[str] = "for no reason"):
		await ctx.send(f"{ctx.author.display_name} slapped {member.mention} {reason}!")

	@slap_member.error
	async def slap_member_error(self, ctx, exc):
		if isinstance(exc, BadArgument):
			await ctx.send("I can't find that member.")

	@command(name="echo", aliases=["say"])
	@cooldown(1, 15, BucketType.guild)
	async def echo_message(self, ctx, *, message):
		await ctx.message.delete()
		await ctx.send(message)

	@command(name="fact")
	@cooldown(3, 60, BucketType.guild)
	async def animal_fact(self, ctx, animal: str):
		if (animal := animal.lower()) in ("dog", "cat", "panda", "fox", "bird", "koala"):
			fact_url = f"https://some-random-api.ml/facts/{animal}"
			image_url = f"https://some-random-api.ml/img/{'birb' if animal == 'bird' else animal}"

			async with request("GET", image_url, headers={}) as response:
				if response.status == 200:
					data = await response.json()
					image_link = data["link"]

				else:
					image_link = None

			async with request("GET", fact_url, headers={}) as response:
				if response.status == 200:
					data = await response.json()

					embed = Embed(title=f"{animal.title()} fact",
								  description=data["fact"],
								  colour=ctx.author.colour)
					if image_link is not None:
						embed.set_image(url=image_link)
					await ctx.send(embed=embed)

				else:
					await ctx.send(f"API returned a {response.status} status.")

		else:
			await ctx.send("No facts are available for that animal.")
    @command(name="anime")
    async def anime(ctx):
      await ctx.send(file=discord.File('anime.jpg'))
      await bot.send_message(log_channel, embed=embed)
    @command(name="clory")
    async def clory(ctx):
      await ctx.send(file=discord.File('anime.jpg'))
      await bot.send_message(log_channel, embed=embed)
    @command(name="tellmeajoke")
    async def tellmeajoke(ctx):
	    randoma = random.randint(1,3)
	    if randoma == 1:
	      await ctx.send("What gets wet the more it dries? A towel")
	    if randoma == 2:
	      await ctx.send("What has four wheels and flies? A garbage truck")
	    if randoma == 3:
	      await ctx.send("You are a joke for running this command 😂")
    @command(name="dickhead")
    async def dickhead(ctx):
      await ctx.send("Cod Fish")
      await ctx.send(file=discord.File('myimage.png'))
    @command(name="dead")
    async def dead(ctx):
      await ctx.send(file=discord.File('nate.png'))
    @command(name="deadserver")
    async def deadserver(ctx):
      await ctx.send(file=discord.File('Fudgy.png'))
    @command(name="uwu")
    async def uwu(ctx):
      await ctx.send("Fuck off")     

	@Cog.listener()
	async def on_ready(self):
		if not self.bot.ready:
			self.bot.cogs_ready.ready_up("fun")


def setup(bot):
	bot.add_cog(Fun(bot))