1
0
Fork 0
mirror of https://github.com/uhIgnacio/EmoteManager.git synced 2024-08-15 02:23:13 +00:00

add add command

a lot of this is from Emoji Connoisseur
This commit is contained in:
bmintz 2018-07-29 23:34:27 -05:00
parent bac48f7095
commit 3b488ab787
7 changed files with 327 additions and 11 deletions

6
utils/__init__.py Normal file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env python3
# encoding: utf-8
from .misc import *
from . import emote
from . import errors

19
utils/emote.py Normal file
View file

@ -0,0 +1,19 @@
#!/usr/bin/env python3
# encoding: utf-8
import re
"""
various utilities related to custom emotes
"""
"""Matches :foo: and ;foo; but not :foo;. Used for emotes in text."""
RE_EMOTE = re.compile(r'(:|;)(?P<name>\w{2,32})\1|(?P<newline>\n)', re.ASCII)
"""Matches only custom server emotes."""
RE_CUSTOM_EMOTE = re.compile(r'<(?P<animated>a?):(?P<name>\w{2,32}):(?P<id>\d{17,})>', re.ASCII)
def url(id, *, animated: bool = False):
"""Convert an emote ID to the image URL for that emote."""
extension = 'gif' if animated else 'png'
return f'https://cdn.discordapp.com/emojis/{id}.{extension}?v=1'

45
utils/errors.py Normal file
View file

@ -0,0 +1,45 @@
#!/usr/bin/env python3
# encoding: utf-8
from discord.ext.commands import CommandError
class EmojiManagerError(CommandError):
"""Generic error with the bot. This can be used to catch all bot errors."""
pass
class HTTPException(EmojiManagerError):
"""The server did not respond with an OK status code."""
def __init__(self, status):
super().__init__(f'URL error: server returned error code {status}')
class EmojiNotFoundError(EmojiManagerError):
"""An emoji with that name was not found"""
def __init__(self, name):
super().__init__(f'An emoji called `{name}` does not exist in this server.')
class InvalidImageError(EmojiManagerError):
"""The image is not a GIF, PNG, or JPG"""
def __init__(self):
super().__init__('The image supplied was not a GIF, PNG, or JPG.')
class NoMoreSlotsError(EmojiManagerError):
"""Raised in case all slots of a particular type (static/animated) are full"""
def __init__(self):
super().__init__('No more backend slots available.')
class PermissionDeniedError(EmojiManagerError):
"""Raised when a user tries to modify an emoji without the Manage Emojis permission"""
def __init__(self, name):
super().__init__(f"You're not authorized to modify `{name}`.")
class DiscordError(Exception):
"""Usually raised when the client cache is being baka"""
def __init__(self):
super().__init__('Discord seems to be having issues right now, please try again later.')

44
utils/misc.py Normal file
View file

@ -0,0 +1,44 @@
#!/usr/bin/env python3
# encoding: utf-8
import discord
"""various utilities for use within the bot"""
"""Emotes used to indicate success/failure. You can obtain these from the discordbots.org guild,
but I uploaded them to my test server
so that both the staging and the stable versions of the bot can use them"""
SUCCESS_EMOTES = ('<:error:416845770239508512>', '<:success:416845760810844160>')
def format_user(bot, id, *, mention=False):
"""Format a user ID for human readable display."""
user = bot.get_user(id)
if user is None:
return f'Unknown user with ID {id}'
# not mention: @null byte#8191 (140516693242937345)
# mention: <@140516693242937345> (null byte#8191)
# this allows people to still see the username and discrim
# if they don't share a server with that user
if mention:
return f'{user.mention} (@{user})'
else:
return f'@{user} ({user.id})'
def format_http_exception(exception: discord.HTTPException):
"""Formats a discord.HTTPException for relaying to the user.
Sample return value:
BAD REQUEST (status code: 400):
Invalid Form Body
In image: File cannot be larger than 256 kb.
"""
return (
f'{exception.response.reason} (status code: {exception.response.status}):'
f'\n{exception.text}')
def strip_angle_brackets(string):
"""Strip leading < and trailing > from a string.
Useful if a user sends you a url like <this> to avoid embeds, or to convert emotes to reactions."""
if string.startswith('<') and string.endswith('>'):
return string[1:-1]
return string