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.
This commit is contained in:
verixx 2017-11-02 20:52:26 +11:00 committed by Rapptz
parent b5bc5a36ce
commit 542924d5df

View File

@ -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