Browse Source

Make __repr__ slightly more detailed and add a few missing ones.

This includes raw events (which didn't have any) and a few other
types that were missing them. Upon review some more useful fields were
added to the repr output which would be more useful during debugging.
pull/2188/head
Rapptz 6 years ago
parent
commit
2cd6d771ec
  1. 12
      discord/activity.py
  2. 6
      discord/audit_logs.py
  3. 26
      discord/channel.py
  4. 2
      discord/emoji.py
  5. 7
      discord/guild.py
  6. 4
      discord/invite.py
  7. 5
      discord/message.py
  8. 3
      discord/object.py
  9. 15
      discord/raw_models.py

12
discord/activity.py

@ -152,6 +152,18 @@ class Activity(_ActivityTag):
self.session_id = kwargs.pop('session_id', None) self.session_id = kwargs.pop('session_id', None)
self.type = try_enum(ActivityType, kwargs.pop('type', -1)) self.type = try_enum(ActivityType, kwargs.pop('type', -1))
def __repr__(self):
attrs = (
'type',
'name',
'url',
'details',
'application_id',
'session_id',
)
mapped = ' '.join('%s=%r' % (attr, getattr(self, attr)) for attr in attrs)
return '<Activity %s>' % mapped
def to_dict(self): def to_dict(self):
ret = {} ret = {}
for attr in self.__slots__: for attr in self.__slots__:

6
discord/audit_logs.py

@ -93,7 +93,8 @@ class AuditLogDiff:
return iter(self.__dict__.items()) return iter(self.__dict__.items())
def __repr__(self): def __repr__(self):
return '<AuditLogDiff attrs={0!r}>'.format(tuple(self.__dict__)) values = ' '.join('%s=%r' % item for item in self.__dict__.items())
return '<AuditLogDiff %s>' % values
class AuditLogChanges: class AuditLogChanges:
TRANSFORMERS = { TRANSFORMERS = {
@ -164,6 +165,9 @@ class AuditLogChanges:
self.after.color = self.after.colour self.after.color = self.after.colour
self.before.color = self.before.colour self.before.color = self.before.colour
def __repr__(self):
return '<AuditLogChanges before=%r after=%r>' % (self.before, self.after)
def _handle_role(self, first, second, entry, elem): def _handle_role(self, first, second, entry, elem):
if not hasattr(first, 'roles'): if not hasattr(first, 'roles'):
setattr(first, 'roles', []) setattr(first, 'roles', [])

26
discord/channel.py

@ -107,7 +107,15 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
self._update(guild, data) self._update(guild, data)
def __repr__(self): def __repr__(self):
return '<TextChannel id={0.id} name={0.name!r} position={0.position}>'.format(self) attrs = [
('id', self.id),
('name', self.name),
('position', self.position),
('nsfw', self.nsfw),
('news', self.is_news()),
('category_id', self.category_id)
]
return '<%s %s>' % (self.__class__.__name__, ' '.join('%s=%r' % t for t in attrs))
def _update(self, guild, data): def _update(self, guild, data):
self.guild = guild self.guild = guild
@ -491,7 +499,15 @@ class VoiceChannel(discord.abc.Connectable, discord.abc.GuildChannel, Hashable):
self._update(guild, data) self._update(guild, data)
def __repr__(self): def __repr__(self):
return '<VoiceChannel id={0.id} name={0.name!r} position={0.position}>'.format(self) attrs = [
('id', self.id),
('name', self.name),
('position', self.position),
('bitrate', self.bitrate),
('user_limit', self.user_limit),
('category_id', self.category_id)
]
return '<%s %s>' % (self.__class__.__name__, ' '.join('%s=%r' % t for t in attrs))
def _get_voice_client_key(self): def _get_voice_client_key(self):
return self.guild.id, 'guild_id' return self.guild.id, 'guild_id'
@ -629,7 +645,7 @@ class CategoryChannel(discord.abc.GuildChannel, Hashable):
self._update(guild, data) self._update(guild, data)
def __repr__(self): def __repr__(self):
return '<CategoryChannel id={0.id} name={0.name!r} position={0.position}>'.format(self) return '<CategoryChannel id={0.id} name={0.name!r} position={0.position} nsfw={0.nsfw}>'.format(self)
def _update(self, guild, data): def _update(self, guild, data):
self.guild = guild self.guild = guild
@ -788,7 +804,7 @@ class StoreChannel(discord.abc.GuildChannel, Hashable):
self._update(guild, data) self._update(guild, data)
def __repr__(self): def __repr__(self):
return '<StoreChannel id={0.id} name={0.name!r} position={0.position}>'.format(self) return '<StoreChannel id={0.id} name={0.name!r} position={0.position} nsfw={0.nsfw}>'.format(self)
def _update(self, guild, data): def _update(self, guild, data):
self.guild = guild self.guild = guild
@ -1030,7 +1046,7 @@ class GroupChannel(discord.abc.Messageable, Hashable):
@property @property
def icon_url(self): def icon_url(self):
""":class:`Asset`: Returns the channel's icon asset.""" """:class:`Asset`: Returns the channel's icon asset if available."""
return Asset._from_icon(self._state, self, 'channel') return Asset._from_icon(self._state, self, 'channel')
@property @property

2
discord/emoji.py

@ -206,7 +206,7 @@ class Emoji:
return "<:{0.name}:{0.id}>".format(self) return "<:{0.name}:{0.id}>".format(self)
def __repr__(self): def __repr__(self):
return '<Emoji id={0.id} name={0.name!r}>'.format(self) return '<Emoji id={0.id} name={0.name!r} animated={0.animated} managed={0.managed}>'.format(self)
def __eq__(self, other): def __eq__(self, other):
return isinstance(other, (PartialEmoji, Emoji)) and self.id == other.id return isinstance(other, (PartialEmoji, Emoji)) and self.id == other.id

7
discord/guild.py

@ -160,7 +160,12 @@ class Guild(Hashable):
return self.name return self.name
def __repr__(self): def __repr__(self):
return '<Guild id={0.id} name={0.name!r} chunked={0.chunked}>'.format(self) attrs = (
'id', 'name', 'shard_id', 'chunked'
)
resolved = ['%s=%r' % (attr, getattr(self, attr)) for attr in attrs]
resolved.append('member_count=%r' % getattr(self, '_member_count', None))
return '<Guild %s>' % ' '.join(resolved)
def _update_voice_state(self, data, channel_id): def _update_voice_state(self, data, channel_id):
user_id = int(data['user_id']) user_id = int(data['user_id'])

4
discord/invite.py

@ -294,7 +294,9 @@ class Invite(Hashable):
return self.url return self.url
def __repr__(self): def __repr__(self):
return '<Invite code={0.code!r}>'.format(self) return '<Invite code={0.code!r} guild={0.guild!r} ' \
'online={0.approximate_presence_count} ' \
'members={0.approximate_member_count}>'.format(self)
def __hash__(self): def __hash__(self):
return hash(self.code) return hash(self.code)

5
discord/message.py

@ -78,6 +78,9 @@ class Attachment:
""":class:`bool`: Whether this attachment contains a spoiler.""" """:class:`bool`: Whether this attachment contains a spoiler."""
return self.filename.startswith('SPOILER_') return self.filename.startswith('SPOILER_')
def __repr__(self):
return '<Attachment id={0.id} filename={0.filename!r} url={0.url!r}>'.format(self)
async def save(self, fp, *, seek_begin=True, use_cached=False): async def save(self, fp, *, seek_begin=True, use_cached=False):
"""|coro| """|coro|
@ -263,7 +266,7 @@ class Message:
self._update(channel, data) self._update(channel, data)
def __repr__(self): def __repr__(self):
return '<Message id={0.id} pinned={0.pinned} author={0.author!r}>'.format(self) return '<Message id={0.id} channel={0.channel!r} type={0.type!r} author={0.author!r}>'.format(self)
def _try_patch(self, data, key, transform=None): def _try_patch(self, data, key, transform=None):
try: try:

3
discord/object.py

@ -64,6 +64,9 @@ class Object(Hashable):
def __init__(self, id): def __init__(self, id):
self.id = id self.id = id
def __repr__(self):
return '<Object id=%r>' % self.id
@property @property
def created_at(self): def created_at(self):
"""Returns the snowflake's creation time in UTC.""" """Returns the snowflake's creation time in UTC."""

15
discord/raw_models.py

@ -24,7 +24,12 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
""" """
class RawMessageDeleteEvent: class _RawReprMixin:
def __repr__(self):
value = ' '.join('%s=%r' % (attr, getattr(self, attr)) for attr in self.__slots__)
return '<%s %s>' % (self.__class__.__name__, value)
class RawMessageDeleteEvent(_RawReprMixin):
"""Represents the event payload for a :func:`on_raw_message_delete` event. """Represents the event payload for a :func:`on_raw_message_delete` event.
Attributes Attributes
@ -50,7 +55,7 @@ class RawMessageDeleteEvent:
except KeyError: except KeyError:
self.guild_id = None self.guild_id = None
class RawBulkMessageDeleteEvent: class RawBulkMessageDeleteEvent(_RawReprMixin):
"""Represents the event payload for a :func:`on_raw_bulk_message_delete` event. """Represents the event payload for a :func:`on_raw_bulk_message_delete` event.
Attributes Attributes
@ -77,7 +82,7 @@ class RawBulkMessageDeleteEvent:
except KeyError: except KeyError:
self.guild_id = None self.guild_id = None
class RawMessageUpdateEvent: class RawMessageUpdateEvent(_RawReprMixin):
"""Represents the payload for a :func:`on_raw_message_edit` event. """Represents the payload for a :func:`on_raw_message_edit` event.
Attributes Attributes
@ -98,7 +103,7 @@ class RawMessageUpdateEvent:
self.data = data self.data = data
self.cached_message = None self.cached_message = None
class RawReactionActionEvent: class RawReactionActionEvent(_RawReprMixin):
"""Represents the payload for a :func:`on_raw_reaction_add` or """Represents the payload for a :func:`on_raw_reaction_add` or
:func:`on_raw_reaction_remove` event. :func:`on_raw_reaction_remove` event.
@ -129,7 +134,7 @@ class RawReactionActionEvent:
except KeyError: except KeyError:
self.guild_id = None self.guild_id = None
class RawReactionClearEvent: class RawReactionClearEvent(_RawReprMixin):
"""Represents the payload for a :func:`on_raw_reaction_clear` event. """Represents the payload for a :func:`on_raw_reaction_clear` event.
Attributes Attributes

Loading…
Cancel
Save