fix: Make 'earned_from' column name consistent

This commit is contained in:
2026-07-30 11:58:35 +03:00
parent d8817d4473
commit 5873b0fe65
3 changed files with 48 additions and 34 deletions
+1 -1
View File
@@ -40,7 +40,7 @@ CREATE TABLE campaign_rewards_earned(
reference TEXT, reference TEXT,
logged_messageid BIGINT, logged_messageid BIGINT,
earned_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), earned_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
earned_reason TEXT NOT NULL, earned_from TEXT NOT NULL,
_timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW() _timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW()
); );
+19 -28
View File
@@ -57,7 +57,7 @@ class RewardCampaign:
async def add_reward( async def add_reward(
self, self,
profileid: int, profileid: int,
earned_reason: str, earned_from: str,
event_id: Optional[int] = None, event_id: Optional[int] = None,
twitch_user_id: Optional[str] = None, twitch_user_id: Optional[str] = None,
twitch_user_name: Optional[str] = None, twitch_user_name: Optional[str] = None,
@@ -66,7 +66,7 @@ class RewardCampaign:
row = await EarnedReward.create( row = await EarnedReward.create(
campaign_id=self.row.campaign_id, campaign_id=self.row.campaign_id,
profileid=profileid, profileid=profileid,
earned_reason=earned_reason, earned_from=earned_from,
event_id=event_id, event_id=event_id,
twitch_user_id=twitch_user_id, twitch_user_id=twitch_user_id,
twitch_user_name=twitch_user_name, twitch_user_name=twitch_user_name,
@@ -93,6 +93,7 @@ class RewardCampaign:
async def try_to_log_reward(self, reward: EarnedReward): async def try_to_log_reward(self, reward: EarnedReward):
import discord import discord
if self.webhook: if self.webhook:
embed = await self._log_format_reward(reward) embed = await self._log_format_reward(reward)
if reward.log_messageid: if reward.log_messageid:
@@ -107,7 +108,9 @@ class RewardCampaign:
await reward.update(log_messageid=message.id) await reward.update(log_messageid=message.id)
except discord.HTTPException: except discord.HTTPException:
# Couldn't send, give up. # 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): 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"Twitch user `{reward.twitch_user_name or 'Unknown'}` `(ID: {reward.twitch_user_id or 'Unknown'})`.\n"
f"Internal ID `{reward.profileid}`" f"Internal ID `{reward.profileid}`"
), ),
inline=True inline=True,
) )
# Earning field # Earning field
embed.add_field( embed.add_field(
name="Reward Earned", name="Reward Earned",
value=( value=(
f"Earned campaign reward at {discord.utils.format_dt(reward.earned_at, 'F')}.\n" f"Earned campaign reward at {discord.utils.format_dt(reward.earned_at, 'F')}.\n"
f"Earned reason: '{reward.earned_from}'" f"Earned reason: '{reward.earned_from}'"
), ),
inline=True inline=True,
) )
# Reference field # Reference field
embed.add_field( embed.add_field(
name="Reference", name="Reference",
value=reward.reference or "No Reference information saved.", value=reward.reference or "No Reference information saved.",
inline=False inline=False,
) )
# Modnote field # Modnote field
embed.add_field( embed.add_field(
name="Notes", name="Notes", value=reward.modnote or "No notes added", inline=False
value=reward.modnote or "No notes added",
inline=False
) )
# Fulfilled field # Fulfilled field
if reward.fulfilled_at is not None: if reward.fulfilled_at is not None:
fluffed = f"Fluffed at {discord.utils.format_dt(reward.fulfilled_at, 'F')}" fluffed = f"Fluffed at {discord.utils.format_dt(reward.fulfilled_at, 'F')}"
if reward.fulfilled_note: if reward.fulfilled_note:
fluffed += '\n' + "Fluff Note: " + reward.fulfilled_note fluffed += "\n" + "Fluff Note: " + reward.fulfilled_note
else: else:
fluffed = "Not yet fluffed" fluffed = "Not yet fluffed"
embed.add_field( embed.add_field(name="Fulfilled", value=fluffed)
name="Fulfilled",
value=fluffed
)
embed.set_footer(text="Last Updated") embed.set_footer(text="Last Updated")
embed.timestamp = utc_now() embed.timestamp = utc_now()
@@ -173,7 +171,9 @@ class RewardCampaign:
class CampaignRegistry: class CampaignRegistry:
VERSION = CampaignData.VERSION 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 self.data = data
# TODO: Actually pass in a session # TODO: Actually pass in a session
self._session = aiohttp.ClientSession() self._session = aiohttp.ClientSession()
@@ -238,27 +238,18 @@ class CampaignRegistry:
return camp return camp
async def create_campaign( async def create_campaign(
self, self, cid: int, campaign_name: str, **kwargs
cid: int,
campaign_name: str,
**kwargs
) -> RewardCampaign: ) -> RewardCampaign:
""" """
Create a new campaign. Create a new campaign.
The name must be unique (ignoring case) to facilitate easy lookup. The name must be unique (ignoring case) to facilitate easy lookup.
""" """
row = await Campaign.create( row = await Campaign.create(
communityid=cid, communityid=cid, campaign_name=campaign_name, **kwargs
campaign_name=campaign_name,
**kwargs
) )
return RewardCampaign(row, session=self._session) return RewardCampaign(row, session=self._session)
async def update_campaign( async def update_campaign(self, campaign_id: int, **kwargs) -> RewardCampaign:
self,
campaign_id: int,
**kwargs
) -> RewardCampaign:
""" """
Update a campaign with the given data args. Update a campaign with the given data args.
+28 -5
View File
@@ -74,7 +74,7 @@ class CampaignComponent(cmds.Component):
# Add a reward to the database with the correct info. # Add a reward to the database with the correct info.
await campaign.add_reward( await campaign.add_reward(
profileid=event_row["profileid"], 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"], event_id=event_row["event_id"],
twitch_user_id=data["chatter_user_id"], twitch_user_id=data["chatter_user_id"],
twitch_user_name=data["chatter_user_name"], twitch_user_name=data["chatter_user_name"],
@@ -83,6 +83,31 @@ class CampaignComponent(cmds.Component):
# TODO: Webhook logging maybe.. (do this in campaign) # TODO: Webhook logging maybe.. (do this in campaign)
# Or just general logging. (also do this in campaign, but we can do again here) # 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() @cmds.Component.listener()
async def event_safe_event_chat_notice_resub(self, payload): 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 # 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 reward_cap = campaign.row.target_rewards
if reward_cap is not None: if reward_cap is not None:
response = ( response = "{name}: {given} rewards earned out of {cap}!"
f"{name}: {rewards_earned} rewards earned out of {reward_cap}!"
)
else: else:
response = f"{name}: {rewards_earned} rewards earned so far!" response = "{name}: {given} rewards earned so far!"
formatted = response.format( formatted = response.format(
name=campaign.row.campaign_name, name=campaign.row.campaign_name,
given=rewards_earned, given=rewards_earned,