async def add_safe_bytes(self, context, name, author_id, image_data: bytes, *, reason=None): """Try to add an emote from bytes. On error, return a string that should be sent to the user. If the image is static and there are not enough free static slots, convert the image to a gif instead. """ counts = collections.Counter(map(operator.attrgetter('animated'), context.guild.emojis)) # >= rather than == because there are sneaky ways to exceed the limit if counts[False] >= context.guild.emoji_limit and counts[True] >= context.guild.emoji_limit: # we raise instead of returning a string in order to abort commands that run this function in a loop raise commands.UserInputError('This server is out of emote slots.') static = utils.image.mime_type_for_image(image_data) != 'image/gif' converted = False if static and counts[False] >= context.guild.emoji_limit: image_data = await utils.image.convert_to_gif_in_subprocess(image_data) converted = True try: emote = await self.create_emote_from_bytes(context.guild, name, author_id, image_data, reason=reason) except discord.InvalidArgument: return discord.utils.escape_mentions(f'{name}: The file supplied was not a valid GIF, PNG, JPEG, or WEBP file.') except discord.HTTPException as ex: return discord.utils.escape_mentions( f'{name}: An error occurred while creating the the emote:\n' + utils.format_http_exception(ex)) s = f'Emote {emote} successfully created' return s + ' as a GIF.' if converted else s + '.'
async def rename(self, context, old, new_name): """Rename an emote on this server. old: the name of the emote to rename, or the emote itself new_name: what you'd like to rename it to """ emote = await self.parse_emote(context, old) if utils.emote.RE_NAME.search(new_name): raise commands.BadArgument( 'New name should only contain alphanumeric characters and underscores!' ) if not 32 >= len(new_name) >= 2: raise commands.BadArgument( 'New name should be between 2 and 32 characters long!') try: await emote.edit( name=new_name, reason= f'Renamed by {utils.format_user(self.bot, context.author.id)}') except discord.HTTPException as ex: return await context.send( 'An error occurred while renaming the emote:\n' + utils.format_http_exception(ex)) await context.send(fr'Emote successfully renamed to \:{new_name}:')
async def add_safe(self, guild, name, url, author_id, *, reason=None): """Try to add an emote. Returns a string that should be sent to the user.""" try: emote = await self.add_from_url(guild, name, url, author_id, reason=reason) except discord.HTTPException as ex: return ( 'An error occurred while creating the emote:\n' + utils.format_http_exception(ex)) except asyncio.TimeoutError: return 'Error: retrieving the image took too long.' except ValueError: return 'Error: Invalid URL.' else: return f'Emote {emote} successfully created.'
async def rename(self, context, old, new_name): """Rename an emote on this server. old: the name of the emote to rename, or the emote itself new_name: what you'd like to rename it to """ emote = await self.parse_emote(context, old) try: await emote.edit( name=new_name, reason=f'Renamed by {utils.format_user(self.bot, context.author.id)}') except discord.HTTPException as ex: return await context.send( 'An error occurred while renaming the emote:\n' + utils.format_http_exception(ex)) await context.send(fr'Emote successfully renamed to \:{new_name}:')
async def rename(self, context, old, new_name): """Rename an emote on this server. old: the name of the emote to rename, or the emote itself new_name: what you'd like to rename it to """ emote = await self.parse_emote(context, old) try: await emote.edit( name=new_name, reason=f'Renamed by {utils.format_user(self.bot, context.author.id)}') except discord.HTTPException as ex: return await context.send( 'An error occurred while renaming the emote:\n' + utils.format_http_exception(ex)) await context.send(f'Emote successfully renamed to \:{new_name}:')