Skip to content

Commit

Permalink
Merge pull request #196 from nekokatt/bugfix/guild-message-create-use…
Browse files Browse the repository at this point in the history
…r-is-none

Fixes edge case where messages in guilds had a None 'author' attribute
  • Loading branch information
Nekokatt authored Sep 16, 2020
2 parents 8a2627b + ccc27fe commit adb7ed4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
56 changes: 52 additions & 4 deletions hikari/events/message_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,41 @@ def author(self) -> users.User:
The user that sent the message.
"""

@property
def is_bot(self) -> bool:
"""Return `builtins.True` if the message is from a bot.
Returns
-------
builtins.bool
`builtins.True` if from a bot, or `builtins.False` otherwise.
"""
return self.message.author.is_bot

@property
def is_webhook(self) -> bool:
"""Return `builtins.True` if the message was created by a webhook.
Returns
-------
builtins.bool
`builtins.True` if from a webhook, or `builtins.False` otherwise.
"""
return self.message.webhook_id is not None

@property
def is_human(self) -> bool:
"""Return `builtins.True` if the message was created by a human.
Returns
-------
builtins.bool
`builtins.True` if from a human user, or `builtins.False` otherwise.
"""
# Not second-guessing some weird edge case will occur in the future with this,
# so I am being safe rather than sorry.
return not self.message.author.is_bot and self.message.webhook_id is None


@base_events.requires_intents(intents.Intents.GUILD_MESSAGES, intents.Intents.PRIVATE_MESSAGES)
@attr.s(kw_only=True, slots=True, weakref_slot=False)
Expand Down Expand Up @@ -367,16 +402,29 @@ def guild_id(self) -> snowflakes.Snowflake:
return guild_id

@property
def author(self) -> guilds.Member:
def author(self) -> users.User:
"""Member that sent the message.
!!! note
For webhooks, this will be a `hikari.users.User`.
Any code relying on this being a `hikari.guilds.Member` directly
should use an `isinstance` assertion to determine if member info
is available or not.
Returns
-------
hikari.guilds.Member
The member that sent the message. This is a specialised
hikari.users.User
The member that sent the message, if known. This is a specialised
implementation of `hikari.users.User`.
If the author was a webhook, then a `hikari.users.User` will be
returned instead, as webhooks do not have member objects.
"""
return typing.cast(guilds.Member, self.message.member)
member = self.message.member
if member is not None:
return member
return self.message.author


@base_events.requires_intents(intents.Intents.PRIVATE_MESSAGES)
Expand Down
6 changes: 2 additions & 4 deletions hikari/utilities/ux.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
"""User-experience extensions and utilities."""
from __future__ import annotations

import time

from hikari.utilities import net

__all__: typing.List[str] = ["init_logging", "print_banner", "supports_color", "HikariVersion", "check_for_updates"]

import contextlib
Expand All @@ -37,11 +33,13 @@
import re
import string
import sys
import time
import typing

import colorlog # type: ignore[import]

from hikari import _about as about
from hikari.utilities import net

if typing.TYPE_CHECKING:
from hikari import config
Expand Down

0 comments on commit adb7ed4

Please sign in to comment.