Add Client.clear to clear the bot's internal state to a clean slate.

In case you want to have some external restart loop.
This commit is contained in:
Rapptz 2017-12-16 21:36:39 -05:00
parent 45432ab26f
commit 5c5a59c18e
2 changed files with 16 additions and 0 deletions

View File

@ -462,6 +462,18 @@ class Client:
yield from self.http.close()
self._ready.clear()
def clear(self):
"""Clears the internal state of the bot.
After this, the bot can be considered "re-opened", i.e. :meth:`.is_closed`
and :meth:`.is_ready` both return ``False`` along with the bot's internal
cache cleared.
"""
self._closed.clear()
self._ready.clear()
self._connection.clear()
self._http.recreate()
@asyncio.coroutine
def start(self, *args, **kwargs):
"""|coro|

View File

@ -102,6 +102,10 @@ class HTTPClient:
user_agent = 'DiscordBot (https://github.com/Rapptz/discord.py {0}) Python/{1[0]}.{1[1]} aiohttp/{2}'
self.user_agent = user_agent.format(__version__, sys.version_info, aiohttp.__version__)
def recreate(self):
if self._session.closed:
self._session = aiohttp.ClientSession(connector=self.connector, loop=self.loop)
@asyncio.coroutine
def request(self, route, *, header_bypass_delay=None, **kwargs):
bucket = route.bucket