Skip to content

Commit fde02e7

Browse files
author
Nekokatt
authored
Merge pull request #165 from nekokatt/task/162-author-message-events
Added message author attributes to events and fixed typing.
2 parents 5a12196 + e82094a commit fde02e7

2 files changed

Lines changed: 184 additions & 45 deletions

File tree

hikari/events/message_events.py

Lines changed: 151 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,17 @@
4646

4747
import attr
4848

49+
from hikari import channels
50+
from hikari import guilds
4951
from hikari import intents
5052
from hikari import snowflakes
53+
from hikari import undefined
5154
from hikari import users
5255
from hikari.events import base_events
5356
from hikari.events import shard_events
5457
from hikari.utilities import attr_extensions
5558

5659
if typing.TYPE_CHECKING:
57-
# Do NOT remove the users import here. It **is** required, even if PyCharm
58-
# tries to assure you otherwise.
59-
from hikari import channels
60-
from hikari import guilds
6160
from hikari import messages
6261
from hikari import traits
6362
from hikari.api import shard as gateway_shard
@@ -131,9 +130,24 @@ def guild_id(self) -> snowflakes.Snowflake:
131130
"""
132131

133132
@property
134-
def channel(self) -> typing.Optional[channels.GuildTextChannel]:
135-
# <<inherited docstring from MessagesEvent>>.
136-
return typing.cast("channels.GuildTextChannel", self.app.cache.get_guild_channel(self.channel_id))
133+
def channel(self) -> typing.Union[None, channels.GuildTextChannel, channels.GuildNewsChannel]:
134+
"""Channel that the message was sent in, if known.
135+
136+
Returns
137+
-------
138+
typing.Union[builtins.None, hikari.channels.GuildTextChannel, hikari.channels.GuildNewsChannel]
139+
The channel the message was sent in, or `builtins.None` if not
140+
known/cached.
141+
142+
This otherwise will always be a `hikari.channels.GuildTextChannel`
143+
if it is a normal message, or `hikari.channels.GuildNewsChannel` if
144+
sent in an announcement channel.
145+
"""
146+
channel = self.app.cache.get_guild_channel(self.channel_id)
147+
assert channel is None or isinstance(
148+
channel, (channels.GuildTextChannel, channels.GuildNewsChannel)
149+
), f"expected cached channel to be None or a GuildTextChannel/GuildNewsChannel, not {channel}"
150+
return channel
137151

138152
@property
139153
def guild(self) -> typing.Optional[guilds.GatewayGuild]:
@@ -157,6 +171,11 @@ def guild(self) -> typing.Optional[guilds.GatewayGuild]:
157171
class MessageCreateEvent(MessageEvent, abc.ABC):
158172
"""Event base for any message creation event."""
159173

174+
@property
175+
def message_id(self) -> snowflakes.Snowflake:
176+
# <<inherited docstring from MessageEvent>>.
177+
return self.message.id
178+
160179
@property
161180
@abc.abstractmethod
162181
def message(self) -> messages.Message:
@@ -168,11 +187,6 @@ def message(self) -> messages.Message:
168187
The message object that was sent with this event.
169188
"""
170189

171-
@property
172-
def message_id(self) -> snowflakes.Snowflake:
173-
# <<inherited docstring from MessageEvent>>.
174-
return self.message.id
175-
176190
@property
177191
def channel_id(self) -> snowflakes.Snowflake:
178192
# <<inherited docstring from MessageEvent>>.
@@ -189,6 +203,17 @@ def author_id(self) -> snowflakes.Snowflake:
189203
"""
190204
return self.message.author.id
191205

206+
@property
207+
@abc.abstractmethod
208+
def author(self) -> users.User:
209+
"""User that sent the message.
210+
211+
Returns
212+
-------
213+
hikari.users.User
214+
The user that sent the message.
215+
"""
216+
192217

193218
@base_events.requires_intents(intents.Intents.GUILD_MESSAGES, intents.Intents.PRIVATE_MESSAGES)
194219
@attr.s(kw_only=True, slots=True, weakref_slot=False)
@@ -219,6 +244,23 @@ def message_id(self) -> snowflakes.Snowflake:
219244
# <<inherited docstring from MessageEvent>>.
220245
return self.message.id
221246

247+
@property
248+
def channel_id(self) -> snowflakes.Snowflake:
249+
# <<inherited docstring from MessageEvent>>.
250+
return self.message.channel_id
251+
252+
@property
253+
@abc.abstractmethod
254+
def channel(self) -> typing.Optional[channels.TextChannel]:
255+
"""Channel that the message was sent in, if known.
256+
257+
Returns
258+
-------
259+
typing.Optional[hikari.channels.TextChannel]
260+
The text channel that the message was sent in, if known and cached,
261+
otherwise, `builtins.None`.
262+
"""
263+
222264
@property
223265
def author_id(self) -> snowflakes.Snowflake:
224266
"""ID of the author that triggered this event.
@@ -230,13 +272,23 @@ def author_id(self) -> snowflakes.Snowflake:
230272
"""
231273
# Looks like `author` is always present in this event variant.
232274
author = self.message.author
233-
assert isinstance(author, users.PartialUser)
275+
assert isinstance(author, users.User), "message.author was expected to be present"
234276
return author.id
235277

236278
@property
237-
def channel_id(self) -> snowflakes.Snowflake:
238-
# <<inherited docstring from MessageEvent>>.
239-
return self.message.channel_id
279+
@abc.abstractmethod
280+
def author(self) -> typing.Optional[users.User]:
281+
"""User that sent the message.
282+
283+
Returns
284+
-------
285+
typing.Optional[hikari.users.User]
286+
The user that sent the message, if known and cached, otherwise
287+
`builtins.None`.
288+
"""
289+
author = self.message.author
290+
assert isinstance(author, users.User), "message.author was expected to be present"
291+
return author
240292

241293

242294
@base_events.requires_intents(intents.Intents.GUILD_MESSAGES, intents.Intents.PRIVATE_MESSAGES)
@@ -278,6 +330,18 @@ def channel_id(self) -> snowflakes.Snowflake:
278330
# <<inherited docstring from MessagesEvent>>.
279331
return self.message.channel_id
280332

333+
@property
334+
@abc.abstractmethod
335+
def channel(self) -> typing.Optional[channels.TextChannel]:
336+
"""Channel that the message was sent in, if known.
337+
338+
Returns
339+
-------
340+
typing.Optional[hikari.channels.TextChannel]
341+
The text channel that the message was sent in, if known and cached,
342+
otherwise, `builtins.None`.
343+
"""
344+
281345

282346
@base_events.requires_intents(intents.Intents.GUILD_MESSAGES)
283347
@attr_extensions.with_copy
@@ -292,14 +356,27 @@ class GuildMessageCreateEvent(GuildMessageEvent, MessageCreateEvent):
292356
# <<inherited docstring from ShardEvent>>.
293357

294358
message: messages.Message = attr.ib()
295-
296359
# <<inherited docstring from MessageCreateEvent>>.
297360

298361
@property
299362
def guild_id(self) -> snowflakes.Snowflake:
300363
# <<inherited docstring from GuildMessageEvent>>.
301364
# Always present in this event.
302-
return typing.cast("snowflakes.Snowflake", self.message.guild_id)
365+
guild_id = self.message.guild_id
366+
assert isinstance(guild_id, snowflakes.Snowflake)
367+
return guild_id
368+
369+
@property
370+
def author(self) -> guilds.Member:
371+
"""Member that sent the message.
372+
373+
Returns
374+
-------
375+
hikari.guilds.Member
376+
The member that sent the message. This is a specialised
377+
implementation of `hikari.users.User`.
378+
"""
379+
return typing.cast(guilds.Member, self.message.member)
303380

304381

305382
@base_events.requires_intents(intents.Intents.PRIVATE_MESSAGES)
@@ -315,14 +392,25 @@ class PrivateMessageCreateEvent(PrivateMessageEvent, MessageCreateEvent):
315392
# <<inherited docstring from ShardEvent>>.
316393

317394
message: messages.Message = attr.ib()
318-
319395
# <<inherited docstring from MessageCreateEvent>>.
320396

321397
@property
322398
def channel(self) -> typing.Optional[channels.DMChannel]:
323-
# <<inherited docstring from MessagesEvent>>.
399+
"""Channel that the message was sent in, if known.
400+
401+
Returns
402+
-------
403+
typing.Optional[hikari.channels.DMChannel]
404+
The DM channel that the message was sent in, if known and cached,
405+
otherwise, `builtins.None`.
406+
"""
324407
return self.app.cache.get_dm(self.author_id)
325408

409+
@property
410+
def author(self) -> users.User:
411+
# <<inherited from MessageCreateEvent>>.
412+
return self.message.author
413+
326414

327415
@base_events.requires_intents(intents.Intents.GUILD_MESSAGES)
328416
@attr_extensions.with_copy
@@ -337,7 +425,6 @@ class GuildMessageUpdateEvent(GuildMessageEvent, MessageUpdateEvent):
337425
# <<inherited docstring from ShardEvent>>.
338426

339427
message: messages.PartialMessage = attr.ib()
340-
341428
# <<inherited docstring from MessageUpdateEvent>>.
342429

343430
@property
@@ -348,6 +435,21 @@ def guild_id(self) -> snowflakes.Snowflake:
348435
assert isinstance(guild_id, snowflakes.Snowflake)
349436
return guild_id
350437

438+
@property
439+
def author(self) -> typing.Union[guilds.Member, users.User]:
440+
# <<inherited from GuildMessageUpdateEvent>>.
441+
member = self.message.member
442+
if member is not undefined.UNDEFINED and member is not None:
443+
return member
444+
member = self.app.cache.get_member(self.guild_id, self.author_id)
445+
if member is not None:
446+
return member
447+
448+
# This should always be present.
449+
author = self.message.author
450+
assert isinstance(author, users.User), "expected author to be present"
451+
return author
452+
351453

352454
@base_events.requires_intents(intents.Intents.PRIVATE_MESSAGES)
353455
@attr_extensions.with_copy
@@ -362,14 +464,20 @@ class PrivateMessageUpdateEvent(PrivateMessageEvent, MessageUpdateEvent):
362464
# <<inherited docstring from ShardEvent>>.
363465

364466
message: messages.PartialMessage = attr.ib()
365-
366467
# <<inherited docstring from MessageUpdateEvent>>.
367468

368469
@property
369470
def channel(self) -> typing.Optional[channels.DMChannel]:
370471
# <<inherited docstring from MessagesEvent>>.
371472
return self.app.cache.get_dm(self.author_id)
372473

474+
@property
475+
def author(self) -> typing.Optional[users.User]:
476+
# Always present on an update event.
477+
author = self.message.author
478+
assert isinstance(author, users.User), "expected author to be present on PartialMessage"
479+
return author
480+
373481

374482
@base_events.requires_intents(intents.Intents.GUILD_MESSAGES)
375483
@attr_extensions.with_copy
@@ -384,14 +492,15 @@ class GuildMessageDeleteEvent(GuildMessageEvent, MessageDeleteEvent):
384492
# <<inherited docstring from ShardEvent>>.
385493

386494
message: messages.PartialMessage = attr.ib()
387-
388495
# <<inherited docstring from MessageDeleteEvent>>.
389496

390497
@property
391498
def guild_id(self) -> snowflakes.Snowflake:
392499
# <<inherited docstring from GuildMessageEvent>>.
393500
# Always present in this event.
394-
return typing.cast("snowflakes.Snowflake", self.message.guild_id)
501+
guild_id = self.message.guild_id
502+
assert isinstance(guild_id, snowflakes.Snowflake), f"expected guild_id to be snowflake, not {guild_id}"
503+
return guild_id
395504

396505

397506
@attr_extensions.with_copy
@@ -407,7 +516,6 @@ class PrivateMessageDeleteEvent(PrivateMessageEvent, MessageDeleteEvent):
407516
# <<inherited docstring from ShardEvent>>.
408517

409518
message: messages.PartialMessage = attr.ib()
410-
411519
# <<inherited docstring from MessageDeleteEvent>>.
412520

413521
@property
@@ -473,9 +581,24 @@ class GuildMessageBulkDeleteEvent(MessageBulkDeleteEvent):
473581
"""
474582

475583
@property
476-
def channel(self) -> typing.Optional[channels.GuildTextChannel]:
477-
# <<inherited docstring from MessagesEvent>>.
478-
return typing.cast("channels.GuildTextChannel", self.app.cache.get_guild_channel(self.channel_id))
584+
def channel(self) -> typing.Union[None, channels.GuildTextChannel, channels.GuildNewsChannel]:
585+
"""Get the cached channel the messages were sent in, if known.
586+
587+
Returns
588+
-------
589+
typing.Union[builtins.None, hikari.channels.GuildTextChannel, hikari.channels.GuildNewsChannel]
590+
The channel the messages were sent in, or `builtins.None` if not
591+
known/cached.
592+
593+
This otherwise will always be a `hikari.channels.GuildTextChannel`
594+
if it is a normal message, or `hikari.channels.GuildNewsChannel` if
595+
sent in an announcement channel.
596+
"""
597+
channel = self.app.cache.get_guild_channel(self.channel_id)
598+
assert channel is None or isinstance(
599+
channel, (channels.GuildTextChannel, channels.GuildNewsChannel)
600+
), f"expected cached channel to be None or a GuildTextChannel/GuildNewsChannel, not {channel}"
601+
return channel
479602

480603
@property
481604
def guild(self) -> typing.Optional[guilds.GatewayGuild]:

0 commit comments

Comments
 (0)