Fix profile fetching.

This commit is contained in:
2025-07-28 13:51:34 +10:00
parent 239b778f27
commit af95977577
3 changed files with 46 additions and 30 deletions

View File

@@ -6,7 +6,7 @@ from twitchio.ext import commands
from twitchio import Scopes, eventsub from twitchio import Scopes, eventsub
from data import Database from data import Database
from datamodels import BotData, UserAuth, BotChannel from datamodels import BotData, Communities, UserAuth, BotChannel, UserProfile
from .config import Conf from .config import Conf
@@ -39,6 +39,22 @@ class CrocBot(commands.Bot):
# logger.info(f"Logged in as {self.nick}. User id is {self.user_id}") # logger.info(f"Logged in as {self.nick}. User id is {self.user_id}")
logger.info("Logged in as %s", self.bot_id) logger.info("Logged in as %s", self.bot_id)
async def profile_fetch(self, twitchid, name):
profiles = await UserProfile.fetch_where(twitchid=twitchid)
if not profiles:
profile = await UserProfile.create(twitchid=twitchid, name=name)
else:
profile = profiles[0]
return profile
async def community_fetch(self, twitchid, name):
profiles = await Communities.fetch_where(twitchid=twitchid)
if not profiles:
profile = await Communities.create(twitchid=twitchid, name=name)
else:
profile = profiles[0]
return profile
async def setup_hook(self): async def setup_hook(self):
await self.data.init() await self.data.init()

View File

@@ -86,10 +86,10 @@ class SubathonComponent(cmds.Component):
# ----- Methods ----- # ----- Methods -----
async def get_community(self, twitchid: str, name: str | None) -> Communities: async def get_community(self, twitchid: str, name: str | None) -> Communities:
return await Communities.fetch_or_create(twitchid=twitchid, name=name) return await self.bot.community_fetch(twitchid=twitchid, name=name)
async def get_profile(self, twitchid: str, name: str | None) -> UserProfile: async def get_profile(self, twitchid: str, name: str | None) -> UserProfile:
return await UserProfile.fetch_or_create(twitchid=twitchid, name=name) return await self.bot.profile_fetch(twitchid=twitchid, name=name)
async def get_active_subathon(self, communityid: int) -> ActiveSubathon | None: async def get_active_subathon(self, communityid: int) -> ActiveSubathon | None:
rows = await Subathon.fetch_where(communityid=communityid, ended_at=None) rows = await Subathon.fetch_where(communityid=communityid, ended_at=None)
@@ -211,7 +211,7 @@ class SubathonComponent(cmds.Component):
# end stream => Automatically pause the timer # end stream => Automatically pause the timer
@cmds.Component.listener() @cmds.Component.listener()
async def event_stream_offline(self, payload: twitchio.StreamOffline): async def event_stream_offline(self, payload: twitchio.StreamOffline):
community = await Communities.fetch_or_create(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name)
cid = community.communityid cid = community.communityid
if (active := await self.get_active_subathon(cid)) is not None: if (active := await self.get_active_subathon(cid)) is not None:
if active.running: if active.running:
@@ -233,7 +233,7 @@ class SubathonComponent(cmds.Component):
@group_subathon.command(name='setup') @group_subathon.command(name='setup')
async def cmd_setup(self, ctx: cmds.Context, initial_hours: float, sub1: float, sub2: float, sub3: float, bit: float, timescore: int): async def cmd_setup(self, ctx: cmds.Context, initial_hours: float, sub1: float, sub2: float, sub3: float, bit: float, timescore: int):
if ctx.broadcaster: if ctx.broadcaster:
community = await Communities.fetch_or_create(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name) community = await self.bot.community_fetch(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name)
cid = community.communityid cid = community.communityid
if (active := await self.get_active_subathon(cid)) is not None: if (active := await self.get_active_subathon(cid)) is not None:
await ctx.reply("There is already an active subathon running! Use !subathon stop to stop it!") await ctx.reply("There is already an active subathon running! Use !subathon stop to stop it!")
@@ -256,7 +256,7 @@ class SubathonComponent(cmds.Component):
@group_subathon.command(name='stop') @group_subathon.command(name='stop')
async def cmd_stop(self, ctx: cmds.Context): async def cmd_stop(self, ctx: cmds.Context):
if ctx.broadcaster: if ctx.broadcaster:
community = await Communities.fetch_or_create(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name) community = await self.bot.community_fetch(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name)
cid = community.communityid cid = community.communityid
if (active := await self.get_active_subathon(cid)) is not None: if (active := await self.get_active_subathon(cid)) is not None:
if active.running: if active.running:
@@ -277,7 +277,7 @@ class SubathonComponent(cmds.Component):
@group_subathon.command(name='pause') @group_subathon.command(name='pause')
async def cmd_pause(self, ctx: cmds.Context): async def cmd_pause(self, ctx: cmds.Context):
if ctx.broadcaster or ctx.author.moderator: if ctx.broadcaster or ctx.author.moderator:
community = await Communities.fetch_or_create(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name) community = await self.bot.community_fetch(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name)
cid = community.communityid cid = community.communityid
if (active := await self.get_active_subathon(cid)) is not None: if (active := await self.get_active_subathon(cid)) is not None:
if active.running: if active.running:
@@ -293,7 +293,7 @@ class SubathonComponent(cmds.Component):
@group_subathon.command(name='resume') @group_subathon.command(name='resume')
async def cmd_resume(self, ctx: cmds.Context): async def cmd_resume(self, ctx: cmds.Context):
if ctx.broadcaster or ctx.author.moderator: if ctx.broadcaster or ctx.author.moderator:
community = await Communities.fetch_or_create(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name) community = await self.bot.community_fetch(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name)
cid = community.communityid cid = community.communityid
if (active := await self.get_active_subathon(cid)) is not None: if (active := await self.get_active_subathon(cid)) is not None:
if not active.running: if not active.running:
@@ -308,7 +308,7 @@ class SubathonComponent(cmds.Component):
@cmds.group(name='goals', invoke_fallback=True) @cmds.group(name='goals', invoke_fallback=True)
async def group_goals(self, ctx: cmds.Context): async def group_goals(self, ctx: cmds.Context):
# List the goals # List the goals
community = await Communities.fetch_or_create(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name) community = await self.bot.community_fetch(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name)
cid = community.communityid cid = community.communityid
if (active := await self.get_active_subathon(cid)) is not None: if (active := await self.get_active_subathon(cid)) is not None:
goals = await active.get_goals() goals = await active.get_goals()
@@ -328,7 +328,7 @@ class SubathonComponent(cmds.Component):
@group_goals.command(name='add') @group_goals.command(name='add')
async def cmd_add(self, ctx: cmds.Context, required: int, description: str): async def cmd_add(self, ctx: cmds.Context, required: int, description: str):
if ctx.broadcaster or ctx.author.moderator: if ctx.broadcaster or ctx.author.moderator:
community = await Communities.fetch_or_create(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name) community = await self.bot.community_fetch(twitchid=ctx.broadcaster.id, name=ctx.broadcaster.name)
cid = community.communityid cid = community.communityid
if (active := await self.get_active_subathon(cid)) is not None: if (active := await self.get_active_subathon(cid)) is not None:
await SubathonGoal.create( await SubathonGoal.create(

View File

@@ -131,9 +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 Communities.fetch_or_create(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name)
cid = community.communityid cid = community.communityid
profile = await UserProfile.fetch_or_create( profile = await self.bot.profile_fetch(
twitchid=payload.user.id, name=payload.user.name, twitchid=payload.user.id, name=payload.user.name,
) )
pid = profile.profileid pid = profile.profileid
@@ -160,9 +160,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 Communities.fetch_or_create(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name)
cid = community.communityid cid = community.communityid
profile = await UserProfile.fetch_or_create( profile = await self.bot.profile_fetch(
twitchid=payload.user.id, name=payload.user.name, twitchid=payload.user.id, name=payload.user.name,
) )
pid = profile.profileid pid = profile.profileid
@@ -188,9 +188,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 Communities.fetch_or_create(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name)
cid = community.communityid cid = community.communityid
profile = await UserProfile.fetch_or_create( profile = await self.bot.profile_fetch(
twitchid=payload.user.id, name=payload.user.name, twitchid=payload.user.id, name=payload.user.name,
) )
pid = profile.profileid pid = profile.profileid
@@ -214,9 +214,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 Communities.fetch_or_create(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name)
cid = community.communityid cid = community.communityid
profile = await UserProfile.fetch_or_create( profile = await self.bot.profile_fetch(
twitchid=payload.user.id, name=payload.user.name, twitchid=payload.user.id, name=payload.user.name,
) )
pid = profile.profileid pid = profile.profileid
@@ -241,9 +241,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 Communities.fetch_or_create(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name)
cid = community.communityid cid = community.communityid
profile = await UserProfile.fetch_or_create( profile = await self.bot.profile_fetch(
twitchid=payload.user.id, name=payload.user.name, twitchid=payload.user.id, name=payload.user.name,
) )
pid = profile.profileid pid = profile.profileid
@@ -266,10 +266,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 Communities.fetch_or_create(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name)
cid = community.communityid cid = community.communityid
if payload.user is not None: if payload.user is not None:
profile = await UserProfile.fetch_or_create( profile = await self.bot.profile_fetch(
twitchid=payload.user.id, name=payload.user.name, twitchid=payload.user.id, name=payload.user.name,
) )
pid = profile.profileid pid = profile.profileid
@@ -294,9 +294,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 Communities.fetch_or_create(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name)
cid = community.communityid cid = community.communityid
profile = await UserProfile.fetch_or_create( profile = await self.bot.profile_fetch(
twitchid=payload.user.id, name=payload.user.name, twitchid=payload.user.id, name=payload.user.name,
) )
pid = profile.profileid pid = profile.profileid
@@ -321,7 +321,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 Communities.fetch_or_create(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name)
cid = community.communityid cid = community.communityid
event_row = await self.data.events.insert( event_row = await self.data.events.insert(
@@ -340,7 +340,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 Communities.fetch_or_create(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name)
cid = community.communityid cid = community.communityid
event_row = await self.data.events.insert( event_row = await self.data.events.insert(
@@ -368,7 +368,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 Communities.fetch_or_create(twitchid=broadcaster.id, name=broadcaster.name) community = await self.bot.community_fetch(twitchid=broadcaster.id, name=broadcaster.name)
cid = community.communityid cid = community.communityid
event_row = await self.data.events.insert( event_row = await self.data.events.insert(
@@ -386,7 +386,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 Communities.fetch_or_create(twitchid=broadcaster.id, name=broadcaster.name) community = await self.bot.community_fetch(twitchid=broadcaster.id, name=broadcaster.name)
cid = community.communityid cid = community.communityid
event_row = await self.data.events.insert( event_row = await self.data.events.insert(
@@ -405,9 +405,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 Communities.fetch_or_create(twitchid=payload.broadcaster.id, name=payload.broadcaster.name) community = await self.bot.community_fetch(twitchid=payload.broadcaster.id, name=payload.broadcaster.name)
cid = community.communityid cid = community.communityid
profile = await UserProfile.fetch_or_create( profile = await self.bot.profile_fetch(
twitchid=payload.chatter.id, name=payload.chatter.name, twitchid=payload.chatter.id, name=payload.chatter.name,
) )
pid = profile.profileid pid = profile.profileid