From c5a989eeb2d9f9e6326b6de56531ce9ccbe47c20 Mon Sep 17 00:00:00 2001 From: Khazhismel Date: Sun, 31 Jul 2016 22:12:06 -0700 Subject: [PATCH] Add custom emoji support. --- discord/__init__.py | 1 + discord/client.py | 6 +++ discord/emoji.py | 104 ++++++++++++++++++++++++++++++++++++++++++++ discord/server.py | 6 ++- discord/state.py | 7 +++ 5 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 discord/emoji.py diff --git a/discord/__init__.py b/discord/__init__.py index 335322e78..62166ddfc 100644 --- a/discord/__init__.py +++ b/discord/__init__.py @@ -20,6 +20,7 @@ __version__ = '0.11.0' from .client import Client, AppInfo, ChannelPermissions from .user import User from .game import Game +from .emoji import Emoji from .channel import Channel, PrivateChannel from .server import Server from .member import Member, VoiceState diff --git a/discord/client.py b/discord/client.py index b8edff241..53cb1802e 100644 --- a/discord/client.py +++ b/discord/client.py @@ -523,6 +523,12 @@ class Client: """Returns a :class:`Server` with the given ID. If not found, returns None.""" return self.connection._get_server(id) + def get_all_emojis(self): + """Returns a generator with every :class:`Emoji` the client can see.""" + for server in self.servers: + for emoji in server.emojis: + yield emoji + def get_all_channels(self): """A generator that retrieves every :class:`Channel` the client can 'access'. diff --git a/discord/emoji.py b/discord/emoji.py new file mode 100644 index 000000000..996582caa --- /dev/null +++ b/discord/emoji.py @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- + +""" +The MIT License (MIT) + +Copyright (c) 2015-2016 Rapptz + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +""" + +from . import utils +from .mixins import Hashable + +class Emoji(Hashable): + """Represents a custom emoji. + + Depending on the way this object was created, some of the attributes can + have a value of ``None``. + + Supported Operations: + + +-----------+-----------------------------------------+ + | Operation | Description | + +===========+=========================================+ + | x == y | Checks if two emoji are the same. | + +-----------+-----------------------------------------+ + | x != y | Checks if two emoji are not the same. | + +-----------+-----------------------------------------+ + | hash(x) | Return the emoji's hash. | + +-----------+-----------------------------------------+ + | iter(x) | Returns an iterator of (field, value) | + | | pairs. This allows this class to be | + | | used as an iterable in list/dict/etc. | + | | constructions. | + +-----------+-----------------------------------------+ + | str(x) | Returns the emoji rendered for discord. | + +-----------+-----------------------------------------+ + + Attributes + ----------- + name : str + The name of the emoji. + id : str + The emoji's ID. + require_colons : bool + If colons are required to use this emoji in the client (:PJSalt: vs PJSalt). + managed : bool + If this emoji is managed by a Twitch integration. + created_at : `datetime.datetime` + A datetime object denoting the time the invite was created. + server : :class:`Server` + The server the emoji belongs to. + roles : List[:class:`Role`] + A list of :class:`Role` that is allowed to use this emoji. If roles is empty, + the emoji is unrestricted. + """ + __slots__ = ["require_colons", "managed", "id", "name", "roles", 'server'] + + def __init__(self, **kwargs): + self.server = kwargs.pop('server') + self._from_data(kwargs) + + def _from_data(self, emoji): + self.require_colons = emoji.get('require_colons') + self.managed = emoji.get('managed') + self.id = emoji.get('id') + self.name = emoji.get('name') + self.roles = emoji.get('roles', []) + if self.roles: + roles = set(self.roles) + self.roles = [role for role in self.server.roles if role.id in roles] + + def _iterator(self): + for attr in self.__slots__: + value = getattr(self, attr, None) + if value is not None: + yield (attr, value) + + def __iter__(self): + return self._iterator() + + def __str__(self): + return "<:{0.name}:{0.id}>".format(self) + + @property + def created_at(self): + """Returns the emoji's creation time in UTC.""" + return utils.snowflake_time(self.id) diff --git a/discord/server.py b/discord/server.py index aa3c230ab..035b52a10 100644 --- a/discord/server.py +++ b/discord/server.py @@ -27,6 +27,7 @@ DEALINGS IN THE SOFTWARE. from . import utils from .role import Role from .member import Member +from .emoji import Emoji from .game import Game from .channel import Channel from .enums import ServerRegion, Status @@ -58,6 +59,8 @@ class Server(Hashable): This is essentially used to get the member version of yourself. roles A list of :class:`Role` that the server has available. + emojis + A list of :class:`Emoji` that the server owns. region : :class:`ServerRegion` The region the server belongs on. There is a chance that the region will be a ``str`` if the value is not recognised by the enumerator. @@ -94,7 +97,7 @@ class Server(Hashable): __slots__ = ['afk_timeout', 'afk_channel', '_members', '_channels', 'icon', 'name', 'id', 'owner', 'unavailable', 'name', 'region', '_default_role', '_default_channel', 'roles', '_member_count', - 'large', 'owner_id', 'mfa_level' ] + 'large', 'owner_id', 'mfa_level', 'emojis'] def __init__(self, **kwargs): self._channels = {} @@ -185,6 +188,7 @@ class Server(Hashable): self.id = guild['id'] self.roles = [Role(server=self, **r) for r in guild.get('roles', [])] self.mfa_level = guild.get('mfa_level') + self.emojis = [Emoji(server=self, **r) for r in guild.get('emojis', [])] for mdata in guild.get('members', []): roles = [self.default_role] diff --git a/discord/state.py b/discord/state.py index a0ce2b890..747c3bd94 100644 --- a/discord/state.py +++ b/discord/state.py @@ -27,6 +27,7 @@ DEALINGS IN THE SOFTWARE. from .server import Server from .user import User from .game import Game +from .emoji import Emoji from .message import Message from .channel import Channel, PrivateChannel from .member import Member @@ -406,6 +407,12 @@ class ConnectionState: member.roles.sort(key=lambda r: int(r.id)) self.dispatch('member_update', old_member, member) + def parse_guild_emojis_update(self, data): + server = self._get_server(data.get('guild_id')) + before_emojis = server.emojis + server.emojis = [Emoji(server=server, **e) for e in data.get('emojis', [])] + self.dispatch('server_emojis_update', before_emojis, server.emojis) + def _get_create_server(self, data): if data.get('unavailable') == False: # GUILD_CREATE with unavailable in the response