Add rudimentary websocket channel.

This commit is contained in:
2026-07-25 10:50:03 +03:00
parent fc5c3d07e4
commit 84de4a7764
2 changed files with 80 additions and 32 deletions
+32 -12
View File
@@ -11,7 +11,8 @@ from utils.lib import utc_now
from . import logger
from ..data import CampaignData
from ..campaign import CampaignRegistry
from ..campaign import CampaignRegistry, RewardCampaign
from ..channel import CampaignPayload, prepare_campaign, CampaignChannel
class CampaignComponent(cmds.Component):
@@ -20,6 +21,9 @@ class CampaignComponent(cmds.Component):
self.data = bot.dbconn.load_registry(CampaignData())
self.campaigns = CampaignRegistry(self.data)
self.channel = CampaignChannel(self.bot.profiles.profiles, self.campaigns)
register_channel("Campaign", self.channel)
# ----- API -----
async def component_load(self):
@@ -30,6 +34,11 @@ class CampaignComponent(cmds.Component):
async def component_teardown(self):
pass
async def dispatch_update(self, campaign: RewardCampaign):
cid = campaign.row.communityid
payload = await prepare_campaign(campaign)
await self.channel.send_campaign_update(cid, payload)
# ------ Event Handlers -----
@cmds.Component.listener()
async def event_safe_event_chat_notice_sub(self, payload):
@@ -55,16 +64,23 @@ class CampaignComponent(cmds.Component):
campaigns = await self.campaigns.fetch_campaigns(event_row["communityid"])
for campaign in campaigns:
# Add a reward to the database with the correct info.
await campaign.add_reward(
profileid=event_row["profileid"],
earned_reason=f"(SUB NOTICE): User subscribed for {duration} months at tier {tier}",
event_id=event_row["event_id"],
twitch_user_id=data["chatter_user_id"],
twitch_user_name=data["chatter_user_name"],
)
# TODO: Webhook logging maybe..
# Or just general logging.
reward_progress = len(await campaign.get_rewards())
if (
campaign.row.target_rewards is None
or reward_progress < campaign.row.target_rewards
):
# Add a reward to the database with the correct info.
await campaign.add_reward(
profileid=event_row["profileid"],
earned_reason=f"(SUB NOTICE): User subscribed for {duration} months at tier {tier}",
event_id=event_row["event_id"],
twitch_user_id=data["chatter_user_id"],
twitch_user_name=data["chatter_user_name"],
)
await self.dispatch_update(campaign)
# TODO: Webhook logging maybe..
# Or just general logging.
@cmds.Component.listener()
async def event_safe_event_chat_notice_resub(self, payload):
@@ -178,6 +194,7 @@ class CampaignComponent(cmds.Component):
f"Success! Your campaign '{name}' has been created and started. "
"Best of luck!"
)
await self.dispatch_update(campaign)
@group_campaign.command(name="finish", aliases=["stop", "complete", "end"])
@cmds.is_moderator()
@@ -221,7 +238,9 @@ class CampaignComponent(cmds.Component):
given=rewards_earned,
cap=reward_cap,
)
await ctx.reply(formatted)
await ctx.reply(formatte
await self.dispatch_update(campaign)
)
@group_campaign.command(name="reward")
@cmds.is_moderator()
@@ -258,3 +277,4 @@ class CampaignComponent(cmds.Component):
await ctx.reply(
f"Successfully added campaign reward to {user.mention}'s account."
)
await self.dispatch_update(campaign)