72 Commits

Author SHA1 Message Date
Rapptz
9d10f9e091 Remove caching from AutoShardedClient.shards 2020-09-24 02:17:55 -04:00
Rapptz
50f4c31d31 Terminate shard processing queue when a clean close is encountered.
Fix #5180
2020-08-01 19:26:49 -04:00
Rapptz
b77af19939 Fix AttributeError from internal queue being double underscore. 2020-07-25 10:32:32 -04:00
Rapptz
7ed26db3b3 Add an exposed way to extract shard-specific information.
Closes #2654
2020-07-25 09:59:41 -04:00
Rapptz
a42bebe581 Propagate exceptions when an unhandled error happens 2020-07-25 09:59:40 -04:00
Rapptz
e2f42597a5 Handle Connection Reset by Peer connection errors.
This should work both on Windows and on Linux.

Apparently these types of blips are considered normal for Discord. So
rather than letting the reconnect logic handler expect these to be
catastrophic, it should handle it specially so it doesn't waste an
IDENTIFY for what ultimately should just be a small networking blip.

This also makes it less noisy for the end-user as these complaints
happen from time to time.
2020-07-25 09:59:40 -04:00
Rapptz
8070d39a23 Add shard related connection and resume events.
These include:
* on_shard_resumed
* on_shard_connect
* on_shard_disconnect
2020-07-25 09:59:39 -04:00
Rapptz
80cc5b5edb Handle connection errors during reidentify flow. 2020-07-25 09:59:39 -04:00
Rapptz
f658fcf164 Make every shard maintain its own reconnect loop
Previously if a disconnect happened the client would get in a bad state
and certain shards would be double sending due to unhandled exceptions
raising back to Client.connect and causing all shards to be reconnected
again.

This new code overrides Client.connect to have more finer control and
allow each individual shard to maintain its own reconnect loop and then
serially request reconnection to ensure that IDENTIFYs are not
overlapping.
2020-07-25 09:59:39 -04:00
Rapptz
394b514cc9 Add before_identify_hook to have finer control over IDENTIFY syncing 2020-07-25 09:59:39 -04:00
Rapptz
9c7ae6b9dc Fix READY not firing for multi-processed AutoShardedClient clusters 2020-07-25 09:59:38 -04:00
Rapptz
988505a97f Use a proper type for the event queue 2020-07-25 09:59:38 -04:00
Rapptz
b8154e365f Rewrite gateway to use aiohttp instead of websockets 2020-07-25 09:59:38 -04:00
Rapptz
45cb231161 Fix AttributeError on reconnection 2020-07-25 09:59:38 -04:00
Rapptz
09ecb16680 Rewrite of AutoShardedClient to prevent overlapping identify
This is experimental and I'm unsure if it actually works
2020-07-25 09:59:37 -04:00
Rapptz
13a3f760e6 Fix timeout issues with fetching members via query_members
This uses the nonce field to properly disambiguate queries. There's
also some redesigning going on behind the scenes and minor clean-up.
Originally I planned on working on this more to account for the more
widespread chunking changes planned for gateway v7 but I realized that
this would indiscriminately slow down everyone else who isn't planning
on working with intents for now.

I will work on the larger chunking changes in the future, should time
allow for it.
2020-05-10 19:35:45 -04:00
Rapptz
6c764465c0 Fix regression with Member.activities not clearing 2020-04-04 03:17:31 -04:00
Rapptz
403651a144 Change default close code to 4000 instead of 1000.
This prevents our sessions from being invalidated by Discord which
caused a lot of failures when resuming. This caused an unnecessary
amount of IDENTIFYs to happen depending on the circumstances.

Closes #2631

See also discord/discord-api-docs#1472
2020-04-04 01:42:08 -04:00
jack1142
7b3335a0b0 Don't add None to Member.activities tuple 2020-04-04 01:12:46 -04:00
Rapptz
5c7df3d943 Bump waiting time of GUILD_CREATE stream by number of shards waited.
2 seconds might be too short and cause it to finish waiting while other
shards are still IDENTIFYing.
2020-01-28 23:56:46 -05:00
Rapptz
6071607176 Bump copyright year to 2020
Closes #2510
2020-01-19 20:03:00 -05:00
Rapptz
5f7a97ff96 Document BaseActivity 2020-01-14 22:09:24 -05:00
Rapptz
42a084028c Expose _ActivityTag as BaseActivity to easily refer to. 2020-01-14 20:56:00 -05:00
Rapptz
4ef0fb0d95 Fix more deprecation warnings 2019-11-20 03:04:04 -05:00
Rapptz
a6f61dcbde Fix all deprecation warnings for 3.8 2019-11-20 02:30:19 -05:00
Reece Dunham
34b93c757c docs: Fix minor typo 2019-06-12 00:47:42 -04:00
NCPlayz
3c9bcc2851 Improve documentation 2019-06-07 19:27:46 -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
Rapptz
186d9a7f9c Use a regular boolean instead of asyncio.Event for close status. 2019-04-25 01:57:32 -04:00
Rapptz
61ee88b0fc Fix oversight where on_disconnect did not get called on WS termination. 2019-04-08 07:45:05 -04:00
NCPlayz
fb02191b80 Organise documentation 2019-03-19 08:24:42 -04:00
Dante Dam
9656a21ebe Bumped copyright years to 2019. 2019-01-28 22:22:50 -05:00
SnowyLuma
e89e7dfe93 Add support for multiple activities 2018-11-24 22:36:52 -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
51d626eabe [lint] Remove redundant paranthesis
Remove redundant parenthisis around await expressions.  Left over from
f25091ef.
2018-11-24 22:17:58 -05:00
Hornwitser
a71b3b5fa0 [lint] Limit unneccessarily broad except clauses
Add exception qualifier(s) to bare except clauses swallowing exceptions.
2018-11-24 22:17:57 -05:00
Gorialis
e29e3438ec
Don't try to close shards if there are none yet. 2018-09-26 02:19:42 +09: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
BeatButton
a4d1599ce9 Change docstrings to raw-strings 2018-09-14 22:55:27 -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
Rapptz
5ae9ea26b1 Disable compression for websockets.
Increase of RAM and CPU doesn't give me much benefit I feel.
2018-06-10 18:37:51 -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
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
Tobotimus
3112e1c17e Add intersphinx 2018-01-06 17:23:59 -05:00
verixx
542924d5df Fixed a zero division error when accessing latencies
When accessing the latencies property on an AutoShardedClient when none of shards are ready, we get a ZeroDivisionError. An example of this can be seen here.
```py
class StatsBot(commands.AutoShardedBot):
    def __init__(self):
        super().__init__(command_prefix=None)
        self._add_commands()

    def _add_commands(self):
        '''Adds commands automatically'''
        for name, attr in inspect.getmembers(self):
            if isinstance(attr, commands.Command):
                self.add_command(attr)
```

When iterating through this custom client's it accesses the latencies property when no shards are ready, therefore it raises the error. A quick fix for this would be to return None if no shards are ready.
2017-11-12 16:58:30 -05:00
Rapptz
47a58d354d Reimplement zlib streaming.
This time with less bugs. It turned out that the crash was due to a
synchronisation issue between the pending reads and the actual shard
polling mechanism.

Essentially the pending reads would be cancelled via a simple bool but
there would still be a pass left and thus we would have a single
pending read left before or after running the polling mechanism and
this would cause a race condition.

Now the pending read mechanism is properly waited for before returning
control back to the caller.
2017-10-14 21:19:46 -04:00
Rapptz
0f7482ed6e Add Client.latency, AutoShardedClient.latency and latencies.
This should allow an easier way to query the Discord protocol gateway
latency, defined by the difference HEARTBEAT_ACK between and the last
sent HEARTBEAT.
2017-08-15 23:41:11 -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