From 542924d5df8553ded17bb7ec4b724d85a1d44820 Mon Sep 17 00:00:00 2001 From: verixx Date: Thu, 2 Nov 2017 20:52:26 +1100 Subject: [PATCH] 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. --- discord/shard.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/discord/shard.py b/discord/shard.py index f7f230db8..32d448cc3 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