def run(self):
"""Run the bot."""
with ClientSession() as session:
url = self.url + "?"
url += urlencode({"v": self.API_VERSION, "encoding": json})
while not self.running.done():
print("Bot is connecting...")
self.ws_running = asyncio.Future()
async with session.ws_connect(url) as ws:
self.ws = ws
while not self.running.done():
# Reading the message.
data = await self._receive()
if not data:
break
await self._handle(data)
# Cleanup
self.futures = [f
for f in self.futures if not f.done()]
# Close the tasks
# Wait for them.
print("Bot is closing...")
self.ws_running.cancel()
while self.futures:
try:
await asyncio.gather(*self.futures)
except CancelledError:
pass
self.futures = [f
for f in self.futures if not f.done()]
评论列表
文章目录