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.

219 lines
9.5KB

  1. # -*- coding: utf-8 -*-
  2. # MIT License
  3. #
  4. # Copyright (c) 2017-2018 Roxanne Gibson
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in all
  14. # copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. import os
  24. import re
  25. import discord
  26. import emoji
  27. from discord.ext import commands
  28. import roxbot
  29. class Util(commands.Cog):
  30. """
  31. The Util cog is a cog filled with a number of utility commands to help more advanced users of Discord.
  32. """
  33. def __init__(self, bot_client):
  34. self.bot = bot_client
  35. @commands.command()
  36. async def avatar(self, ctx, *, user: discord.User = None):
  37. """
  38. Uploads a downloadable picture of an avatar.
  39. Example:
  40. # Get my avatar
  41. ;avatar
  42. # Get USER's avatar
  43. ;avatar USER#0001
  44. """
  45. if not user:
  46. user = ctx.author
  47. url = user.avatar_url_as(static_format="png")
  48. filename = re.sub(' |\?|\.|!|/|\\|:|\"|\[|\]|;|=|\||\*|,', '', user.name+str(user.id))
  49. if ".gif" in url:
  50. avaimg = '{0}.gif'.format(filename)
  51. else:
  52. avaimg = '{0}.png'.format(filename)
  53. await roxbot.http.download_file(url, avaimg)
  54. await ctx.send(file=discord.File(avaimg))
  55. os.remove(avaimg)
  56. @commands.command(aliases=["user"])
  57. async def info(self, ctx, member: discord.Member = None):
  58. """
  59. Provides information (account creation date, ID, roles [if in a guild]) on your or another persons account.
  60. Example:
  61. # Get account information for yourself
  62. ;info
  63. # Get account information for a user called USER
  64. ;info @USER
  65. """
  66. if not member:
  67. member = ctx.author
  68. if member.activity:
  69. if member.activity.type == discord.ActivityType.playing:
  70. activity = "Playing **{}**".format(member.activity.name)
  71. elif member.activity.type == discord.ActivityType.streaming:
  72. activity = "Streaming **{}**".format(member.activity.name)
  73. elif member.activity.type == discord.ActivityType.listening:
  74. activity = "Listening to **{} by {}**".format(member.activity.title, member.activity.artist)
  75. else:
  76. activity = ""
  77. else:
  78. activity = ""
  79. colour = member.colour.value
  80. avatar = member.avatar_url
  81. embed = discord.Embed(colour=colour, description=activity)
  82. embed.set_thumbnail(url=avatar)
  83. embed.set_author(name=str(member), icon_url=avatar)
  84. embed.add_field(name="ID", value=member.id)
  85. embed.add_field(name="Status", value=member.status)
  86. if member.nick:
  87. embed.add_field(name="Nickname", value=member.nick)
  88. embed.add_field(name="Account Created", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(member.created_at), inline=True)
  89. embed.add_field(name="Joined Server", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(member.joined_at), inline=True)
  90. roles = ""
  91. count = 0
  92. for role in member.roles:
  93. if role == ctx.guild.default_role:
  94. pass
  95. else:
  96. roles += role.name + ", "
  97. count += 1
  98. if not roles:
  99. roles = "None"
  100. count = 0
  101. embed.add_field(name="Roles [{}]".format(count), value=roles.strip(", "))
  102. return await ctx.send(embed=embed)
  103. @commands.guild_only()
  104. @commands.command(aliases=["server"])
  105. async def guild(self, ctx):
  106. """Gives information (creation date, owner, ID) on the guild this command is executed in."""
  107. guild = ctx.guild
  108. guild_icon_url = "https://cdn.discordapp.com/icons/{}/{}.png".format(guild.id, guild.icon)
  109. guild_splash_url = "https:/cdn.discordapp.com/splashes/{}/{}.png".format(guild.id, guild.splash)
  110. embed = discord.Embed(title=guild.name, colour=guild.me.colour.value)
  111. embed.set_thumbnail(url=guild_icon_url)
  112. embed.add_field(name="ID", value=guild.id, inline=False)
  113. embed.add_field(name="Owner", value="{} ({})".format(self.bot.get_user(guild.owner_id), guild.owner_id), inline=False)
  114. embed.add_field(name="Created at", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(guild.created_at, inline=False))
  115. embed.add_field(name="Voice Region", value=guild.region, inline=False)
  116. embed.add_field(name="AFK Timeout", value="{} Minutes".format(guild.afk_timeout/60), inline=False)
  117. if guild.afk_channel:
  118. embed.add_field(name="AFK Channel", value=guild.afk_channel, inline=False)
  119. embed.add_field(name="Verification Level", value=guild.verification_level, inline=False)
  120. embed.add_field(name="Explicit Content Filtering", value=guild.explicit_content_filter, inline=False)
  121. embed.add_field(name="Members", value=guild.member_count)
  122. number_of_bots = 0
  123. for member in guild.members:
  124. if member.bot:
  125. number_of_bots += 1
  126. human_members = guild.member_count - number_of_bots
  127. embed.add_field(name="Human Members", value=human_members, inline=False)
  128. embed.add_field(name="Roles".format(), value=str(len(guild.roles)), inline=False)
  129. embed.add_field(name="Emotes".format(), value=str(len(guild.emojis)), inline=False)
  130. 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)))
  131. if guild.features:
  132. embed.add_field(name="Extra Features", value=guild.features, inline=False)
  133. if guild.splash:
  134. embed.set_image(url=guild_splash_url)
  135. return await ctx.send(embed=embed)
  136. @commands.guild_only()
  137. @commands.command()
  138. async def role(self, ctx, *, role: discord.Role):
  139. """Gives information (creation date, colour, ID) on the role given. Can only work if the role is in the guild you execute this command in.
  140. Examples:
  141. # Get information on the role called Admin
  142. ;role Admin
  143. """
  144. embed = discord.Embed(title="Role '{}'".format(role.name), colour=role.colour.value)
  145. embed.add_field(name="ID", value=role.id, inline=False)
  146. embed.add_field(name="Created at", value="{:%a %Y/%m/%d %H:%M:%S} UTC".format(discord.utils.snowflake_time(role.id)), inline=False)
  147. embed.add_field(name="Colour", value="#{}".format(str(hex(role.colour.value)).strip("0x")), inline=False)
  148. embed.add_field(name="Hoisted", value=str(role.hoist), inline=False)
  149. embed.add_field(name="Managed", value=str(role.managed), inline=False)
  150. return await ctx.send(embed=embed)
  151. @commands.command(aliases=["emoji"])
  152. async def emote(self, ctx, emote):
  153. """
  154. Displays information (creation date, guild, ID) and an easily downloadable version of the given custom emote.
  155. Example:
  156. # Get information of the emoji ":Kappa:"
  157. ;emote :Kappa:
  158. """
  159. try: # If emote given is custom emote
  160. emote = await roxbot.converters.Emoji().convert(ctx, emote)
  161. em = discord.Embed(title=emote.name, colour=roxbot.EmbedColours.blue)
  162. em.add_field(name="ID", value=str(emote.id), inline=False)
  163. if isinstance(emote, discord.Emoji):
  164. em.add_field(name="Guild", value=str(emote.guild), inline=False)
  165. em.add_field(name="Created At", value=roxbot.datetime.format(emote.created_at), inline=False)
  166. em.set_image(url=emote.url)
  167. return await ctx.send(embed=em)
  168. except commands.errors.BadArgument: # unicode emoji
  169. title = emoji.demojize(emote)
  170. if not emoji.EMOJI_UNICODE.get(title):
  171. raise commands.BadArgument("Could not convert input to either unicode emoji or Discord custom emote.")
  172. emojis = []
  173. for char in emote:
  174. emojis.append(hex(ord(char))[2:])
  175. if len(emojis) > 1:
  176. svg_url = "https://twemoji.maxcdn.com/2/svg/{0}-{1}.svg".format(*emojis)
  177. png_url = "https://twemoji.maxcdn.com/2/72x72/{0}-{1}.png".format(*emojis)
  178. else:
  179. svg_url = "https://twemoji.maxcdn.com/2/svg/{0}.svg".format(*emojis)
  180. png_url = "https://twemoji.maxcdn.com/2/72x72/{0}.png".format(*emojis)
  181. em = discord.Embed(title=title, colour=roxbot.EmbedColours.blue)
  182. em.description = "[SVG Link]({0})\n[PNG Link]({1})".format(svg_url, png_url)
  183. em.set_image(url=png_url)
  184. return await ctx.send(embed=em)
  185. def setup(bot_client):
  186. bot_client.add_cog(Util(bot_client))