From 4707bc2aca6f0e9c0e0fd50ebbdb35bffeafeb79 Mon Sep 17 00:00:00 2001 From: Interitio Date: Tue, 2 Sep 2025 07:14:51 +1000 Subject: [PATCH] Move to new plugin framework. - User `profiles` component for profile and community fetch. - Add data version check - Move `datamodels` to `botdata`. --- __init__.py | 4 +-- data/tracker.sql | 4 +++ tracker/component.py | 60 ++++++++++++++++---------------------------- tracker/data.py | 2 ++ 4 files changed, 29 insertions(+), 41 deletions(-) diff --git a/__init__.py b/__init__.py index 8341625..58a6307 100644 --- a/__init__.py +++ b/__init__.py @@ -1,3 +1 @@ -async def twitch_setup(bot): - from .tracker import setup - await setup(bot) +from .tracker import setup as twitch_setup diff --git a/data/tracker.sql b/data/tracker.sql index 88115d1..af43dd1 100644 --- a/data/tracker.sql +++ b/data/tracker.sql @@ -1,3 +1,5 @@ +BEGIN; + INSERT INTO version_history (component, from_version, to_version, author) VALUES ('EVENT_TRACKER', 0, 1, 'Initial Creation'); -- TODO: Assert profiles data version @@ -176,3 +178,5 @@ CREATE TABLE raid_in_events( FOREIGN KEY (event_id, event_type) REFERENCES events (event_id, event_type) ); -- }}} + +COMMIT; diff --git a/tracker/component.py b/tracker/component.py index c587241..ed29861 100644 --- a/tracker/component.py +++ b/tracker/component.py @@ -4,7 +4,7 @@ import twitchio from twitchio import PartialUser, Scopes, eventsub from twitchio.ext import commands as cmds -from datamodels import BotChannel, Communities, UserProfile +from botdata import BotChannel from meta import Bot from utils.lib import utc_now @@ -16,7 +16,6 @@ class TrackerComponent(cmds.Component): def __init__(self, bot: Bot): self.bot = bot self.data = bot.dbconn.load_registry(EventData()) - print(self.__all_listeners__) # One command, which sends join link to start tracking # Utility for fetch_or_create community and profiles and setting names @@ -25,6 +24,7 @@ class TrackerComponent(cmds.Component): # ----- API ----- async def component_load(self): await self.data.init() + await self.bot.version_check(*self.data.VERSION) async def component_teardown(self): pass @@ -131,11 +131,9 @@ class TrackerComponent(cmds.Component): async def event_custom_redemption_add(self, payload: twitchio.ChannelPointsRedemptionAdd): tracked = await TrackingChannel.fetch(payload.broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) + community = await self.bot.profiles.fetch_community(payload.broadcaster) cid = community.communityid - profile = await self.bot.profile_fetch( - twitchid=payload.user.id, name=payload.user.name, - ) + profile = await self.bot.profiles.fetch_profile(payload.user) pid = profile.profileid event_row = await self.data.events.insert( @@ -160,11 +158,9 @@ class TrackerComponent(cmds.Component): async def event_custom_redemption_update(self, payload: twitchio.ChannelPointsRedemptionUpdate): tracked = await TrackingChannel.fetch(payload.broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) + community = await self.bot.profiles.fetch_community(payload.broadcaster) cid = community.communityid - profile = await self.bot.profile_fetch( - twitchid=payload.user.id, name=payload.user.name, - ) + profile = await self.bot.profiles.fetch_profile(payload.user) pid = profile.profileid event_row = await self.data.events.insert( @@ -188,11 +184,9 @@ class TrackerComponent(cmds.Component): async def event_follow(self, payload: twitchio.ChannelFollow): tracked = await TrackingChannel.fetch(payload.broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) + community = await self.bot.profiles.fetch_community(payload.broadcaster) cid = community.communityid - profile = await self.bot.profile_fetch( - twitchid=payload.user.id, name=payload.user.name, - ) + profile = await self.bot.profiles.fetch_profile(payload.user) pid = profile.profileid # Computer follower count @@ -214,11 +208,9 @@ class TrackerComponent(cmds.Component): async def event_bits_use(self, payload: twitchio.ChannelBitsUse): tracked = await TrackingChannel.fetch(payload.broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) + community = await self.bot.profiles.fetch_community(payload.broadcaster) cid = community.communityid - profile = await self.bot.profile_fetch( - twitchid=payload.user.id, name=payload.user.name, - ) + profile = await self.bot.profiles.fetch_profile(payload.user) pid = profile.profileid event_row = await self.data.events.insert( @@ -241,11 +233,9 @@ class TrackerComponent(cmds.Component): async def event_subscription(self, payload: twitchio.ChannelSubscribe): tracked = await TrackingChannel.fetch(payload.broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) + community = await self.bot.profiles.fetch_community(payload.broadcaster) cid = community.communityid - profile = await self.bot.profile_fetch( - twitchid=payload.user.id, name=payload.user.name, - ) + profile = await self.bot.profiles.fetch_profile(payload.user) pid = profile.profileid event_row = await self.data.events.insert( @@ -266,12 +256,10 @@ class TrackerComponent(cmds.Component): async def event_subscription_gift(self, payload: twitchio.ChannelSubscriptionGift): tracked = await TrackingChannel.fetch(payload.broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) + community = await self.bot.profiles.fetch_community(payload.broadcaster) cid = community.communityid if payload.user is not None: - profile = await self.bot.profile_fetch( - twitchid=payload.user.id, name=payload.user.name, - ) + profile = await self.bot.profiles.fetch_profile(payload.user) pid = profile.profileid else: pid = None @@ -294,11 +282,9 @@ class TrackerComponent(cmds.Component): async def event_subscription_message(self, payload: twitchio.ChannelSubscriptionMessage): tracked = await TrackingChannel.fetch(payload.broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) + community = await self.bot.profiles.fetch_community(payload.broadcaster) cid = community.communityid - profile = await self.bot.profile_fetch( - twitchid=payload.user.id, name=payload.user.name, - ) + profile = await self.bot.profiles.fetch_profile(payload.user) pid = profile.profileid event_row = await self.data.events.insert( @@ -322,7 +308,7 @@ class TrackerComponent(cmds.Component): async def event_stream_online(self, payload: twitchio.StreamOnline): tracked = await TrackingChannel.fetch(payload.broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) + community = await self.bot.profiles.fetch_community(payload.broadcaster) cid = community.communityid event_row = await self.data.events.insert( @@ -341,7 +327,7 @@ class TrackerComponent(cmds.Component): async def event_stream_offline(self, payload: twitchio.StreamOffline): tracked = await TrackingChannel.fetch(payload.broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) + community = await self.bot.profiles.fetch_community(payload.broadcaster) cid = community.communityid event_row = await self.data.events.insert( @@ -369,7 +355,7 @@ class TrackerComponent(cmds.Component): async def _event_raid_out(self, broadcaster: PartialUser, to_broadcaster: PartialUser, viewer_count: int): tracked = await TrackingChannel.fetch(broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=broadcaster.id, name=broadcaster.name) + community = await self.bot.profiles.fetch_community(broadcaster) cid = community.communityid event_row = await self.data.events.insert( @@ -387,7 +373,7 @@ class TrackerComponent(cmds.Component): async def _event_raid_in(self, broadcaster: PartialUser, from_broadcaster: PartialUser, viewer_count: int): tracked = await TrackingChannel.fetch(broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=broadcaster.id, name=broadcaster.name) + community = await self.bot.profiles.fetch_community(broadcaster) cid = community.communityid event_row = await self.data.events.insert( @@ -406,11 +392,9 @@ class TrackerComponent(cmds.Component): async def event_message(self, payload: twitchio.ChatMessage): tracked = await TrackingChannel.fetch(payload.broadcaster.id) if tracked and tracked.joined: - community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) + community = await self.bot.profiles.fetch_community(payload.broadcaster) cid = community.communityid - profile = await self.bot.profile_fetch( - twitchid=payload.chatter.id, name=payload.chatter.name, - ) + profile = await self.bot.profiles.fetch_profile(payload.chatter) pid = profile.profileid event_row = await self.data.events.insert( diff --git a/tracker/data.py b/tracker/data.py index 65bc6b5..e0315c0 100644 --- a/tracker/data.py +++ b/tracker/data.py @@ -13,6 +13,8 @@ class TrackingChannel(RowModel): class EventData(Registry): + VERSION = ('TRACKER', 1) + tracking_channels = TrackingChannel.table events = Table('events')