Compare commits
10 Commits
1d7888ca51
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 52ba72b584 | |||
| df0c6451fc | |||
| e473de3b4b | |||
| 1fb5c45b5b | |||
| 2e02f39c29 | |||
| a3f1fe1322 | |||
| 3f19ec4a17 | |||
| 85f7465283 | |||
| 67fbe1b658 | |||
| 8c868ca4f7 |
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +1,6 @@
|
||||
[submodule "src/data"]
|
||||
path = src/data
|
||||
url = https://git.thewisewolf.dev/HoloTech/psqlmapper.git
|
||||
[submodule "src/modules/profiles"]
|
||||
path = src/modules/profiles
|
||||
url = https://git.thewisewolf.dev/HoloTech/profiles-plugin.git
|
||||
|
||||
@@ -4,7 +4,7 @@ CREATE TABLE version_history(
|
||||
from_version INTEGER NOT NULL,
|
||||
to_version INTEGER NOT NULL,
|
||||
author TEXT NOT NULL,
|
||||
_timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
_timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
INSERT INTO version_history (component, from_version, to_version, author) VALUES ('ROOT', 0, 1, 'Initial Creation');
|
||||
|
||||
|
||||
@@ -4,9 +4,15 @@ bot_id =
|
||||
|
||||
ALSO_READ = config/secrets.conf
|
||||
|
||||
wshost = localhost
|
||||
wsport = 4343
|
||||
wsdomain = localhost:4343
|
||||
[TWTICH]
|
||||
host =
|
||||
port =
|
||||
domain =
|
||||
redirect_path =
|
||||
oauth_path =
|
||||
evenstub_path =
|
||||
|
||||
webhooks =
|
||||
|
||||
[LOGGING]
|
||||
general_log =
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
[CROCBOT]
|
||||
[BOT]
|
||||
client_id =
|
||||
client_secret =
|
||||
|
||||
[TWITCH]
|
||||
eventsub_secret =
|
||||
|
||||
[DATA]
|
||||
args =
|
||||
appid =
|
||||
|
||||
40
src/bot.py
40
src/bot.py
@@ -4,7 +4,7 @@ import websockets
|
||||
|
||||
from twitchio.web import AiohttpAdapter
|
||||
|
||||
from meta import CrocBot, conf, setup_main_logger, args
|
||||
from meta import Bot, conf, setup_main_logger, args, sockets
|
||||
from data import Database
|
||||
|
||||
from modules import twitch_setup
|
||||
@@ -12,28 +12,46 @@ 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 main():
|
||||
db = Database(conf.data['args'])
|
||||
|
||||
async with db.open():
|
||||
adapter = AiohttpAdapter(
|
||||
host=conf.bot.get('wshost', None),
|
||||
port=conf.bot.getint('wsport', None),
|
||||
domain=conf.bot.get('wsdomain', None),
|
||||
eventsub_secret=conf.bot.get('eventsub_secret', None)
|
||||
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 = CrocBot(
|
||||
bot = Bot(
|
||||
config=conf,
|
||||
dbconn=db,
|
||||
adapter=adapter,
|
||||
setup=twitch_setup,
|
||||
using_webhooks=conf.twitch.getboolean('webhooks', False)
|
||||
)
|
||||
|
||||
try:
|
||||
await bot.start()
|
||||
finally:
|
||||
await bot.close()
|
||||
async with websockets.serve(sockets.root_handler, '', conf.wserver.getint('port')):
|
||||
try:
|
||||
await bot.start()
|
||||
finally:
|
||||
await bot.close()
|
||||
|
||||
|
||||
def _main():
|
||||
|
||||
2
src/data
2
src/data
Submodule src/data updated: cfdfe0eb50...334b5f5892
@@ -1,4 +1,5 @@
|
||||
from .args import args
|
||||
from .bot import Bot
|
||||
from .context import Context
|
||||
from .config import Conf, conf
|
||||
from .logger import setup_main_logger, log_context, log_action_stack, log_app, set_logging_context, logging_context, with_log_ctx, persist_task
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import logging
|
||||
from typing import Optional
|
||||
from typing import TYPE_CHECKING, Any, Literal, Optional, overload
|
||||
|
||||
from twitchio.authentication import UserTokenPayload
|
||||
from twitchio.ext import commands
|
||||
@@ -10,6 +10,10 @@ from botdata import BotData, UserAuth, BotChannel, VersionHistory
|
||||
from constants import BOTUSER_SCOPES, CHANNEL_SCOPES, SCHEMA_VERSIONS
|
||||
|
||||
from .config import Conf
|
||||
from .context import Context
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from modules.profiles.profiles.twitch.component import ProfilesComponent
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -24,10 +28,8 @@ class Bot(commands.Bot):
|
||||
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
if config.bot.get('eventsub_secret', None):
|
||||
self.using_webhooks = True
|
||||
else:
|
||||
self.using_webhooks = False
|
||||
# Whether we should do eventsub via webhooks or websockets
|
||||
self.using_webhooks = kwargs.get('using_webhooks', False)
|
||||
|
||||
self.config = config
|
||||
self.dbconn = dbconn
|
||||
@@ -36,6 +38,28 @@ class Bot(commands.Bot):
|
||||
|
||||
self.joined: dict[str, BotChannel] = {}
|
||||
|
||||
# Make the type checker happy about fetching components by name
|
||||
# TODO: Move to stubs
|
||||
|
||||
@property
|
||||
def profiles(self):
|
||||
return self.get_component('ProfilesComponent')
|
||||
|
||||
@overload
|
||||
def get_component(self, name: Literal['ProfilesComponent']) -> 'ProfilesComponent':
|
||||
...
|
||||
|
||||
@overload
|
||||
def get_component(self, name: str) -> Optional[commands.Component]:
|
||||
...
|
||||
|
||||
def get_component(self, name: str) -> Optional[commands.Component]:
|
||||
return super().get_component(name)
|
||||
|
||||
def get_context(self, payload, *, cls: Any = None) -> Context:
|
||||
cls = cls or Context
|
||||
return cls(payload, bot=self)
|
||||
|
||||
async def event_ready(self):
|
||||
# logger.info(f"Logged in as {self.nick}. User id is {self.user_id}")
|
||||
logger.info("Logged in as %s", self.bot_id)
|
||||
@@ -151,17 +175,15 @@ class Bot(commands.Bot):
|
||||
|
||||
# Save the token and scopes to data
|
||||
# Wrap this in a transaction so if it fails halfway we rollback correctly
|
||||
async with self.dbconn.connection() as conn:
|
||||
self.dbconn.conn = conn
|
||||
async with conn.transaction():
|
||||
row = await UserAuth.fetch_or_create(userid, token=token, refresh_token=refresh)
|
||||
if row.token != token or row.refresh_token != refresh:
|
||||
await row.update(token=token, refresh_token=refresh)
|
||||
await self.data.user_auth_scopes.delete_where(userid=userid)
|
||||
await self.data.user_auth_scopes.insert_many(
|
||||
('userid', 'scope'),
|
||||
*((userid, scope) for scope in new_scopes)
|
||||
)
|
||||
# TODO
|
||||
row = await UserAuth.fetch_or_create(userid, token=token, refresh_token=refresh)
|
||||
if row.token != token or row.refresh_token != refresh:
|
||||
await row.update(token=token, refresh_token=refresh)
|
||||
await self.data.user_auth_scopes.delete_where(userid=userid)
|
||||
await self.data.user_auth_scopes.insert_many(
|
||||
('userid', 'scope'),
|
||||
*((userid, scope) for scope in new_scopes)
|
||||
)
|
||||
|
||||
logger.info("Updated auth token for user '%s' with scopes: %s", resp.user_id, ', '.join(new_scopes))
|
||||
return resp
|
||||
|
||||
4
src/meta/context.py
Normal file
4
src/meta/context.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from twitchio.ext import commands as cmds
|
||||
|
||||
class Context(cmds.Context):
|
||||
...
|
||||
@@ -14,6 +14,7 @@ import aiohttp
|
||||
|
||||
from .config import conf
|
||||
from utils.ratelimits import Bucket, BucketOverFull, BucketFull
|
||||
from utils.lib import utc_now
|
||||
|
||||
|
||||
log_logger = logging.getLogger(__name__)
|
||||
@@ -365,6 +366,8 @@ class WebHookHandler(logging.StreamHandler):
|
||||
await self._send(batched)
|
||||
|
||||
async def _send(self, message, as_file=False):
|
||||
import discord
|
||||
from discord import File
|
||||
try:
|
||||
self.bucket.request()
|
||||
except BucketOverFull:
|
||||
|
||||
68
src/meta/sockets.py
Normal file
68
src/meta/sockets.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from abc import ABC
|
||||
from collections import defaultdict
|
||||
import json
|
||||
from typing import Any
|
||||
import logging
|
||||
|
||||
import websockets
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
||||
class Channel(ABC):
|
||||
"""
|
||||
A channel is a stateful connection handler for a group of connected websockets.
|
||||
"""
|
||||
name = "Root Channel"
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.connections = set()
|
||||
|
||||
@property
|
||||
def empty(self):
|
||||
return not self.connections
|
||||
|
||||
async def on_connection(self, websocket: websockets.WebSocketServerProtocol, event: dict[str, Any]):
|
||||
logger.info(f"Channel '{self.name}' attached new connection {websocket=} {event=}")
|
||||
self.connections.add(websocket)
|
||||
|
||||
async def del_connection(self, websocket: websockets.WebSocketServerProtocol):
|
||||
logger.info(f"Channel '{self.name}' dropped connection {websocket=}")
|
||||
self.connections.discard(websocket)
|
||||
|
||||
async def handle_message(self, websocket: websockets.WebSocketServerProtocol, message):
|
||||
raise NotImplementedError
|
||||
|
||||
async def send_event(self, event, websocket=None):
|
||||
message = json.dumps(event)
|
||||
if not websocket:
|
||||
for ws in self.connections:
|
||||
await ws.send(message)
|
||||
else:
|
||||
await websocket.send(message)
|
||||
|
||||
channels = {}
|
||||
|
||||
def register_channel(name, channel: Channel):
|
||||
channels[name] = channel
|
||||
|
||||
|
||||
async def root_handler(websocket: websockets.WebSocketServerProtocol):
|
||||
message = await websocket.recv()
|
||||
event = json.loads(message)
|
||||
|
||||
if event.get('type', None) != 'init':
|
||||
raise ValueError("Received Websocket connection with no init.")
|
||||
|
||||
if (channel_name := event.get('channel', None)) not in channels:
|
||||
raise ValueError(f"Received Init for unhandled channel {channel_name=}")
|
||||
channel = channels[channel_name]
|
||||
|
||||
try:
|
||||
await channel.on_connection(websocket, event)
|
||||
async for message in websocket:
|
||||
await channel.handle_message(websocket, message)
|
||||
finally:
|
||||
await channel.del_connection(websocket)
|
||||
@@ -5,7 +5,5 @@ if TYPE_CHECKING:
|
||||
|
||||
|
||||
async def twitch_setup(bot: 'Bot'):
|
||||
# Import and run setup methods from each module
|
||||
# from . import module
|
||||
# await module.twitch_setup(bot)
|
||||
pass
|
||||
from . import profiles
|
||||
await profiles.twitch_setup(bot)
|
||||
|
||||
1
src/modules/profiles
Submodule
1
src/modules/profiles
Submodule
Submodule src/modules/profiles added at 0363dc2bcd
Reference in New Issue
Block a user