diff --git a/data/pluscampaign-v1.sql b/data/pluscampaign-v1.sql index e0a74ef..4df05c3 100644 --- a/data/pluscampaign-v1.sql +++ b/data/pluscampaign-v1.sql @@ -40,7 +40,7 @@ CREATE TABLE campaign_rewards_earned( reference TEXT, logged_messageid BIGINT, earned_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), - earned_reason TEXT NOT NULL, + earned_from TEXT NOT NULL, _timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW() ); diff --git a/plugin/campaign.py b/plugin/campaign.py index 60b9543..932d947 100644 --- a/plugin/campaign.py +++ b/plugin/campaign.py @@ -57,7 +57,7 @@ class RewardCampaign: async def add_reward( self, profileid: int, - earned_reason: str, + earned_from: str, event_id: Optional[int] = None, twitch_user_id: Optional[str] = None, twitch_user_name: Optional[str] = None, @@ -66,7 +66,7 @@ class RewardCampaign: row = await EarnedReward.create( campaign_id=self.row.campaign_id, profileid=profileid, - earned_reason=earned_reason, + earned_from=earned_from, event_id=event_id, twitch_user_id=twitch_user_id, twitch_user_name=twitch_user_name, @@ -93,6 +93,7 @@ class RewardCampaign: async def try_to_log_reward(self, reward: EarnedReward): import discord + if self.webhook: embed = await self._log_format_reward(reward) if reward.log_messageid: @@ -107,7 +108,9 @@ class RewardCampaign: await reward.update(log_messageid=message.id) except discord.HTTPException: # Couldn't send, give up. - logger.warning(f"Failed to log campaign reward {reward!r}", exc_info=True) + logger.warning( + f"Failed to log campaign reward {reward!r}", exc_info=True + ) async def _log_format_reward(self, reward: EarnedReward): """ @@ -125,44 +128,39 @@ class RewardCampaign: f"Twitch user `{reward.twitch_user_name or 'Unknown'}` `(ID: {reward.twitch_user_id or 'Unknown'})`.\n" f"Internal ID `{reward.profileid}`" ), - inline=True + inline=True, ) # Earning field embed.add_field( - name="Reward Earned", + name="Reward Earned", value=( f"Earned campaign reward at {discord.utils.format_dt(reward.earned_at, 'F')}.\n" f"Earned reason: '{reward.earned_from}'" ), - inline=True + inline=True, ) # Reference field embed.add_field( name="Reference", value=reward.reference or "No Reference information saved.", - inline=False + inline=False, ) # Modnote field embed.add_field( - name="Notes", - value=reward.modnote or "No notes added", - inline=False + name="Notes", value=reward.modnote or "No notes added", inline=False ) # Fulfilled field if reward.fulfilled_at is not None: fluffed = f"Fluffed at {discord.utils.format_dt(reward.fulfilled_at, 'F')}" if reward.fulfilled_note: - fluffed += '\n' + "Fluff Note: " + reward.fulfilled_note + fluffed += "\n" + "Fluff Note: " + reward.fulfilled_note else: fluffed = "Not yet fluffed" - embed.add_field( - name="Fulfilled", - value=fluffed - ) + embed.add_field(name="Fulfilled", value=fluffed) embed.set_footer(text="Last Updated") embed.timestamp = utc_now() @@ -173,7 +171,9 @@ class RewardCampaign: class CampaignRegistry: VERSION = CampaignData.VERSION - def __init__(self, data: CampaignData, session: aiohttp.ClientSession | None = None): + def __init__( + self, data: CampaignData, session: aiohttp.ClientSession | None = None + ): self.data = data # TODO: Actually pass in a session self._session = aiohttp.ClientSession() @@ -238,27 +238,18 @@ class CampaignRegistry: return camp async def create_campaign( - self, - cid: int, - campaign_name: str, - **kwargs + self, cid: int, campaign_name: str, **kwargs ) -> RewardCampaign: """ Create a new campaign. The name must be unique (ignoring case) to facilitate easy lookup. """ row = await Campaign.create( - communityid=cid, - campaign_name=campaign_name, - **kwargs + communityid=cid, campaign_name=campaign_name, **kwargs ) return RewardCampaign(row, session=self._session) - async def update_campaign( - self, - campaign_id: int, - **kwargs - ) -> RewardCampaign: + async def update_campaign(self, campaign_id: int, **kwargs) -> RewardCampaign: """ Update a campaign with the given data args. diff --git a/plugin/twitch/component.py b/plugin/twitch/component.py index 744baab..647ee86 100644 --- a/plugin/twitch/component.py +++ b/plugin/twitch/component.py @@ -74,7 +74,7 @@ class CampaignComponent(cmds.Component): # Add a reward to the database with the correct info. await campaign.add_reward( profileid=event_row["profileid"], - earned_reason=f"(SUB) User subscribed for {duration} months at tier {tier}", + earned_from=f"(SUB) 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"], @@ -83,6 +83,31 @@ class CampaignComponent(cmds.Component): # TODO: Webhook logging maybe.. (do this in campaign) # Or just general logging. (also do this in campaign, but we can do again here) + @cmds.Component.listener() + async def event_message(self, payload): + community = await self.bot.profiles.fetch_community(payload.broadcaster) + cid = community.communityid + profile = await self.bot.profiles.fetch_profile(payload.chatter) + pid = profile.profileid + campaigns = await self.campaigns.fetch_campaigns(cid) + + for campaign in campaigns: + 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=pid, + earned_from=f"(MSG) User messaged in chat: {payload.text}", + event_id=None, + twitch_user_id=payload.chatter.id, + twitch_user_name=payload.chatter.name, + ) + await self.dispatch_update(campaign) + @cmds.Component.listener() async def event_safe_event_chat_notice_resub(self, payload): # Check that the end of the sub is past the threshold by adding duration to the previous sub @@ -137,11 +162,9 @@ class CampaignComponent(cmds.Component): reward_cap = campaign.row.target_rewards if reward_cap is not None: - response = ( - f"{name}: {rewards_earned} rewards earned out of {reward_cap}!" - ) + response = "{name}: {given} rewards earned out of {cap}!" else: - response = f"{name}: {rewards_earned} rewards earned so far!" + response = "{name}: {given} rewards earned so far!" formatted = response.format( name=campaign.row.campaign_name, given=rewards_earned,