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.

util.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import os
  2. import json
  3. import checks
  4. import random
  5. import aiohttp
  6. import discord
  7. import requests
  8. from discord.ext.commands import bot
  9. class Util():
  10. """
  11. A cog that offers utility commands.
  12. """
  13. def __init__(self, Bot):
  14. self.bot = Bot
  15. @bot.command(pass_context=True)
  16. async def avatar(self, ctx, *,user: discord.User = None):
  17. """
  18. Returns a mentioned users avatar
  19. Example:
  20. {command_prefix}avatar @RoxBot#4170
  21. {command_prefix}avatar RoxBot
  22. """
  23. if not user:
  24. user = ctx.message.author
  25. url = user.avatar_url
  26. avaimg = 'avaimg.webp'
  27. async with aiohttp.ClientSession() as session:
  28. async with session.get(url) as img:
  29. with open(avaimg, 'wb') as f:
  30. f.write(await img.read())
  31. await self.bot.send_file(ctx.message.channel, avaimg)
  32. os.remove(avaimg)
  33. @bot.command(pass_context=True)
  34. async def info(self, ctx, member: discord.Member = None):
  35. """
  36. Gets info for a mentioned user
  37. Example:
  38. {command_prefix}info @RoxBot#4170
  39. {command_prefix}info RoxBot
  40. """
  41. if not member:
  42. member = ctx.message.author
  43. name_disc = member.name + "#" + member.discriminator
  44. if member.game:
  45. if member.game.type:
  46. game = "**" + member.game.name + "**"
  47. desc = "Streaming "
  48. else:
  49. game = "**" + member.game.name + "**"
  50. desc = "Playing "
  51. else:
  52. desc = ""
  53. game = ""
  54. colour = member.colour.value
  55. avatar = member.avatar_url
  56. embed = discord.Embed(colour=colour, description=desc+game)
  57. embed.set_thumbnail(url=avatar)
  58. embed.set_author(name=name_disc, icon_url=avatar)
  59. embed.add_field(name="ID", value=member.id)
  60. embed.add_field(name="Status", value=member.status)
  61. if member.nick:
  62. embed.add_field(name="Nickname", value=member.nick)
  63. embed.add_field(name="Account Created", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(member.created_at), inline=True)
  64. embed.add_field(name="Joined Server", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(member.joined_at), inline=True)
  65. roles = ""
  66. count = 0
  67. for role in member.roles:
  68. if role == ctx.message.server.default_role:
  69. pass
  70. else:
  71. roles += role.name + ", "
  72. count += 1
  73. if not roles:
  74. roles = "None"
  75. count = 0
  76. embed.add_field(name="Roles [{}]".format(count), value=roles.strip(", "))
  77. return await self.bot.say(embed=embed)
  78. @bot.command(pass_context=True)
  79. async def upload(self, ctx):
  80. """
  81. Uploads selected file to the host, thanks to the fact that
  82. every pomf.se based site has pretty much the same architecture.
  83. """
  84. sites = [
  85. "https://comfy.moe/",
  86. "https://safe.moe/api/",
  87. "http://up.che.moe/",
  88. "https://mixtape.moe/",
  89. "https://pomf.cat/",
  90. "https://sugoi.vidyagam.es/",
  91. "https://doko.moe/",
  92. "https://pomfe.co/",
  93. "https://pomf.space/",
  94. "https://vidga.me/",
  95. "https://pomf.pyonpyon.moe/"
  96. ] # List of pomf clone sites and upload limits
  97. await self.bot.send_typing(ctx.message.channel)
  98. if ctx.message.attachments:
  99. # Site choice, shouldn't need an upload size check since max upload for discord atm is 50MB
  100. site = random.choice(sites)
  101. urls = []
  102. for attachment in ctx.message.attachments:
  103. name = attachment['url'].split("/")[-1]
  104. # Download File
  105. with aiohttp.ClientSession() as session:
  106. async with session.get(attachment['url']) as img:
  107. with open(name, 'wb') as f:
  108. f.write(await img.read())
  109. # Upload file
  110. try:
  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. except Exception as e:
  117. print(e)
  118. print(name + ' couldn\'t be uploaded to ' + site)
  119. os.remove(name)
  120. msg = "".join(urls)
  121. return await self.bot.say(msg)
  122. else:
  123. return await self.bot.say("Send me stuff to upload.")
  124. @bot.command(pass_context=True, aliases=["emoji"]) # This command will only work with normal emoji once I can put in something to get the svgs for twiemoji and convert em
  125. async def emote(self, ctx, emote):
  126. """
  127. Uploads the emote given. Useful for downloading emotes. ONLY WORKS WITH CUSTOM EMOJI
  128. """
  129. emoteid = emote.split(":")[-1].strip("<>")
  130. if not emoteid.isdigit():
  131. return await self.bot.say("This command only works with custom emotes.")
  132. url = "https://discordapp.com/api/emojis/{}.png".format(emoteid)
  133. imgname = 'img.png'
  134. async with aiohttp.ClientSession() as session:
  135. async with session.get(url) as img:
  136. with open(imgname, 'wb') as f:
  137. f.write(await img.read())
  138. await self.bot.send_file(ctx.message.channel, imgname)
  139. os.remove(imgname)
  140. #return await self.bot.say(url)
  141. @bot.command(pass_context=True, enabled=False, hidden=True)
  142. @checks.is_bot_owner()
  143. async def emoterob(self, ctx, emote, name=None):
  144. """
  145. Gets a emoji and adds it to the custom emoji list. ONLY WORKS WITH CUSTOM EMOJI
  146. """
  147. emoteid = emote.split(":")[-1].strip("<>")
  148. if not emoteid.isdigit():
  149. return await self.bot.say("This command only works with custom emotes.")
  150. url = "https://discordapp.com/api/emojis/{}.png".format(emoteid)
  151. imgname = 'img.png'
  152. async with aiohttp.ClientSession() as session:
  153. async with session.get(url) as img:
  154. with open(imgname, 'wb') as f:
  155. f.write(await img.read())
  156. with open(imgname, "rb") as f:
  157. return await self.bot.create_custom_emoji(ctx.message.server, name=name, image=f)
  158. @bot.command(pass_context=True, hidden=True)
  159. @checks.is_bot_owner()
  160. async def echo(self, ctx, channel, *, message: str):
  161. if ctx.message.channel_mentions: # If Mentioned
  162. for channel in ctx.message.channel_mentions:
  163. await self.bot.send_message(channel, content=message)
  164. return await self.bot.say(":point_left:")
  165. elif channel.isdigit(): # If ID is given
  166. channel = ctx.message.server.get_channel(channel)
  167. await self.bot.send_message(channel, content=message)
  168. return await self.bot.say(":point_left:")
  169. else:
  170. return await self.bot.say("You did something wrong smh")
  171. @bot.command(pass_context=True, enabled=False, hidden=True)
  172. async def say(self, ctx, *, echo):
  173. return await self.bot.say(echo)
  174. def setup(Bot):
  175. Bot.add_cog(Util(Bot))