]> git.localhorst.tv Git - aosr-racetime.git/commitdiff
dummy bot master
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 28 May 2022 16:23:24 +0000 (18:23 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 28 May 2022 16:23:24 +0000 (18:23 +0200)
bot.py [new file with mode: 0644]

diff --git a/bot.py b/bot.py
new file mode 100644 (file)
index 0000000..0972e4f
--- /dev/null
+++ b/bot.py
@@ -0,0 +1,78 @@
+import logging
+import requests
+
+from racetime_bot import Bot, RaceHandler
+
+class AosrRaceHandler(RaceHandler):
+
+    stop_at = ['cancelled', 'finished']
+
+    async def begin(self):
+        if self.should_stop():
+            return
+        if not self.state.get('intro_sent') and not self._race_in_progress():
+            await self.send_message(
+                'Welcome to AoSR! Create a seed with !preset <preset> or !race <preset>'
+            )
+            self.state['intro_sent'] = True
+
+    async def ex_preset(self, args, message):
+        try:
+            preset_name = args[0]
+        except IndexError:
+            await self.send_message(
+                'You must specify a preset!'
+            )
+            return
+        await self.roll_game(preset_name, race=False)
+
+    async def ex_race(self, args, message):
+        try:
+            preset_name = args[0]
+        except IndexError:
+            await self.send_message(
+                'You must specify a preset!'
+            )
+            return
+        await self.roll_game(preset_name, race=True)
+
+    async def roll_game(self, preset_name, race=False):
+        await self.send_message('Generating game, please wait.')
+        params = { 'preset': preset_name, 'race': '1' if race else '0' }
+        try:
+            response = requests.post('https://aos.localhorst.tv/api/aos-generate', data=params, headers={ 'Accept': 'application/json' })
+            if response.status_code == 404:
+                await self.send_message(
+                    'unknown preset'
+                )
+                return
+            response.raise_for_status()
+        except requests.exceptions.RequestException as e:
+            print(e)
+            await self.send_message(
+                'error generating seed'
+            )
+            return
+        json = response.json();
+        await self.send_message(
+            'Seed: https://aos.localhorst.tv/h/%(hash)s' % json
+        )
+        await self.set_bot_raceinfo('%(preset)s - https://aos.localhorst.tv/h/%(hash)s' % json)
+
+    def _race_in_progress(self):
+        return self.data.get('status').get('value') in ('pending', 'in_progress')
+
+class AosrBot(Bot):
+
+    def get_handler_class(self):
+        return AosrRaceHandler
+
+logger = logging.getLogger()
+
+bot = AosrBot(
+    category_slug='aosr',
+    client_id='2NUPncWc83ygvx3TZyCuCYgdlrwogAvHf6rYYnHz',
+    client_secret='j5XN5gcQzWJiS25R4dhQquSREgYdlDJgTbvAMhyHGbnK3ROHsXpOzl0JUIHLU1n91qIOfzkiJN864RICWTBi9pnNIrkvvWNtZH77FC39QnABQ5CNPW0YWPpjC8M0f9hb',
+    logger=logger,
+)
+bot.run()