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.

170 lines
4.8KB

  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=discord.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:
  46. if member.activity.type == discord.ActivityType.playing:
  47. activity = "Playing **{}**".format(member.activity.name)
  48. elif member.activity.type == discord.ActivityType.streaming:
  49. activity = "Streaming **{}**".format(member.activity.name)
  50. elif member.activity.type == discord.ActivityType.listening:
  51. activity = "Listening to **{} by {}**".format(member.activity.title, member.activity.artist)
  52. else:
  53. activity = ""
  54. else:
  55. activity = ""
  56. colour = member.colour.value
  57. avatar = member.avatar_url
  58. embed = discord.Embed(colour=colour, description=activity)
  59. embed.set_thumbnail(url=avatar)
  60. embed.set_author(name=str(member), icon_url=avatar)
  61. embed.add_field(name="ID", value=member.id)
  62. embed.add_field(name="Status", value=member.status)
  63. if member.nick:
  64. embed.add_field(name="Nickname", value=member.nick)
  65. embed.add_field(name="Account Created", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(member.created_at), inline=True)
  66. embed.add_field(name="Joined Server", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(member.joined_at), inline=True)
  67. roles = ""
  68. count = 0
  69. for role in member.roles:
  70. if role == ctx.guild.default_role:
  71. pass
  72. else:
  73. roles += role.name + ", "
  74. count += 1
  75. if not roles:
  76. roles = "None"
  77. count = 0
  78. embed.add_field(name="Roles [{}]".format(count), value=roles.strip(", "))
  79. return await ctx.send(embed=embed)
  80. @bot.command()
  81. async def upload(self, ctx):
  82. """
  83. Uploads selected file to the host, thanks to the fact that
  84. every pomf.se based site has pretty much the same architecture.
  85. """
  86. sites = [
  87. "https://comfy.moe/",
  88. "https://safe.moe/api/",
  89. "http://up.che.moe/",
  90. "https://mixtape.moe/",
  91. "https://pomf.cat/",
  92. "https://sugoi.vidyagam.es/",
  93. "https://doko.moe/",
  94. "https://pomfe.co/",
  95. "https://pomf.space/",
  96. "https://vidga.me/",
  97. "https://pomf.pyonpyon.moe/"
  98. ] # List of pomf clone sites and upload limits
  99. if ctx.message.attachments:
  100. # Site choice, shouldn't need an upload size check since max upload for discord atm is 50MB
  101. site = random.choice(sites)
  102. urls = []
  103. for attachment in ctx.message.attachments:
  104. name = attachment['url'].split("/")[-1]
  105. # Download File
  106. with aiohttp.ClientSession() as session:
  107. async with session.get(attachment['url']) as img:
  108. with open(name, 'wb') as f:
  109. f.write(await img.read())
  110. # Upload file
  111. with open(name, 'rb') as f:
  112. answer = requests.post(url=site+"upload.php",files={'files[]': f.read()})
  113. response = json.loads(answer.text)
  114. file_name_1 = response["files"][0]["url"].replace("\\", "")
  115. urls.append(file_name_1)
  116. os.remove(name)
  117. msg = "".join(urls)
  118. return await ctx.send(msg)
  119. else:
  120. return await ctx.send("Send me stuff to upload.")
  121. @upload.error
  122. async def upload_err(self, ctx, error):
  123. return await ctx.send("File couldn't be uploaded. {}".format(error))
  124. @bot.command(aliases=["emoji"])
  125. async def emote(self, ctx, emote):
  126. """
  127. Uploads the emote given. Useful for downloading emotes.
  128. Usage:
  129. ;emote [emote]
  130. """
  131. emote = emote.strip("<>").split(":")
  132. if emote[0] == "a":
  133. imgname = "emote.gif"
  134. emoji_id = emote[2]
  135. else:
  136. imgname = "emote.png"
  137. emoji_id = emote[1]
  138. url = "https://cdn.discordapp.com/emojis/{}".format(emoji_id)
  139. async with aiohttp.ClientSession() as session:
  140. async with session.get(url) as img:
  141. with open(imgname, 'wb') as f:
  142. f.write(await img.read())
  143. await ctx.send(file=discord.File(imgname))
  144. os.remove(imgname)
  145. @bot.command()
  146. @is_owner()
  147. async def echo(self, ctx, channel, *, message: str):
  148. channel = self.bot.get_channel(channel)
  149. await channel.send(message)
  150. return await ctx.send(":point_left:")
  151. def setup(bot_client):
  152. bot_client.add_cog(Util(bot_client))