snake.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:snake 作者: AnonymousDapper 项目源码 文件源码
def start_shard(self, shard_id, future=None):
        if self.loop.is_closed():
            print(f"Event loop closed, exiting shard#{shard_id}")

        if future is None:
            print(f"Starting {paint(self.bot_class.__name__, 'cyan')}<{shard_id}>")

        else:
            try:
                result = future.result()

            except asyncio.CancelledError: # task was cancelled, it was probably the close_all call
                return

            except aiohttp.ClientOSError:
                if self.shard_connect_failures[shard_id] < 4:
                    self.shard_connect_failures[shard_id] += 1
                    print(f"Shard#{shard_id} lost connection, retrying with {paint(self.shard_connect_failures[shard_id], 'red')} failures so far")
                else:
                    print(f"Shard#{shard_id} could not connect after 4 retries")
                    return

                if all(retries == 4 for retries in self.shard_connect_failures.values()):
                    print(paint("All shards lost connection.", "red"))
                    sys.exit(1)

            except Exception as e:
                print(f"Shard#{shard_id} failed to run: [{paint(type(e).__name__, 'b_red')}]: {e}")

            else:
                print(f"{paint(self.bot_class.__name__, 'cyan')}<{shard_id}>: {result}")

            print(f"Attempting resume of shard#{shard_id}")

        new_shard = self.bot_class(self, shard_id, *self.args, **self.kwargs)
        new_shard.add_cog(Builtin(new_shard))

        shard_task = self.loop.create_task(new_shard.start(self.credentials["token"]))
        shard_task.add_done_callback(partial(self.start_shard, shard_id)) # oh this is weird

        self.shard_tasks[shard_id] = shard_task
        self.shards[shard_id] = new_shard
        if shard_id not in self.shard_connect_failures:
            self.shard_connect_failures[shard_id] = 0
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号