Files
2026-09-15 19:40:13 +10:00

82 lines
2.3 KiB
Python

import asyncio
import logging
import websockets
from aiohttp import web
from twitchio.web import AiohttpAdapter
from meta import Bot, conf, setup_main_logger, args, sockets
from data import Database
from modules import twitch_setup
logger = logging.getLogger(__name__)
class ProxyAiohttpAdapter(AiohttpAdapter):
"""
Overrides the computed AiohttpAdapter redirect_url
to always use provided domain.
"""
def _find_redirect(self, request):
return self.redirect_url
async def oauth_callback(self, request: web.Request) -> web.Response:
payload: FetchTokenPayload = await self.fetch_token(request)
# Change the default success response to no content...
print(payload)
if 200 <= payload.response.status < 300:
return web.Response(
status=200,
text=(
"Successfully added Croccyfocus to your channel! Good luck with your hyperfocused sessions!\n"
"<p>For further usage and setup information, see the documentation <a href=\"{docs}\">here</a>.</p>"
).format(
docs="https://git.thewisewolf.dev/HoloTech/hyperfocus-twitch-bot/wiki/Focus-docs"
),
content_type="text/html",
)
# Return the default error responses...
return payload.response
async def main():
db = Database(conf.data['args'])
async with db.open():
adapter_keys = (
'host', 'domain', 'port',
'redirect_path', 'oauth_path', 'eventsub_path',
'eventsub_secret',
)
adapter_args = {}
for key in adapter_keys:
value = conf.twitch.get(key, '').strip()
if value:
if key == 'port':
value = int(value)
adapter_args[key] = value
adapter = ProxyAiohttpAdapter(**adapter_args)
bot = Bot(
config=conf,
dbconn=db,
adapter=adapter,
setup=twitch_setup,
using_webhooks=conf.twitch.getboolean('webhooks', False)
)
async with websockets.serve(sockets.root_handler, '', conf.wserver.getint('port')):
try:
await bot.start()
finally:
await bot.close()
def _main():
setup_main_logger()
asyncio.run(main())