You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.6KB

  1. import os
  2. import json
  3. import random
  4. import aiohttp
  5. import discord
  6. import requests
  7. from discord.ext.commands import bot, is_owner
  8. class Util():
  9. """
  10. A cog that offers utility commands.
  11. """
  12. def __init__(self, bot_client):
  13. self.bot = bot_client
  14. @bot.command()
  15. async def avatar(self, ctx, *,user: discord.User = None):
  16. """
  17. Returns a mentioned users avatar
  18. Example:
  19. {command_prefix}avatar @RoxBot#4170
  20. {command_prefix}avatar RoxBot
  21. """
  22. if not user:
  23. user = ctx.author
  24. url = user.avatar_url
  25. if url.split(".")[-1] == "gif":
  26. avaimg = 'avaimg.gif'
  27. else:
  28. avaimg = 'avaimg.webp'
  29. async with aiohttp.ClientSession() as session:
  30. async with session.get(url) as img:
  31. with open(avaimg, 'wb') as f:
  32. f.write(await img.read())
  33. await ctx.send(file=avaimg)
  34. os.remove(avaimg)
  35. @bot.command()
  36. async def info(self, ctx, member: discord.Member = None):
  37. """
  38. Gets info for a mentioned user
  39. Example:
  40. {command_prefix}info @RoxBot#4170
  41. {command_prefix}info RoxBot
  42. """
  43. if not member:
  44. member = ctx.author
  45. if member.activity.type == discord.ActivityType.playing:
  46. activity = "Playing **{}**".format(member.activity.name)
  47. elif member.activity.type == discord.ActivityType.streaming:
  48. activity = "Streaming **{}**".format(member.activity.name)
  49. elif member.activity.tyoe == discord.ActivityType.listening:
  50. activity = "Listening to **{} by {}**".format(member.activity.title, member.activity.artist)
  51. else:
  52. activity = ""
  53. colour = member.colour.value
  54. avatar = member.avatar_url
  55. embed = discord.Embed(colour=colour, description=activity)
  56. embed.set_thumbnail(url=avatar)
  57. embed.set_author(name=str(member), icon_url=avatar)
  58. embed.add_field(name="ID", value=member.id)
  59. embed.add_field(name="Status", value=member.status)
  60. if member.nick:
  61. embed.add_field(name="Nickname", value=member.nick)
  62. embed.add_field(name="Account Created", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(member.created_at), inline=True)
  63. embed.add_field(name="Joined Server", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(member.joined_at), inline=True)
  64. roles = ""
  65. count = 0
  66. for role in member.roles.reverse():
  67. if role == ctx.server.default_role:
  68. pass
  69. else:
  70. roles += role.name + ", "
  71. count += 1
  72. if not roles:
  73. roles = "None"
  74. count = 0
  75. embed.add_field(name="Roles [{}]".format(count), value=roles.strip(", "))
  76. return await ctx.send(embed=embed)
  77. @bot.command()
  78. async def upload(self, ctx):
  79. """
  80. Uploads selected file to the host, thanks to the fact that
  81. every pomf.se based site has pretty much the same architecture.
  82. """
  83. sites = [
  84. "https://comfy.moe/",
  85. "https://safe.moe/api/",
  86. "http://up.che.moe/",
  87. "https://mixtape.moe/",
  88. "https://pomf.cat/",
  89. "https://sugoi.vidyagam.es/",
  90. "https://doko.moe/",
  91. "https://pomfe.co/",
  92. "https://pomf.space/",
  93. "https://vidga.me/",
  94. "https://pomf.pyonpyon.moe/"
  95. ] # List of pomf clone sites and upload limits
  96. async with ctx.channel.typing():
  97. if ctx.message.attachments:
  98. # Site choice, shouldn't need an upload size check since max upload for discord atm is 50MB
  99. site = random.choice(sites)
  100. urls = []
  101. for attachment in ctx.message.attachments:
  102. name = attachment['url'].split("/")[-1]
  103. # Download File
  104. with aiohttp.ClientSession() as session:
  105. async with session.get(attachment['url']) as img:
  106. with open(name, 'wb') as f:
  107. f.write(await img.read())
  108. # Upload file
  109. with open(name, 'rb') as f:
  110. answer = requests.post(url=site+"upload.php",files={'files[]': f.read()})
  111. response = json.loads(answer.text)
  112. file_name_1 = response["files"][0]["url"].replace("\\", "")
  113. urls.append(file_name_1)
  114. os.remove(name)
  115. msg = "".join(urls)
  116. return await ctx.send(msg)
  117. else:
  118. return await ctx.send("Send me stuff to upload.")
  119. @upload.error
  120. async def upload_err(self, ctx, error):
  121. return await ctx.send("File couldn't be uploaded. {}".format(error))
  122. @bot.command(aliases=["emoji"])
  123. async def emote(self, ctx, emote: discord.Emoji = None):
  124. """
  125. Uploads the emote given. Useful for downloading emotes.
  126. Usage:
  127. ;emote [emote]
  128. """
  129. if emote.animated:
  130. imgname = "emote.gif"
  131. else:
  132. imgname = "img.png"
  133. async with aiohttp.ClientSession() as session:
  134. async with session.get(emote.url) as img:
  135. with open(imgname, 'wb') as f:
  136. f.write(await img.read())
  137. await ctx.send(file=imgname)
  138. os.remove(imgname)
  139. @bot.command()
  140. @is_owner()
  141. async def echo(self, ctx, channel, *, message: str):
  142. channel = self.bot.get_channel(channel)
  143. await channel.send(message)
  144. return await ctx.send(":point_left:")
  145. def setup(bot_client):
  146. bot_client.add_cog(Util(bot_client))