Compare commits

...

2 Commits

Author SHA1 Message Date
f853f01bf8 fix: Version module typo. 2025-09-02 07:57:13 +10:00
4707bc2aca Move to new plugin framework.
- User `profiles` component for profile and community fetch.
- Add data version check
- Move `datamodels` to `botdata`.
2025-09-02 07:14:51 +10:00
4 changed files with 29 additions and 41 deletions

View File

@@ -1,3 +1 @@
async def twitch_setup(bot): from .tracker import setup as twitch_setup
from .tracker import setup
await setup(bot)

View File

@@ -1,3 +1,5 @@
BEGIN;
INSERT INTO version_history (component, from_version, to_version, author) VALUES ('EVENT_TRACKER', 0, 1, 'Initial Creation'); INSERT INTO version_history (component, from_version, to_version, author) VALUES ('EVENT_TRACKER', 0, 1, 'Initial Creation');
-- TODO: Assert profiles data version -- 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) FOREIGN KEY (event_id, event_type) REFERENCES events (event_id, event_type)
); );
-- }}} -- }}}
COMMIT;

View File

@@ -4,7 +4,7 @@ import twitchio
from twitchio import PartialUser, Scopes, eventsub from twitchio import PartialUser, Scopes, eventsub
from twitchio.ext import commands as cmds from twitchio.ext import commands as cmds
from datamodels import BotChannel, Communities, UserProfile from botdata import BotChannel
from meta import Bot from meta import Bot
from utils.lib import utc_now from utils.lib import utc_now
@@ -16,7 +16,6 @@ class TrackerComponent(cmds.Component):
def __init__(self, bot: Bot): def __init__(self, bot: Bot):
self.bot = bot self.bot = bot
self.data = bot.dbconn.load_registry(EventData()) self.data = bot.dbconn.load_registry(EventData())
print(self.__all_listeners__)
# One command, which sends join link to start tracking # One command, which sends join link to start tracking
# Utility for fetch_or_create community and profiles and setting names # Utility for fetch_or_create community and profiles and setting names
@@ -25,6 +24,7 @@ class TrackerComponent(cmds.Component):
# ----- API ----- # ----- API -----
async def component_load(self): async def component_load(self):
await self.data.init() await self.data.init()
await self.bot.version_check(*self.data.VERSION)
async def component_teardown(self): async def component_teardown(self):
pass pass
@@ -131,11 +131,9 @@ class TrackerComponent(cmds.Component):
async def event_custom_redemption_add(self, payload: twitchio.ChannelPointsRedemptionAdd): async def event_custom_redemption_add(self, payload: twitchio.ChannelPointsRedemptionAdd):
tracked = await TrackingChannel.fetch(payload.broadcaster.id) tracked = await TrackingChannel.fetch(payload.broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
profile = await self.bot.profile_fetch( profile = await self.bot.profiles.fetch_profile(payload.user)
twitchid=payload.user.id, name=payload.user.name,
)
pid = profile.profileid pid = profile.profileid
event_row = await self.data.events.insert( 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): async def event_custom_redemption_update(self, payload: twitchio.ChannelPointsRedemptionUpdate):
tracked = await TrackingChannel.fetch(payload.broadcaster.id) tracked = await TrackingChannel.fetch(payload.broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
profile = await self.bot.profile_fetch( profile = await self.bot.profiles.fetch_profile(payload.user)
twitchid=payload.user.id, name=payload.user.name,
)
pid = profile.profileid pid = profile.profileid
event_row = await self.data.events.insert( event_row = await self.data.events.insert(
@@ -188,11 +184,9 @@ class TrackerComponent(cmds.Component):
async def event_follow(self, payload: twitchio.ChannelFollow): async def event_follow(self, payload: twitchio.ChannelFollow):
tracked = await TrackingChannel.fetch(payload.broadcaster.id) tracked = await TrackingChannel.fetch(payload.broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
profile = await self.bot.profile_fetch( profile = await self.bot.profiles.fetch_profile(payload.user)
twitchid=payload.user.id, name=payload.user.name,
)
pid = profile.profileid pid = profile.profileid
# Computer follower count # Computer follower count
@@ -214,11 +208,9 @@ class TrackerComponent(cmds.Component):
async def event_bits_use(self, payload: twitchio.ChannelBitsUse): async def event_bits_use(self, payload: twitchio.ChannelBitsUse):
tracked = await TrackingChannel.fetch(payload.broadcaster.id) tracked = await TrackingChannel.fetch(payload.broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
profile = await self.bot.profile_fetch( profile = await self.bot.profiles.fetch_profile(payload.user)
twitchid=payload.user.id, name=payload.user.name,
)
pid = profile.profileid pid = profile.profileid
event_row = await self.data.events.insert( event_row = await self.data.events.insert(
@@ -241,11 +233,9 @@ class TrackerComponent(cmds.Component):
async def event_subscription(self, payload: twitchio.ChannelSubscribe): async def event_subscription(self, payload: twitchio.ChannelSubscribe):
tracked = await TrackingChannel.fetch(payload.broadcaster.id) tracked = await TrackingChannel.fetch(payload.broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
profile = await self.bot.profile_fetch( profile = await self.bot.profiles.fetch_profile(payload.user)
twitchid=payload.user.id, name=payload.user.name,
)
pid = profile.profileid pid = profile.profileid
event_row = await self.data.events.insert( event_row = await self.data.events.insert(
@@ -266,12 +256,10 @@ class TrackerComponent(cmds.Component):
async def event_subscription_gift(self, payload: twitchio.ChannelSubscriptionGift): async def event_subscription_gift(self, payload: twitchio.ChannelSubscriptionGift):
tracked = await TrackingChannel.fetch(payload.broadcaster.id) tracked = await TrackingChannel.fetch(payload.broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
if payload.user is not None: if payload.user is not None:
profile = await self.bot.profile_fetch( profile = await self.bot.profiles.fetch_profile(payload.user)
twitchid=payload.user.id, name=payload.user.name,
)
pid = profile.profileid pid = profile.profileid
else: else:
pid = None pid = None
@@ -294,11 +282,9 @@ class TrackerComponent(cmds.Component):
async def event_subscription_message(self, payload: twitchio.ChannelSubscriptionMessage): async def event_subscription_message(self, payload: twitchio.ChannelSubscriptionMessage):
tracked = await TrackingChannel.fetch(payload.broadcaster.id) tracked = await TrackingChannel.fetch(payload.broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
profile = await self.bot.profile_fetch( profile = await self.bot.profiles.fetch_profile(payload.user)
twitchid=payload.user.id, name=payload.user.name,
)
pid = profile.profileid pid = profile.profileid
event_row = await self.data.events.insert( event_row = await self.data.events.insert(
@@ -322,7 +308,7 @@ class TrackerComponent(cmds.Component):
async def event_stream_online(self, payload: twitchio.StreamOnline): async def event_stream_online(self, payload: twitchio.StreamOnline):
tracked = await TrackingChannel.fetch(payload.broadcaster.id) tracked = await TrackingChannel.fetch(payload.broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
event_row = await self.data.events.insert( event_row = await self.data.events.insert(
@@ -341,7 +327,7 @@ class TrackerComponent(cmds.Component):
async def event_stream_offline(self, payload: twitchio.StreamOffline): async def event_stream_offline(self, payload: twitchio.StreamOffline):
tracked = await TrackingChannel.fetch(payload.broadcaster.id) tracked = await TrackingChannel.fetch(payload.broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
event_row = await self.data.events.insert( 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): async def _event_raid_out(self, broadcaster: PartialUser, to_broadcaster: PartialUser, viewer_count: int):
tracked = await TrackingChannel.fetch(broadcaster.id) tracked = await TrackingChannel.fetch(broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
event_row = await self.data.events.insert( 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): async def _event_raid_in(self, broadcaster: PartialUser, from_broadcaster: PartialUser, viewer_count: int):
tracked = await TrackingChannel.fetch(broadcaster.id) tracked = await TrackingChannel.fetch(broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
event_row = await self.data.events.insert( event_row = await self.data.events.insert(
@@ -406,11 +392,9 @@ class TrackerComponent(cmds.Component):
async def event_message(self, payload: twitchio.ChatMessage): async def event_message(self, payload: twitchio.ChatMessage):
tracked = await TrackingChannel.fetch(payload.broadcaster.id) tracked = await TrackingChannel.fetch(payload.broadcaster.id)
if tracked and tracked.joined: 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 cid = community.communityid
profile = await self.bot.profile_fetch( profile = await self.bot.profiles.fetch_profile(payload.chatter)
twitchid=payload.chatter.id, name=payload.chatter.name,
)
pid = profile.profileid pid = profile.profileid
event_row = await self.data.events.insert( event_row = await self.data.events.insert(

View File

@@ -13,6 +13,8 @@ class TrackingChannel(RowModel):
class EventData(Registry): class EventData(Registry):
VERSION = ('EVENT_TRACKER', 1)
tracking_channels = TrackingChannel.table tracking_channels = TrackingChannel.table
events = Table('events') events = Table('events')