Add setup

Closes https://git.autonomic.zone/decentral1se/xbotlib/issues/36.
This commit is contained in:
Luke Murphy 2021-01-24 13:00:47 +01:00
parent dd40939cbf
commit 6a30078351
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
3 changed files with 18 additions and 0 deletions

View File

@ -1,5 +1,9 @@
# xbotlib x.x.x (UNRELEASED)
# xbotlib x.x.x (UNRELEASED)
- Add `setup` function hook ([#36](https://git.autonomic.zone/decentral1se/xbotlib/issues/36))
# xbotlib 0.15.1 (2021-01-24)
- Save to file based storage on all writes ([#39](https://git.autonomic.zone/decentral1se/xbotlib/issues/39))

View File

@ -25,6 +25,7 @@ you want to chat or just invite your bots for testing.
- [Bot.direct(message)](#bot-direct-message)
- [Bot.group(message)](#bot-group-message)
- [Bot.serve(request)](#bot-serve-request)
- [Bot.setup()](#bot-setup)
- [SimpleMessage](#simplemessage)
- [Bot](#bot)
- [Working with your bot](#working-with-your-bot)
@ -114,6 +115,10 @@ Arguments:
- **request**: the web request
### Bot.setup()
Run some setup logic before starting your bot.
### SimpleMessage
A simple message interface.

View File

@ -673,6 +673,15 @@ class Bot(ClientXMPP):
if self.serve:
self.log.info("Turning on the web server")
self.serve_web()
if hasattr(self, "setup"):
try:
self.setup()
self.log.info("Finished running Bot.setup")
except Exception as exception:
message = f"Bot.setup failed: {exception}"
self.log.info(message)
self.process(forever=False)
except (KeyboardInterrupt, RuntimeError):
pass