diff options
| author | verixx <[email protected]> | 2017-11-02 20:52:26 +1100 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2017-11-12 16:58:30 -0500 |
| commit | 542924d5df8553ded17bb7ec4b724d85a1d44820 (patch) | |
| tree | 8cd5a58f355e81a14f6652c04c9038294c1411bb | |
| parent | Fix Spelling Error (diff) | |
| download | discord.py-542924d5df8553ded17bb7ec4b724d85a1d44820.tar.xz discord.py-542924d5df8553ded17bb7ec4b724d85a1d44820.zip | |
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.
| -rw-r--r-- | discord/shard.py | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/discord/shard.py b/discord/shard.py index f7f230db..32d448cc 100644 --- a/discord/shard.py +++ b/discord/shard.py @@ -175,8 +175,10 @@ class AutoShardedClient(Client): This operates similarly to :meth:`.Client.latency` except it uses the average latency of every shard's latency. To get a list of shard latency, check the - :attr:`latencies` property. + :attr:`latencies` property. Returns ``nan`` if there are no shards ready. """ + if not self.shards: + return float('nan') return sum(latency for _, latency in self.latencies) / len(self.shards) @property |