185 Commits

Author SHA1 Message Date
Rapptz
d9960dd421 animation field in reaction objects are not consistently available 2019-11-18 19:12:01 -05:00
Rapptz
a69738b635 Fix regression with unresolved channels due to reordering.
Channels are meant to fallback to Object if the message is out of
order. Somewhere along the commit line this got removed despite the
issue still existing.
2019-10-17 20:19:53 -04:00
Rapptz
7e2da1c93b Publicly expose a type property for all channels.
Fix #2185
2019-06-09 00:23:22 -04:00
Rapptz
991140eebe Replace Enum with an internal one for significant speed improvements.
This has been a massive pain point for me personally due to the poor
design of the Enum class leading to the common use cases used in the
library being significantly slow. Since this Enum is not public facing
in terms of *creation*, I can only implement the APIs that are used
when *accessing* them.

This Enum is a drop-in replacement to the pre-existing enum.Enum class
except it comes with significant speed-ups. Since this is a lot to go
over, I will let the numbers speak for themselves:

In [4]: %timeit enums.try_enum(enums.Status, 'offline')
263 ns ± 34.3 ns per loop (7 runs, 1000000 loops each)
In [5]: %timeit NeoStatus.try_value('offline')
134 ns ± 0.859 ns per loop (7 runs, 10000000 loops each)

In [6]: %timeit enums.Status.offline
116 ns ± 0.378 ns per loop (7 runs, 10000000 loops each)
In [7]: %timeit NeoStatus.offline
31.6 ns ± 0.327 ns per loop (7 runs, 10000000 loops each)

In [8]: %timeit enums.Status.offline.value
382 ns ± 15.2 ns per loop (7 runs, 1000000 loops each)
In [9]: %timeit NeoStatus.offline.value
65.5 ns ± 0.953 ns per loop (7 runs, 10000000 loops each)

In [10]: %timeit str(enums.Status.offline)
630 ns ± 14.8 ns per loop (7 runs, 1000000 loops each)
In [11]: %timeit str(NeoStatus.offline)
253 ns ± 3.53 ns per loop (7 runs, 1000000 loops each)

In [12]: %timeit enums.Status('offline')
697 ns ± 8.42 ns per loop (7 runs, 1000000 loops each)
In [13]: %timeit NeoStatus('offline')
182 ns ± 1.83 ns per loop (7 runs, 10000000 loops each)
2019-06-09 00:06:34 -04:00
Rapptz
f84219c2b2 Improve performance of parsing MESSAGE_UPDATE and MESSAGE_CREATE events
Embed edits are no longer special cased in the dispatch code, which
could lead to on_message_edit being called more often than it used to
be called. I am not sure on the general impact on that being removed.

Fixes #2195
2019-06-07 22:18:01 -04:00
Rapptz
00a0856cc4 Use a dict instead of getattr for parsing events.
Probably not a significant difference but might as well use it here.
The basic idea is to cache the getattr calls instead of repeatedly
doing it (since they're around 105ns on my machine). The dictionary
lookup is about 41ns on my machine.

The next step in speeding up library code some more should be in
the parser bodies themselves but that's a problem to tackle another
day.
2019-05-29 01:22:53 -04:00
Vexs
82a39eb148 Add cached_message to on_raw_message_edit event
Also add documentation for this behavior
2019-05-09 05:53:56 -04:00
Rapptz
f9e95a35f9 Fix USER_UPDATE changes not triggering for member instances.
Also add a ClientUser.locale attribute.
2019-04-28 06:36:12 -04:00
Rapptz
f8999b63ae Fix long-standing issue with user updates not dispatching properly.
This fix is long coming. For a long time due to the addition of a
global user cache, the on_member_update event would only have the
updated user in the very first dispatch due to a quirk in the reference
only being updated once.

In order to fix this issue two things had to change:

1. There had to be a new event, `on_user_update` to complement
   the equivalent member event.
2. Unnecessary copies of User had to be removed to compensate for the
   performance hit from the diffing.

While doing these two fixes I also re-evaluated some more unnecessary
copies done during the PRESENCE_UPDATE to add member case while
fetch_offline_members=False is set or due to chunking issues. The
number of copies was brought down from 2 to 1, discounting the original
Member creation. Unsure on the benefits of this one, however.

N.B: this doesn't change the pre-existing behaviour of on_member_update
2019-04-08 07:45:04 -04:00
Rapptz
1a9726087a GUILD_MEMBER_UPDATE no longer does actual user updates.
This had been delegated to PRESENCE_UPDATE a long time ago,
unfortunately.
2019-04-08 06:34:28 -04:00
Vexs
9591b00dcf Redesign bulk delete events
Change `parse_message_delete_bulk` to not dispatch individual
`message_delete` events, and instead `bulk_message_delete`. Also make
`raw_bulk_message` delete and `raw_message_delete` have message
objects attached.

Fixes #1807
2019-04-06 19:18:06 -04:00
NCPlayz
be227ebcf0 Redesign asset retrieval in the library.
Most assets now return a new class named `Asset`. This allows for the
assets to be consistently saved via a `save` method instead of special
casing for `Attachment`.

`AppInfo` is no longer a namedtuple it is a fully documented dataclass,
as well as having the state attached to it.

Fixes #1997
2019-04-06 19:12:50 -04:00
Rapptz
b41a29a889 Get cached message in reverse order for improved performance.
Most embed-only edits happen for the newest message.
2019-03-23 00:57:50 -04:00
Rapptz
35c0476dbb Add TextChannel.last_message_id and TextChannel.last_message 2019-03-20 22:30:23 -04:00
Rapptz
73aca4d4d3 Expose Embed.from_data as Embed.from_dict
This is a breaking change.
2019-03-13 06:02:56 -04:00
CraftSpider
8e97a0a248 Log unknown channel warning in WEBHOOK_UPDATE 2019-02-18 03:47:57 -05:00
CraftSpider
e920fe9a56 Add guild_integrations_update event 2019-02-18 03:47:56 -05:00
Dante Dam
9656a21ebe Bumped copyright years to 2019. 2019-01-28 22:22:50 -05:00
Hornwitser
efb4ff850e [lint] Fix import order
Reorder imports to be consistenly grouped by standard library, third
party library, and local modules in that order thoughout the library.
2018-11-24 22:17:58 -05:00
Hornwitser
633192b3cd [lint] Replace equality comparisons to singletons
Restrict the values accepted by comparisons with booleans to be actual
booleans.

Minor breaking of undocumented behaviour in permissions; the value to
set bits to must be booleans (as indicated by the type error thrown).
2018-11-24 22:17:57 -05:00
Hornwitser
fa46b07db1 [lint] Rename exception variables to exc
Use the more explicit (and common) exc instead of e as the variable
holding the exception in except handlers.
2018-11-24 22:17:57 -05:00
Rapptz
2721689254 Optimise tight loops in DiscordGateway.received_message
* type(x) is y is faster than isinstance(x, y)
* Re-arrange if-statements for common statements
* Drop handler getattr for most events that don't  use it
2018-09-24 23:22:49 -04:00
Rapptz
095f0ec2fc Optimise some member and user related routines.
* Skip user update if possible
* Drop copy.copy in favour of manual copy

Credit to Hornwitser for these findings.
2018-09-24 22:50:05 -04:00
Rapptz
3d03dbc451 Change internal role storage in Guild to a dict instead of a list.
This adds the following APIs:

* Guild.get_role

This removes the following APIs:

* Guild.role_hierarchy

To compensate for the removed APIs, Guild.roles is now a sorted list
based on hierarchy. The first element will always be the @everyone
role.

This speeds up access at the cost of some memory, theoretically.
2018-09-24 21:08:48 -04:00
Jeremy Zhang
d092c6635b Fix webhook_update from code review 2018-09-15 18:28:57 -04:00
Jeremy Zhang
17f9916b03 Simple parser for WEBHOOKS_UPDATE 2018-09-15 18:28:57 -04:00
Bryan Forbes
b6824c073e Ensure large guilds are properly appended to the ready state guild list 2018-08-28 23:50:55 -04:00
Hornwitser
c8b49d37be [lint] Fix incorrect and inconsistent whitespace
Adjust whitespace to be consistent with the rest of the library.
2018-08-22 21:43:53 -04:00
Hornwitser
119c5a0618 [lint] Remove unused variables
Left over from various refactoring and rewrites.
2018-08-22 21:43:51 -04:00
Hornwitser
d58fc0ccee [lint] Remove unused imports
Left over from various refactoring and rewrites.
2018-08-22 21:43:50 -04:00
Rapptz
351bb1927a Fix crashing with wonky TYPING_START events.
Fixes #1439
2018-07-20 04:15:58 -04:00
khazhyk
9d301f7cae Store guild, unavailable tuple in ready as well
This should fix userbots.

Fixes: 74380cd ("don't drop guild_available/join before ready")
2018-06-30 23:26:33 -07:00
khazhyk
74380cd72a don't drop guild_available/join before ready
large guilds that recieved GUILD_CREATE events prior to READY would
never dispatch guild_available/join. This is inconsistent with
smaller guilds and makes these events unreliable. Dispatch the
"delayed" guilds after they've been chunked, prior to ready.
2018-06-29 21:48:31 -04:00
Rapptz
c67d95327e Remove dead package references. 2018-06-10 18:16:40 -04:00
Rapptz
f25091efe1 Drop support for Python 3.4 and make minimum version 3.5.2. 2018-06-10 18:10:00 -04:00
Rapptz
6b75179c1f Remove emojis from cache if they're removed from a guild. 2018-05-19 17:07:45 -04:00
Rapptz
56650ae7c2 Change raw events to use slotted data models instead of parameters.
This allows for internal changes in Discord to not cause a breaking
change every time something changes.

Also implements #1214 which adds guild_id to every event where
applicable.

This is a breaking change.
2018-04-14 02:23:38 -04:00
Rapptz
3cb6ac152e Userbots do not have a DM cache cap limit. 2018-04-02 00:42:14 -04:00
Rapptz
f8f8f418f3 Split Game object to separate Activity subtypes for Rich Presences.
This is a massive breaking change.

* All references to "game" have been renamed to "activity"
* Activity objects contain a majority of the rich presence information
* Game and Streaming are subtypes for memory optimisation purposes for
  the more common cases.
* Introduce a more specialised read-only type, Spotify, for the
  official Spotify integration to make it easier to use.
2018-03-05 11:15:49 -05:00
Rapptz
1c215cfbbe Don't cache webhook users.
Fixes #1101
2018-02-24 17:37:37 -05:00
Gorialis
04d9dd9c0d Change PartialReactionEmoji to PartialEmoji, add a PartialEmojiConverter 2018-01-06 17:32:25 -05:00
Rapptz
53b4890435 Add category support.
This adds:

* CategoryChannel, which represents a category
* Guild.by_category() which traverses the channels grouping by category
* Guild.categories to get a list of categories
* abc.GuildChannel.category to get the category a channel belongs to
* sync_permissions keyword argument to abc.GuildChannel.edit to sync
  permissions with a pre-existing or new category
* category keyword argument to abc.GuildChannel.edit to move a channel
  to a category
2017-09-13 09:44:36 -04:00
Rapptz
1367877d36 Fix crashing for now when a category is created.
A full implementation will come later.
2017-09-08 21:30:25 -04:00
Rapptz
de65f7309b Add heartbeat_timeout to the Client options.
This setting configures how long before a timeout event is emitted
internally and disconnects the websocket. Since some users were
experiencing issues with the gateway not responding, this should help
mitigate the issue for those with poor PCs.
2017-08-08 21:12:04 -04:00
Rapptz
b88cc8156c Accidentally left a print statement. 2017-07-05 00:48:27 -04:00
Rapptz
3bd0c2120c Implement a LRU cache for private channels.
Another fix related to the discord issue[1].

[1]: https://github.com/hammerandchisel/discord-api-docs/issues/184
2017-07-04 20:02:17 -04:00
Rapptz
169f3a8322 Don't unnecessarily re-create private channels.
New API change[1] will make it so CHANNEL_CREATE will keep getting
sent for private channels, so might as well avoid the overhead of
constantly creating the channel if we can avoid it.

[1]: https://github.com/hammerandchisel/discord-api-docs/issues/184
2017-07-04 20:02:11 -04:00
Rapptz
c1dad43f9d Downgrade PartialReactionEmoji to str in non-raw reaction events. 2017-06-09 22:01:31 -04:00
Rapptz
b06899e7d4 Defer logging formatting until the logger is actually called.
This would cause unnecessary format calls even if you didn't have
logging enabled.
2017-06-09 18:53:24 -04:00
Rapptz
d239cc2666 Implement "partial" message events.
These are events that get triggered regardless of the state of the
message cache. Useful for getting data from before the bot was booted.
2017-06-09 18:36:59 -04:00