Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 KB | None | 0 0
  1. import re
  2. import aiohttp
  3. from redbot.core import commands
  4. import discord
  5.  
  6.  
  7.  
  8. class EmojiConverter(commands.Converter):
  9.     async def convert(self, ctx, emoji):
  10.         is_link_ = self.is_link(emoji)
  11.         if not is_link_:
  12.             base_link = "https://cdn.discordapp.com/emojis"
  13.             emoji_id = self.get_emoji_id(emoji)
  14.             if not emoji_id:
  15.                 return None
  16.             link = f"{base_link}/{emoji_id.replace('>', '')}.png"
  17.         else:
  18.             link = emoji
  19.         byte_emoji = await self.get_emoji(link)
  20.         return byte_emoji
  21.  
  22.     @staticmethod
  23.     def get_emoji_id(emoji):
  24.         emoji_parts = emoji.split(":")
  25.         emoji_id = emoji_parts[1]
  26.         if type(emoji_id) != int:
  27.             emoji_id = emoji_parts[2]
  28.         return emoji_id
  29.  
  30.     @staticmethod
  31.     def is_link(emoji):
  32.         regex = re.compile(
  33.             r"^(?:http|ftp)s?://"  # http:// or https://
  34.             r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|"  # domain...
  35.             r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"  # ...or ip
  36.             r"(?::\d+)?"  # optional port
  37.             r"(?:/?|[/?]\S+)$",
  38.             re.IGNORECASE,
  39.         )
  40.         return re.match(regex, emoji)
  41.  
  42.     @staticmethod
  43.     async def get_emoji(link):
  44.         async with aiohttp.ClientSession() as session:
  45.             async with session.get(link) as result:
  46.                 return await result.read()
  47.  
  48. BaseCog = getattr(commands, "Cog", object)
  49.  
  50.  
  51. class EmojiSteal(BaseCog):
  52.     def __init__(self, bot):
  53.         self.bot = bot
  54.      
  55.     @commands.command(name="emotesteal", aliases=["esteal", "emoteupload", "eupload"])
  56.     @commands.bot_has_permissions(manage_emojis=True)
  57.     @commands.guild_only()
  58.     @commands.has_permissions(manage_emojis=True)
  59.     async def create_emote(self, ctx, emoji: EmojiConverter=None, name=None):
  60.         """Uploads a custom emoji to your server."""
  61.         if emoji is None:
  62.             return await ctx.send("Make sure that you specified a valid emoji. This can either be a link or any other"
  63.                                   " custom emoji.\n**Note:** Make sure that the emoji is static because otherwise"
  64.                                   " I will not be able to upload it.")
  65.         if name is None:
  66.             return await ctx.send("Make sure that you specified a valid name. This name will be used for the the emoji."
  67.                                   )
  68.         try:
  69.             emoji = await ctx.guild.create_custom_emoji(name=name, image=emoji,
  70.                                                         reason=f"Custom emoji uploaded by {ctx.author.id}")
  71.             return await ctx.send(f"Successfully uploaded the emoji! {emoji}")
  72.         except Exception as e:
  73.             print(e)
  74.             return await ctx.send("Something didn't go quite right. Please make sure that you haven't already uploaded"
  75.                                   " 50 custom emotes!")
  76.                                  
  77.     async def __error(self, ctx, error):
  78.         # error handler
  79.         if isinstance(error, commands.BadArgument):
  80.             print(error)
  81.             await ctx.send(f"Please make sure you supplied the right arguments. For more information please use the "
  82.                            f"command {ctx.prefix}help {ctx.command}")
  83.             return
  84.         elif isinstance(error, commands.BotMissingPermissions):
  85.             return
  86.         elif isinstance(error, commands.MissingPermissions):
  87.             return
  88.         elif isinstance(error, commands.CommandOnCooldown):
  89.             await ctx.send(error)
  90.             return
  91.         elif isinstance(error, ValueError):
  92.             await ctx.send(f"Please make sure you supplied the right arguments. For more information please use the "
  93.                            f"command {ctx.prefix}help {ctx.command}")
  94.             return
  95.         elif isinstance(error, commands.CommandError):
  96.             self.error_handler(error)
  97.             await ctx.send("Something didn't go quite right.")
  98.            
  99.     @staticmethod
  100.     def error_handler(error):
  101.         print("An error occurred:")
  102.         traceback.print_exception(type(error), error, error.__traceback__)
  103.         return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement