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.

218 lines
7.6KB

  1. import os
  2. import json
  3. import random
  4. import aiohttp
  5. import discord
  6. import requests
  7. from discord.ext import commands
  8. from discord.ext.commands import bot
  9. class Util():
  10. """
  11. A cog that offers utility commands.
  12. """
  13. def __init__(self, bot_client):
  14. self.bot = bot_client
  15. @bot.command()
  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.author
  25. url = user.avatar_url
  26. if url.split(".")[-1] == "gif":
  27. avaimg = 'avaimg.gif'
  28. else:
  29. avaimg = 'avaimg.webp'
  30. async with aiohttp.ClientSession() as session:
  31. async with session.get(url) as img:
  32. with open(avaimg, 'wb') as f:
  33. f.write(await img.read())
  34. await ctx.send(file=discord.File(avaimg))
  35. os.remove(avaimg)
  36. @bot.command()
  37. async def info(self, ctx, member: discord.Member = None):
  38. """
  39. Gets info for a mentioned user
  40. Example:
  41. {command_prefix}info @RoxBot#4170
  42. {command_prefix}info RoxBot
  43. """
  44. if not member:
  45. member = ctx.author
  46. if member.activity:
  47. if member.activity.type == discord.ActivityType.playing:
  48. activity = "Playing **{}**".format(member.activity.name)
  49. elif member.activity.type == discord.ActivityType.streaming:
  50. activity = "Streaming **{}**".format(member.activity.name)
  51. elif member.activity.type == discord.ActivityType.listening:
  52. activity = "Listening to **{} by {}**".format(member.activity.title, member.activity.artist)
  53. else:
  54. activity = ""
  55. else:
  56. activity = ""
  57. colour = member.colour.value
  58. avatar = member.avatar_url
  59. embed = discord.Embed(colour=colour, description=activity)
  60. embed.set_thumbnail(url=avatar)
  61. embed.set_author(name=str(member), icon_url=avatar)
  62. embed.add_field(name="ID", value=member.id)
  63. embed.add_field(name="Status", value=member.status)
  64. if member.nick:
  65. embed.add_field(name="Nickname", value=member.nick)
  66. embed.add_field(name="Account Created", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(member.created_at), inline=True)
  67. embed.add_field(name="Joined Server", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(member.joined_at), inline=True)
  68. roles = ""
  69. count = 0
  70. for role in member.roles:
  71. if role == ctx.guild.default_role:
  72. pass
  73. else:
  74. roles += role.name + ", "
  75. count += 1
  76. if not roles:
  77. roles = "None"
  78. count = 0
  79. embed.add_field(name="Roles [{}]".format(count), value=roles.strip(", "))
  80. return await ctx.send(embed=embed)
  81. @commands.guild_only()
  82. @bot.command(aliases=["server"])
  83. async def guild(self, ctx):
  84. """Returns info on the current guild(server)."""
  85. guild = ctx.guild
  86. guild_icon_url = "https://cdn.discordapp.com/icons/{}/{}.png".format(guild.id, guild.icon)
  87. guild_splash_url = "https:/cdn.discordapp.com/splashes/{}/{}.png".format(guild.id, guild.splash)
  88. embed = discord.Embed(title=guild.name, colour=guild.me.colour.value)
  89. embed.set_thumbnail(url=guild_icon_url)
  90. embed.add_field(name="ID", value=guild.id, inline=False)
  91. embed.add_field(name="Created at", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(guild.created_at, inline=False))
  92. embed.add_field(name="Voice Region", value=guild.region, inline=False)
  93. embed.add_field(name="AFK Timeout", value="{} Minutes".format(guild.afk_timeout/60), inline=False)
  94. if guild.afk_channel:
  95. embed.add_field(name="AFK Channel", value=guild.afk_channel, inline=False)
  96. embed.add_field(name="Owner", value="{} ({})".format(self.bot.get_user(guild.owner_id), guild.owner_id), inline=False)
  97. embed.add_field(name="Verification Level", value=guild.verification_level, inline=False)
  98. embed.add_field(name="Explicit Content Filtering", value=guild.explicit_content_filter, inline=False)
  99. embed.add_field(name="Roles [{}]".format(len(guild.roles)), value="For all roles, use `{}guild roles`.", inline=False)
  100. embed.add_field(name="Emotes [{}]".format(len(guild.emojis)), value="For all emotes, use `{}guild emotes`.".format(self.bot.command_prefix), inline=False)
  101. embed.add_field(name="Channels [{}]".format(len(guild.channels)), value="{} Channel Categories\n{} Text Channels\n{} Voice Channels".format(len(guild.categories), len(guild.text_channels), len(guild.voice_channels)))
  102. if guild.features:
  103. embed.add_field(name="Extra Features", value=guild.features)
  104. if guild.splash:
  105. embed.set_image(url=guild_splash_url)
  106. return await ctx.send(embed=embed)
  107. @commands.guild_only()
  108. @bot.command()
  109. async def role(self, ctx, *, role: discord.Role):
  110. """Displays the info on a role"""
  111. embed = discord.Embed(title="Role '{}'".format(role.name), colour=role.colour.value)
  112. embed.add_field(name="ID", value=role.id, inline=False)
  113. embed.add_field(name="Created at", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(discord.utils.snowflake_time(role.id)), inline=False)
  114. embed.add_field(name="Colour", value="#{}".format(str(hex(role.colour.value)).strip("0x")), inline=False)
  115. embed.add_field(name="Hoisted", value=str(role.hoist), inline=False)
  116. embed.add_field(name="Managed", value=str(role.managed), inline=False)
  117. return await ctx.send(embed=embed)
  118. @bot.command()
  119. async def upload(self, ctx):
  120. """
  121. Uploads selected file to the host, thanks to the fact that
  122. every pomf.se based site has pretty much the same architecture.
  123. """
  124. sites = [
  125. "https://comfy.moe/",
  126. "https://safe.moe/api/",
  127. "http://up.che.moe/",
  128. "https://mixtape.moe/",
  129. "https://pomf.cat/",
  130. "https://sugoi.vidyagam.es/",
  131. "https://doko.moe/",
  132. "https://pomfe.co/",
  133. "https://pomf.space/",
  134. "https://vidga.me/",
  135. "https://pomf.pyonpyon.moe/"
  136. ] # List of pomf clone sites and upload limits
  137. if ctx.message.attachments:
  138. # Site choice, shouldn't need an upload size check since max upload for discord atm is 50MB
  139. site = random.choice(sites)
  140. urls = []
  141. for attachment in ctx.message.attachments:
  142. name = attachment['url'].split("/")[-1]
  143. # Download File
  144. with aiohttp.ClientSession() as session:
  145. async with session.get(attachment['url']) as img:
  146. with open(name, 'wb') as f:
  147. f.write(await img.read())
  148. # Upload file
  149. with open(name, 'rb') as f:
  150. answer = requests.post(url=site+"upload.php",files={'files[]': f.read()})
  151. response = json.loads(answer.text)
  152. file_name_1 = response["files"][0]["url"].replace("\\", "")
  153. urls.append(file_name_1)
  154. os.remove(name)
  155. msg = "".join(urls)
  156. return await ctx.send(msg)
  157. else:
  158. return await ctx.send("Send me stuff to upload.")
  159. @upload.error
  160. async def upload_err(self, ctx, error):
  161. return await ctx.send("File couldn't be uploaded. {}".format(error))
  162. @bot.command(aliases=["emoji"])
  163. async def emote(self, ctx, emote):
  164. """
  165. Uploads the emote given. Useful for downloading emotes.
  166. Usage:
  167. ;emote [emote]
  168. """
  169. emote = emote.strip("<>").split(":")
  170. if emote[0] == "a":
  171. imgname = "emote.gif"
  172. emoji_id = emote[2]
  173. else:
  174. imgname = "emote.png"
  175. emoji_id = emote[2]
  176. url = "https://cdn.discordapp.com/emojis/{}".format(emoji_id)
  177. async with aiohttp.ClientSession() as session:
  178. async with session.get(url) as img:
  179. with open(imgname, 'wb') as f:
  180. f.write(await img.read())
  181. await ctx.send(file=discord.File(imgname))
  182. os.remove(imgname)
  183. @bot.command(hidden=True)
  184. async def inviteme(self, ctx):
  185. """Returns an invite link to invite the bot to your server."""
  186. link = discord.utils.oauth_url(self.bot.user.id, discord.Permissions.all_channel())
  187. return await ctx.send("WARNING: This is only for Roxie to use atm. Here is a link to invite me to your server! <{}>".format(link))
  188. @bot.command()
  189. @commands.is_owner()
  190. async def echo(self, ctx, channel: discord.TextChannel, *, message: str):
  191. await channel.send(message)
  192. return await ctx.send(":point_left:")
  193. def setup(bot_client):
  194. bot_client.add_cog(Util(bot_client))