generated from HoloTech/holotech-plugin-template
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0923f32a82 | |||
| 8fc0ff0667 | |||
| 9203a795e3 | |||
| ee70b80120 | |||
| a1dd04685b | |||
| d7447fc300 | |||
| bc231da61f |
@@ -7,7 +7,7 @@ DO $$
|
|||||||
$$ LANGUAGE plpgsql;
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
-- Plugin version history
|
-- Plugin version history
|
||||||
INSERT INTO version_history (component, from_version, to_version, author) VALUES ('PLUSCAMPAIGN', 0, 1, 'Initial Creation');
|
INSERT INTO version_history (component, from_version, to_version, author) VALUES ('REWARDCAMPAIGN', 0, 1, 'Initial Creation');
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE campaigns(
|
CREATE TABLE campaigns(
|
||||||
@@ -15,6 +15,8 @@ CREATE TABLE campaigns(
|
|||||||
communityid INTEGER NOT NULL REFERENCES communities(communityid) ON DELETE CASCADE ON UPDATE CASCADE,
|
communityid INTEGER NOT NULL REFERENCES communities(communityid) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||||
target_rewards INTEGER,
|
target_rewards INTEGER,
|
||||||
campaign_name TEXT NOT NULL,
|
campaign_name TEXT NOT NULL,
|
||||||
|
moderator_role_id BIGINT,
|
||||||
|
logging_webhook_url TEXT,
|
||||||
started_at TIMESTAMPTZ,
|
started_at TIMESTAMPTZ,
|
||||||
completed_at TIMESTAMPTZ,
|
completed_at TIMESTAMPTZ,
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
@@ -33,6 +35,8 @@ CREATE TABLE campaign_rewards_earned(
|
|||||||
fulfilled_at TIMESTAMPTZ,
|
fulfilled_at TIMESTAMPTZ,
|
||||||
fulfilled_note TEXT,
|
fulfilled_note TEXT,
|
||||||
modnote TEXT,
|
modnote TEXT,
|
||||||
|
reference TEXT,
|
||||||
|
logged_messageid BIGINT,
|
||||||
earned_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
earned_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
earned_reason TEXT NOT NULL,
|
earned_reason TEXT NOT NULL,
|
||||||
_timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
_timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
|
|||||||
+121
-3
@@ -6,22 +6,38 @@ from data import ORDER, Condition
|
|||||||
from data.conditions import NULL, condition
|
from data.conditions import NULL, condition
|
||||||
from utils.lib import utc_now
|
from utils.lib import utc_now
|
||||||
|
|
||||||
from .lib import LOWER, asexpr
|
from .lib import LOWER, asexpr, ThreadedWebhook
|
||||||
from .data import (
|
from .data import (
|
||||||
CampaignData,
|
CampaignData,
|
||||||
Campaign,
|
Campaign,
|
||||||
EarnedReward,
|
EarnedReward,
|
||||||
)
|
)
|
||||||
|
from . import logger
|
||||||
|
|
||||||
|
|
||||||
class RewardCampaign:
|
class RewardCampaign:
|
||||||
def __init__(self, row: Campaign):
|
def __init__(self, row: Campaign):
|
||||||
self.row = row
|
self.row = row
|
||||||
|
|
||||||
|
self._webhook: ThreadedWebhook | None = None
|
||||||
|
self._cached_webhookurl: str | None = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_active(self):
|
def is_active(self):
|
||||||
return self.row.started_at is not None and self.row.completed_at is None
|
return self.row.started_at is not None and self.row.completed_at is None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def webhook(self):
|
||||||
|
if self._cached_webhookurl != self.row.logging_webhook_url:
|
||||||
|
url = self._cached_webhookurl = self.row.logging_webhook_url
|
||||||
|
if url is not None:
|
||||||
|
# TODO: I don't know if these needs a client
|
||||||
|
# Might be hard if so given we need to run this from Twitch client as well
|
||||||
|
self._webhook = ThreadedWebhook.from_url(url)
|
||||||
|
else:
|
||||||
|
self._webhook = None
|
||||||
|
return self._webhook
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
"""Start the reward campaign."""
|
"""Start the reward campaign."""
|
||||||
if self.row.started_at is not None:
|
if self.row.started_at is not None:
|
||||||
@@ -53,15 +69,103 @@ class RewardCampaign:
|
|||||||
twitch_user_name=twitch_user_name,
|
twitch_user_name=twitch_user_name,
|
||||||
earned_at=earned_at or utc_now(),
|
earned_at=earned_at or utc_now(),
|
||||||
)
|
)
|
||||||
|
await self.try_to_log_reward(row)
|
||||||
|
|
||||||
return row
|
return row
|
||||||
|
|
||||||
|
async def update_reward(self, rewardid: int, **kwargs):
|
||||||
|
# In particular, log or update the logged message
|
||||||
|
if kwargs:
|
||||||
|
reward = await EarnedReward.fetch(rewardid)
|
||||||
|
if reward is None:
|
||||||
|
raise ValueError("Reward doesn't exist")
|
||||||
|
await reward.update(**kwargs)
|
||||||
|
await self.try_to_log_reward(reward)
|
||||||
|
|
||||||
async def get_rewards(self) -> list[EarnedReward]:
|
async def get_rewards(self) -> list[EarnedReward]:
|
||||||
rows = await EarnedReward.fetch_where(
|
rows = await EarnedReward.fetch_where(
|
||||||
campaign_id=self.row.campaign_id
|
campaign_id=self.row.campaign_id
|
||||||
).order_by("earned_at", direction=ORDER.ASC)
|
).order_by("earned_at", direction=ORDER.ASC)
|
||||||
return rows
|
return rows
|
||||||
|
|
||||||
|
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:
|
||||||
|
# Try and edit message
|
||||||
|
try:
|
||||||
|
await self.webhook.edit_message(reward.log_messageid, embed=embed)
|
||||||
|
except discord.HTTPException:
|
||||||
|
await reward.update(log_messageid=None)
|
||||||
|
if not reward.log_messageid:
|
||||||
|
try:
|
||||||
|
message = await self.webhook.send(embed=embed, wait=True)
|
||||||
|
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)
|
||||||
|
|
||||||
|
async def _log_format_reward(self, reward: EarnedReward):
|
||||||
|
"""
|
||||||
|
Quick and ugly embed format for the webhook.
|
||||||
|
"""
|
||||||
|
import discord
|
||||||
|
|
||||||
|
embed = discord.Embed(
|
||||||
|
title=f"Reward #{reward.earned_id} in {self.row.campaign_name}",
|
||||||
|
)
|
||||||
|
# User Field
|
||||||
|
embed.add_field(
|
||||||
|
name="User Information",
|
||||||
|
value=(
|
||||||
|
f"Twitch user `{reward.twitch_user_name or 'Unknown'}` `(ID: {reward.twitch_user_id or 'Unknown'})`.\n"
|
||||||
|
f"Internal ID `{reward.profileid}`"
|
||||||
|
),
|
||||||
|
inline=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# Earning field
|
||||||
|
embed.add_field(
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
# Reference field
|
||||||
|
embed.add_field(
|
||||||
|
name="Reference",
|
||||||
|
value=reward.reference or "No Reference information saved.",
|
||||||
|
inline=False
|
||||||
|
)
|
||||||
|
# Modnote field
|
||||||
|
embed.add_field(
|
||||||
|
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
|
||||||
|
else:
|
||||||
|
fluffed = "Not yet fluffed"
|
||||||
|
|
||||||
|
embed.add_field(
|
||||||
|
name="Fulfilled",
|
||||||
|
value=fluffed
|
||||||
|
)
|
||||||
|
|
||||||
|
embed.set_footer(text="Last Updated")
|
||||||
|
embed.timestamp = utc_now()
|
||||||
|
|
||||||
|
return embed
|
||||||
|
|
||||||
|
|
||||||
class CampaignRegistry:
|
class CampaignRegistry:
|
||||||
VERSION = CampaignData.VERSION
|
VERSION = CampaignData.VERSION
|
||||||
@@ -132,7 +236,7 @@ class CampaignRegistry:
|
|||||||
self,
|
self,
|
||||||
cid: int,
|
cid: int,
|
||||||
campaign_name: str,
|
campaign_name: str,
|
||||||
target_rewards: Optional[int] = None,
|
**kwargs
|
||||||
) -> RewardCampaign:
|
) -> RewardCampaign:
|
||||||
"""
|
"""
|
||||||
Create a new campaign.
|
Create a new campaign.
|
||||||
@@ -140,7 +244,21 @@ class CampaignRegistry:
|
|||||||
"""
|
"""
|
||||||
row = await Campaign.create(
|
row = await Campaign.create(
|
||||||
communityid=cid,
|
communityid=cid,
|
||||||
target_rewards=target_rewards,
|
|
||||||
campaign_name=campaign_name,
|
campaign_name=campaign_name,
|
||||||
|
**kwargs
|
||||||
)
|
)
|
||||||
return RewardCampaign(row)
|
return RewardCampaign(row)
|
||||||
|
|
||||||
|
async def update_campaign(
|
||||||
|
self,
|
||||||
|
campaign_id: int,
|
||||||
|
**kwargs
|
||||||
|
) -> RewardCampaign:
|
||||||
|
"""
|
||||||
|
Update a campaign with the given data args.
|
||||||
|
|
||||||
|
Registry handles this for caching and dispatch reasons.
|
||||||
|
"""
|
||||||
|
campaign = await Campaign.fetch(campaign_id)
|
||||||
|
await campaign.update(**kwargs)
|
||||||
|
return RewardCampaign(campaign)
|
||||||
|
|||||||
+1
-1
@@ -54,7 +54,7 @@ async def prepare_campaign(campaign: RewardCampaign) -> CampaignPayload:
|
|||||||
|
|
||||||
|
|
||||||
class CampaignChannel(Channel):
|
class CampaignChannel(Channel):
|
||||||
name = "PlusCampaign"
|
name = "CampaignRewards"
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, profiler: ProfilesRegistry, campaigns: CampaignRegistry, **kwargs
|
self, profiler: ProfilesRegistry, campaigns: CampaignRegistry, **kwargs
|
||||||
|
|||||||
+7
-1
@@ -13,6 +13,9 @@ class Campaign(RowModel):
|
|||||||
started_at = Timestamp()
|
started_at = Timestamp()
|
||||||
completed_at = Timestamp()
|
completed_at = Timestamp()
|
||||||
|
|
||||||
|
moderator_role_id = Integer()
|
||||||
|
logging_webhook_url = String()
|
||||||
|
|
||||||
created_at = Timestamp()
|
created_at = Timestamp()
|
||||||
_timestamp = Timestamp()
|
_timestamp = Timestamp()
|
||||||
|
|
||||||
@@ -34,13 +37,16 @@ class EarnedReward(RowModel):
|
|||||||
earned_at = Timestamp()
|
earned_at = Timestamp()
|
||||||
earned_from = String()
|
earned_from = String()
|
||||||
modnote = String()
|
modnote = String()
|
||||||
|
reference = String()
|
||||||
|
|
||||||
|
log_messageid = Integer()
|
||||||
|
|
||||||
_timestamp = Timestamp()
|
_timestamp = Timestamp()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class CampaignData(Registry):
|
class CampaignData(Registry):
|
||||||
VERSION = ("CAMPAIGN", 1)
|
VERSION = ("REWARDCAMPAIGN", 1)
|
||||||
|
|
||||||
Campaign = Campaign
|
Campaign = Campaign
|
||||||
campaigns = Campaign.table
|
campaigns = Campaign.table
|
||||||
|
|||||||
+445
-3
@@ -3,14 +3,17 @@ import asyncio
|
|||||||
|
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands as cmds
|
from discord.ext import commands as cmds
|
||||||
from discord import app_commands as appcmds
|
from discord import Forbidden, User, app_commands as appcmds
|
||||||
|
|
||||||
from meta import LionBot, LionCog, LionContext
|
from meta import LionBot, LionCog, LionContext
|
||||||
|
from meta.errors import SafeCancellation, UserInputError
|
||||||
from meta.logger import log_wrap
|
from meta.logger import log_wrap
|
||||||
from utils.lib import utc_now
|
from utils.lib import utc_now
|
||||||
|
|
||||||
from ..data import CampaignData
|
from ..data import CampaignData, EarnedReward
|
||||||
from ..campaigns import CampaignRegistry
|
from ..campaign import CampaignRegistry, RewardCampaign
|
||||||
|
from ..lib import ThreadedWebhook
|
||||||
|
from .ui import RewardList, RewardEditor, CampaignDashboard
|
||||||
|
|
||||||
|
|
||||||
class CampaignCog(LionCog):
|
class CampaignCog(LionCog):
|
||||||
@@ -24,3 +27,442 @@ class CampaignCog(LionCog):
|
|||||||
await self.data.init()
|
await self.data.init()
|
||||||
await self.bot.version_check(*self.data.VERSION)
|
await self.bot.version_check(*self.data.VERSION)
|
||||||
await self.campaigns.init()
|
await self.campaigns.init()
|
||||||
|
|
||||||
|
async def resolve_campaign(
|
||||||
|
self, cid: int, campaign_name: str | None
|
||||||
|
) -> RewardCampaign:
|
||||||
|
campaign = None
|
||||||
|
if campaign_name is not None:
|
||||||
|
campaign = await self.campaigns.fetch_campaign_by_name(cid, campaign_name)
|
||||||
|
if campaign is None:
|
||||||
|
raise UserInputError(
|
||||||
|
f"Sorry, no campaign found named '{campaign_name}'"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# Find active campaign
|
||||||
|
active = await self.campaigns.fetch_campaigns(cid, active=True)
|
||||||
|
if len(active) > 1:
|
||||||
|
names = ", ".join(camp.row.campaign_name for camp in active)
|
||||||
|
raise UserInputError(f"Multiple active campaigns running: {names}")
|
||||||
|
elif not active:
|
||||||
|
raise UserInputError("No active campaigns running")
|
||||||
|
else:
|
||||||
|
campaign = active[0]
|
||||||
|
return campaign
|
||||||
|
|
||||||
|
async def campaign_modcheck(self, campaign: RewardCampaign, member: discord.Member):
|
||||||
|
if member.guild_permissions.administrator:
|
||||||
|
return True
|
||||||
|
if campaign.row.moderator_role_id and campaign.row.moderator_role_id in [
|
||||||
|
r.id for r in member.roles
|
||||||
|
]:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def _campaign_acmpl(
|
||||||
|
self, interaction: discord.Interaction, partial: str
|
||||||
|
) -> list[appcmds.Choice]:
|
||||||
|
"""
|
||||||
|
Generate a list of campaigns, with active campaigns listed first.
|
||||||
|
|
||||||
|
Campaign values are their name.
|
||||||
|
"""
|
||||||
|
if not interaction.guild:
|
||||||
|
return []
|
||||||
|
community = await self.bot.profiles.fetch_community(
|
||||||
|
interaction.guild, interaction=interaction
|
||||||
|
)
|
||||||
|
cid = community.communityid
|
||||||
|
|
||||||
|
campaigns = await self.campaigns.fetch_campaigns(cid, active=None)
|
||||||
|
sorted_campaigns = sorted(
|
||||||
|
campaigns,
|
||||||
|
key=lambda camp: (camp.is_active, camp.row.started_at, camp.row.created_at),
|
||||||
|
)
|
||||||
|
names = [
|
||||||
|
camp.row.campaign_name
|
||||||
|
for camp in sorted_campaigns
|
||||||
|
if partial.lower() in camp.row.campaign_name.lower()
|
||||||
|
]
|
||||||
|
|
||||||
|
choices = [appcmds.Choice(name=name[:100], value=name) for name in names]
|
||||||
|
return choices
|
||||||
|
|
||||||
|
@cmds.hybrid_group(
|
||||||
|
name="campaign", description="Command group for administering reward campaigns"
|
||||||
|
)
|
||||||
|
@appcmds.guild_only()
|
||||||
|
@appcmds.default_permissions(manage_guild=True)
|
||||||
|
async def campaign_group(self, ctx: LionContext):
|
||||||
|
"""
|
||||||
|
As a base command group this never gets executed.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@campaign_group.command(
|
||||||
|
name="dashboard",
|
||||||
|
description="Show summary dashboard for active or selected campaign.",
|
||||||
|
)
|
||||||
|
@appcmds.describe(campaign_name="Name of the campaign to display")
|
||||||
|
@appcmds.rename(campaign_name="campaign")
|
||||||
|
async def campaign_dashboard_cmd(
|
||||||
|
self, ctx: LionContext, campaign_name: Optional[str] = None
|
||||||
|
):
|
||||||
|
if not ctx.guild:
|
||||||
|
return
|
||||||
|
if not ctx.interaction:
|
||||||
|
return
|
||||||
|
|
||||||
|
campaign = await self.resolve_campaign(ctx.community.communityid, campaign_name)
|
||||||
|
if not await self.campaign_modcheck(campaign, ctx.author):
|
||||||
|
await ctx.interaction.response.send_message(
|
||||||
|
"You need to be an administrator or have the configured modrole to use that!",
|
||||||
|
ephemeral=True,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Open campaign UI
|
||||||
|
widget = CampaignDashboard(bot=self.bot, campaign=campaign, callerid=ctx.author.id)
|
||||||
|
await widget.run(ctx.interaction)
|
||||||
|
await widget.wait()
|
||||||
|
|
||||||
|
campaign_dashboard_cmd.autocomplete("campaign_name")(_campaign_acmpl)
|
||||||
|
|
||||||
|
@campaign_group.command(name="start", description="Setup and start a campaign")
|
||||||
|
@appcmds.describe(
|
||||||
|
campaign_name="Name of the campaign to create. Must be unique.",
|
||||||
|
rewards_cap="Optional maximum number of rewards to give.",
|
||||||
|
moderator_role="Optional discord role to allow to edit and moderate the campaign.",
|
||||||
|
logging_webhook="Optional discord webhook URL to log earned rewards to.",
|
||||||
|
)
|
||||||
|
async def campaign_start_cmd(
|
||||||
|
self,
|
||||||
|
ctx: LionContext,
|
||||||
|
campaign_name: str,
|
||||||
|
rewards_cap: Optional[int] = None,
|
||||||
|
moderator_role: Optional[discord.Role] = None,
|
||||||
|
logging_webhook: Optional[str] = None,
|
||||||
|
):
|
||||||
|
if not ctx.guild:
|
||||||
|
return
|
||||||
|
if not ctx.interaction:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Permission check
|
||||||
|
if not ctx.author.guild_permissions.administrator:
|
||||||
|
await ctx.interaction.response.send_message(
|
||||||
|
"You need to be an administrator to start a campaign!", ephemeral=True
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Check name clash
|
||||||
|
existing = await self.campaigns.fetch_campaign_by_name(
|
||||||
|
ctx.community.communityid, campaign_name
|
||||||
|
)
|
||||||
|
if existing:
|
||||||
|
await ctx.reply("A campaign with that name already exists!")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Check active campaign
|
||||||
|
campaigns = await self.campaigns.fetch_campaigns(
|
||||||
|
ctx.community.communityid, active=True
|
||||||
|
)
|
||||||
|
if campaigns:
|
||||||
|
existing_name = campaigns[0].row.campaign_name
|
||||||
|
await ctx.reply(
|
||||||
|
f"Your campaign '{existing_name}' is already active! "
|
||||||
|
"Sorry, we don't yet support multiple active campaigns, "
|
||||||
|
"please use '/campaign finish' to end your current campaign before starting a new one."
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# If logging webhook is given, check that it works
|
||||||
|
if logging_webhook is not None:
|
||||||
|
webhook = ThreadedWebhook.from_url(logging_webhook)
|
||||||
|
try:
|
||||||
|
await webhook.test_webhook()
|
||||||
|
except (discord.HTTPException, discord.Forbidden):
|
||||||
|
await ctx.reply("Couldn't post to the logging webhook provided!")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Passed all the checks, now we can create
|
||||||
|
campaign = await self.campaigns.create_campaign(
|
||||||
|
cid=ctx.community.communityid,
|
||||||
|
campaign_name=campaign_name,
|
||||||
|
target_rewards=rewards_cap,
|
||||||
|
moderator_role_id=moderator_role.id if moderator_role else None,
|
||||||
|
logging_webhook_url=logging_webhook,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Ack creation
|
||||||
|
# TODO: Can also show dashboard
|
||||||
|
await ctx.reply(
|
||||||
|
f"Setup and started your reward campaign {campaign.row.campaign_name}! Good luck"
|
||||||
|
)
|
||||||
|
# Open campaign UI
|
||||||
|
widget = CampaignDashboard(bot=self.bot, campaign=campaign, callerid=ctx.author.id)
|
||||||
|
await widget.run(ctx.interaction)
|
||||||
|
await widget.wait()
|
||||||
|
|
||||||
|
@campaign_group.command(name="configure", description="Update campaign settings")
|
||||||
|
@appcmds.describe(
|
||||||
|
campaign_name="Name of the campaign you want to update.",
|
||||||
|
new_name="New name for the campaign. Must be unique.",
|
||||||
|
new_cap="New cap for the number of rewards. Use 0 or negative for no cap.",
|
||||||
|
moderator_role="Optional discord role to allow to edit and moderate the campaign.",
|
||||||
|
logging_webhook="Optional discord webhook URL to log earned rewards to.",
|
||||||
|
)
|
||||||
|
@appcmds.rename(campaign_name="campaign")
|
||||||
|
async def campaign_configure_cmd(
|
||||||
|
self,
|
||||||
|
ctx: LionContext,
|
||||||
|
campaign_name: Optional[str] = None,
|
||||||
|
new_name: Optional[str] = None,
|
||||||
|
new_cap: Optional[int] = None,
|
||||||
|
moderator_role: Optional[discord.Role] = None,
|
||||||
|
logging_webhook: Optional[str] = None,
|
||||||
|
):
|
||||||
|
if not ctx.guild:
|
||||||
|
return
|
||||||
|
if not ctx.interaction:
|
||||||
|
return
|
||||||
|
|
||||||
|
campaign = await self.resolve_campaign(ctx.community.communityid, campaign_name)
|
||||||
|
if not await self.campaign_modcheck(campaign, ctx.author):
|
||||||
|
await ctx.interaction.response.send_message(
|
||||||
|
"You need to be an administrator or have the configured modrole to use that!",
|
||||||
|
ephemeral=True,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Verify each of the given settings to update
|
||||||
|
update_args = {}
|
||||||
|
|
||||||
|
# Check name clash
|
||||||
|
if (
|
||||||
|
new_name is not None
|
||||||
|
and new_name.lower() != campaign.row.campaign_name.lower()
|
||||||
|
):
|
||||||
|
existing = await self.campaigns.fetch_campaign_by_name(
|
||||||
|
ctx.community.communityid, campaign_name
|
||||||
|
)
|
||||||
|
if existing:
|
||||||
|
await ctx.reply("A campaign with that name already exists!")
|
||||||
|
return
|
||||||
|
update_args["campaign_name"] = new_name
|
||||||
|
|
||||||
|
if new_cap is not None:
|
||||||
|
update_args["total_rewards"] = new_cap if new_cap > 0 else None
|
||||||
|
|
||||||
|
# If logging webhook is given, check that it works
|
||||||
|
if logging_webhook is not None:
|
||||||
|
webhook = ThreadedWebhook.from_url(logging_webhook)
|
||||||
|
try:
|
||||||
|
await webhook.test_webhook()
|
||||||
|
except (discord.HTTPException, discord.Forbidden):
|
||||||
|
await ctx.reply("Couldn't post to the logging webhook provided!")
|
||||||
|
return
|
||||||
|
update_args["logging_webhook_url"] = logging_webhook
|
||||||
|
|
||||||
|
if moderator_role is not None:
|
||||||
|
# TODO: Currently no way to actually unset this
|
||||||
|
update_args["moderator_role_id"] = moderator_role.id
|
||||||
|
|
||||||
|
# Passed all the checks, now we can update
|
||||||
|
|
||||||
|
campaign = await self.campaigns.update_campaign(
|
||||||
|
campaign.row.campaign_id,
|
||||||
|
**update_args,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Ack creation
|
||||||
|
# TODO: Can also show dashboard
|
||||||
|
await ctx.reply(
|
||||||
|
f"Setup and started your reward campaign {campaign.row.campaign_name}! Good luck"
|
||||||
|
)
|
||||||
|
|
||||||
|
campaign_configure_cmd.autocomplete("campaign_name")(_campaign_acmpl)
|
||||||
|
|
||||||
|
@campaign_group.command(
|
||||||
|
name="rewards",
|
||||||
|
description="View and edit the rewards earned so far this campaign",
|
||||||
|
)
|
||||||
|
@appcmds.describe(campaign_name="Name of the campaign to display rewards for")
|
||||||
|
@appcmds.rename(campaign_name="campaign")
|
||||||
|
async def campaign_rewards_cmd(
|
||||||
|
self,
|
||||||
|
ctx: LionContext,
|
||||||
|
campaign_name: Optional[str] = None,
|
||||||
|
):
|
||||||
|
if not ctx.guild:
|
||||||
|
return
|
||||||
|
if not ctx.interaction:
|
||||||
|
return
|
||||||
|
|
||||||
|
campaign = await self.resolve_campaign(ctx.community.communityid, campaign_name)
|
||||||
|
if not await self.campaign_modcheck(campaign, ctx.author):
|
||||||
|
await ctx.interaction.response.send_message(
|
||||||
|
"You need to be an administrator or have the configured modrole to use that!",
|
||||||
|
ephemeral=True,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Show rewardlist widget
|
||||||
|
widget = RewardList(bot=self.bot, campaign=campaign, callerid=ctx.author.id)
|
||||||
|
await widget.run(ctx.interaction)
|
||||||
|
await widget.wait()
|
||||||
|
|
||||||
|
campaign_rewards_cmd.autocomplete("campaign_name")(_campaign_acmpl)
|
||||||
|
|
||||||
|
@campaign_group.command(
|
||||||
|
name="editreward",
|
||||||
|
description="Add or edit details for a given reward in a campaign (see also /campaign rewards)",
|
||||||
|
)
|
||||||
|
@appcmds.describe(
|
||||||
|
rewardid="Earned reward to edit",
|
||||||
|
)
|
||||||
|
@appcmds.rename(rewardid="reward")
|
||||||
|
async def campaign_editreward_cmd(self, ctx: LionContext, rewardid: str):
|
||||||
|
# For this we'll just open the reward editor
|
||||||
|
# Will need to identify the reward with autocomplete. Can use the reward id directly..
|
||||||
|
if not rewardid.isdigit():
|
||||||
|
raise UserInputError(
|
||||||
|
"Please enter the reward number or select a reward from the argument menu"
|
||||||
|
)
|
||||||
|
|
||||||
|
reward = await EarnedReward.fetch(int(rewardid))
|
||||||
|
if not reward:
|
||||||
|
raise UserInputError(
|
||||||
|
"Reward not found. Please enter the reward number or select a reward from the argument menu"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Fetch campaign and then do a perm check
|
||||||
|
campaign = await self.campaigns.fetch_campaign(reward.campaign_id)
|
||||||
|
if campaign is None:
|
||||||
|
raise SafeCancellation("Something went wrong.. please try again soon.")
|
||||||
|
if campaign.row.communityid != ctx.community.communityid:
|
||||||
|
raise UserInputError("This reward doesn't belong to this community!")
|
||||||
|
|
||||||
|
if not await self.campaign_modcheck(campaign, ctx.author):
|
||||||
|
await ctx.interaction.response.send_message(
|
||||||
|
"You need to be an administrator or have the configured modrole to use that!",
|
||||||
|
ephemeral=True,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Okay, this is our reward, and author has permission to modify it. Spin the modal.
|
||||||
|
modal = RewardEditor.from_reward(reward)
|
||||||
|
|
||||||
|
currently_fluffed = reward.fulfilled_at is not None
|
||||||
|
|
||||||
|
@modal.submit_callback()
|
||||||
|
async def on_editor_submit(interaction: discord.Interaction):
|
||||||
|
update_args = {}
|
||||||
|
|
||||||
|
if modal.flufbox.component.value and not currently_fluffed:
|
||||||
|
# Reward has been fluffed
|
||||||
|
update_args["fulfilled_at"] = utc_now()
|
||||||
|
elif currently_fluffed and not modal.flufbox.component.value:
|
||||||
|
# Reward has been unfluffed
|
||||||
|
update_args["fulfilled_at"] = None
|
||||||
|
|
||||||
|
new_ref_value = modal.reference.value or None
|
||||||
|
if new_ref_value != reward.reference:
|
||||||
|
update_args["reference"] = new_ref_value
|
||||||
|
|
||||||
|
new_notes_value = modal.notes.value or None
|
||||||
|
if new_notes_value != reward.modnote:
|
||||||
|
update_args["modnote"] = new_notes_value
|
||||||
|
|
||||||
|
if update_args:
|
||||||
|
await interaction.response.defer(thinking=True, ephemeral=True)
|
||||||
|
await campaign.update_reward(reward.earned_id, **update_args)
|
||||||
|
await interaction.followup.send(
|
||||||
|
content="Reward updated!", ephemeral=True
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
await interaction.response.defer(thinking=False)
|
||||||
|
|
||||||
|
def _reward_acmpl_format(
|
||||||
|
self, campaign: RewardCampaign, reward: EarnedReward
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Format an EarnedReward for completion.
|
||||||
|
|
||||||
|
#100: username in campaign
|
||||||
|
"""
|
||||||
|
...
|
||||||
|
|
||||||
|
@campaign_editreward_cmd.autocomplete("rewardid")
|
||||||
|
async def _reward_acmpl(
|
||||||
|
self, interaction: discord.Interaction, partial: str
|
||||||
|
) -> list[appcmds.Choice]:
|
||||||
|
"""
|
||||||
|
Display list of rewards.
|
||||||
|
|
||||||
|
If possible, use the active campaign.
|
||||||
|
If no matching choices, search all campaigns of the community.
|
||||||
|
|
||||||
|
Values are the earned_id as a string
|
||||||
|
"""
|
||||||
|
if not interaction.guild:
|
||||||
|
return []
|
||||||
|
community = await self.bot.profiles.fetch_community(
|
||||||
|
interaction.guild, interaction=interaction
|
||||||
|
)
|
||||||
|
cid = community.communityid
|
||||||
|
|
||||||
|
choices = []
|
||||||
|
|
||||||
|
campaigns = await self.campaigns.fetch_campaigns(cid, active=None)
|
||||||
|
sorted_campaigns = sorted(
|
||||||
|
campaigns,
|
||||||
|
key=lambda camp: (camp.is_active, camp.row.started_at, camp.row.created_at),
|
||||||
|
)
|
||||||
|
for campaign in sorted_campaigns:
|
||||||
|
rewards = await campaign.get_rewards()
|
||||||
|
for reward in reversed(rewards):
|
||||||
|
formatted = self._reward_acmpl_format(campaign, reward)
|
||||||
|
if partial in formatted:
|
||||||
|
choices.append(
|
||||||
|
appcmds.Choice(name=formatted, value=str(reward.earned_id))
|
||||||
|
)
|
||||||
|
if len(choices) >= 20:
|
||||||
|
break
|
||||||
|
if len(choices) >= 20:
|
||||||
|
break
|
||||||
|
|
||||||
|
return choices[:25]
|
||||||
|
|
||||||
|
# @campaign_group.command(
|
||||||
|
# name="reward",
|
||||||
|
# description="Add a reward to a given user"
|
||||||
|
# )
|
||||||
|
# async def campaign_reward_cmd(self, ctx: LionContext, twitch_username: str, reward_reason: str, campaign_name: Optional[str] = None):
|
||||||
|
# # TODO: This does
|
||||||
|
# ...
|
||||||
|
|
||||||
|
# @campaign_group.command(
|
||||||
|
# name="start", description="Add or edit details for a given reward in a campaign"
|
||||||
|
# )
|
||||||
|
# async def campaign_rewardnote_cmd(self, ctx: LionContext): ...
|
||||||
|
#
|
||||||
|
# @campaign_group.command(name="finish", description="Close a campaign")
|
||||||
|
# @appcmds.describe(campaign_name="Name of the campaign to mark as complete")
|
||||||
|
# @appcmds.rename(campaign_name='campaign')
|
||||||
|
# async def campaign_finish_cmd(
|
||||||
|
# self, ctx: LionContext, campaign_name: Optional[str] = None
|
||||||
|
# ):
|
||||||
|
# if not ctx.guild:
|
||||||
|
# return
|
||||||
|
# if not ctx.interaction:
|
||||||
|
# return
|
||||||
|
#
|
||||||
|
# campaign = await self.resolve_campaign(ctx.community.communityid, campaign_name)
|
||||||
|
# if not await self.campaign_modcheck(campaign, ctx.author):
|
||||||
|
# await ctx.interaction.response.send_message(
|
||||||
|
# "You need to be an administrator or have the configured modrole to use that!",
|
||||||
|
# ephemeral=True
|
||||||
|
# )
|
||||||
|
# return
|
||||||
|
# campaign_finish_cmd.autocomplete('campaign_name')(_campaign_acmpl)
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
from .rewards import RewardList, RewardEditor
|
||||||
|
from .campaign import CampaignDashboard
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
"""
|
||||||
|
Defines widgets for displaying a campaign
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Optional, TYPE_CHECKING
|
||||||
|
import asyncio
|
||||||
|
import datetime as dt
|
||||||
|
|
||||||
|
import discord
|
||||||
|
from discord.ui import Modal
|
||||||
|
from discord.ui.select import select, Select, SelectOption
|
||||||
|
from discord.ui.button import button, Button, ButtonStyle
|
||||||
|
from discord.ui.text_input import TextInput, TextStyle
|
||||||
|
|
||||||
|
from meta import LionBot
|
||||||
|
from meta.errors import UserInputError
|
||||||
|
from meta.config import conf
|
||||||
|
from utils.lib import tabulate, utc_now, MessageArgs, parse_duration
|
||||||
|
from utils.ui import MessageUI, AButton, AsComponents, ConfigEditor
|
||||||
|
from utils.ui.micros import FastModal
|
||||||
|
from utils.ui.pagers import BasePager, Pager
|
||||||
|
|
||||||
|
from ...campaign import RewardCampaign
|
||||||
|
from ...data import EarnedReward
|
||||||
|
from .. import logger
|
||||||
|
|
||||||
|
from .rewards import RewardList
|
||||||
|
|
||||||
|
|
||||||
|
class CampaignDashboard(MessageUI):
|
||||||
|
def __init__(self, bot: LionBot, campaign: RewardCampaign, **kwargs):
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
self.bot = bot
|
||||||
|
self.campaign = campaign
|
||||||
|
|
||||||
|
# UI state
|
||||||
|
|
||||||
|
# ------ UI API -----
|
||||||
|
|
||||||
|
# ------ UI Components -----
|
||||||
|
# Button to open rewards list
|
||||||
|
|
||||||
|
@button(label="Rewards Earned")
|
||||||
|
async def rewards_list_button(self, press: discord.Interaction, pressed: Button):
|
||||||
|
await press.response.defer()
|
||||||
|
widget = RewardList(
|
||||||
|
bot=self.bot,
|
||||||
|
campaign=self.campaign,
|
||||||
|
callerid=self._callerid,
|
||||||
|
)
|
||||||
|
self._slaves.append(widget)
|
||||||
|
await widget.run(press)
|
||||||
|
await widget.wait()
|
||||||
|
self._slaves.remove(widget)
|
||||||
|
|
||||||
|
@button(emoji=conf.emojis.cancel, style=ButtonStyle.red)
|
||||||
|
async def quit_button(self, press: discord.Interaction, pressed: Button):
|
||||||
|
"""Close the UI and all children."""
|
||||||
|
await press.response.defer(thinking=False)
|
||||||
|
await self.quit()
|
||||||
|
|
||||||
|
@button(emoji=conf.emojis.refresh)
|
||||||
|
async def refresh_button(self, press: discord.Interaction, pressed: Button):
|
||||||
|
await press.response.defer()
|
||||||
|
await self.refresh()
|
||||||
|
|
||||||
|
# ------ UI Flow -----
|
||||||
|
|
||||||
|
async def refresh_layout(self):
|
||||||
|
# Nothing to refresh
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def make_message(self) -> MessageArgs:
|
||||||
|
embed = discord.Embed(
|
||||||
|
title=f"{self.campaign.row.campaign_name} Campaign Dashboard"
|
||||||
|
)
|
||||||
|
# embed.set_footer with last update
|
||||||
|
|
||||||
|
# Don't show the webhook url since it contains secrets, just show whether it is set
|
||||||
|
# Hard-coded channel/timer URL for now
|
||||||
|
campaign = self.campaign
|
||||||
|
started_at = (
|
||||||
|
discord.utils.format_dt(campaign.row.started_at, "F")
|
||||||
|
if campaign.row.started_at
|
||||||
|
else "*Not Started*"
|
||||||
|
)
|
||||||
|
|
||||||
|
all_rewards = await campaign.get_rewards()
|
||||||
|
rewards_earned = len(all_rewards)
|
||||||
|
reward_cap = campaign.row.target_rewards
|
||||||
|
if reward_cap is not None:
|
||||||
|
rewards = f"{rewards_earned} out of {reward_cap}"
|
||||||
|
else:
|
||||||
|
rewards = f"{rewards_earned}"
|
||||||
|
|
||||||
|
description = (
|
||||||
|
f"Campaign running since {started_at} with {rewards} rewards given."
|
||||||
|
)
|
||||||
|
|
||||||
|
table = {
|
||||||
|
"Created at": discord.utils.format_dt(campaign.row.created_at, "F"),
|
||||||
|
"Started at": started_at,
|
||||||
|
"Moderator Role": f"<&@{campaign.row.moderator_role_id}>" if campaign.row.moderator_role_id else "*Not Set*",
|
||||||
|
"Logging webhook": "*Set Up*" if campaign.row.logging_webhook_url else "*Not Set*",
|
||||||
|
"Rewards Earned": str(rewards_earned),
|
||||||
|
"Rewards Cap": str(reward_cap),
|
||||||
|
}
|
||||||
|
if campaign.row.completed_at is not None:
|
||||||
|
table['Finished at'] = discord.utils.format_dt(campaign.row.completed_at, "F")
|
||||||
|
prop_table = '\n'.join(tabulate(*table.items()))
|
||||||
|
|
||||||
|
embed.description = f"{description}\n\n{prop_table}"
|
||||||
|
|
||||||
|
# Brief summary of rewards in columns. 12 per column. Empty titles?
|
||||||
|
rewardrows = []
|
||||||
|
for reward in all_rewards:
|
||||||
|
name = reward.twitch_user_name or str(reward.twitch_user_id) or str(reward.profileid)
|
||||||
|
fluffed = reward.fulfilled_at is not None
|
||||||
|
fluffed_emoji = '✅' if fluffed else '🔳'
|
||||||
|
rewardrows.append(f"{fluffed_emoji} {name}")
|
||||||
|
|
||||||
|
blocks = ['\n'.join(rewardrows[i:i+12]) for i in range(0, len(rewardrows), 12)]
|
||||||
|
|
||||||
|
embed.add_field(
|
||||||
|
name="Rewards Summary",
|
||||||
|
value=blocks[0] or 'No Rewards Earned',
|
||||||
|
inline=True
|
||||||
|
)
|
||||||
|
for block in blocks[1:]:
|
||||||
|
embed.add_field(
|
||||||
|
name="--",
|
||||||
|
value=block,
|
||||||
|
)
|
||||||
|
|
||||||
|
return MessageArgs(embed=embed)
|
||||||
|
|
||||||
|
async def reload(self):
|
||||||
|
await self.campaign.row.refresh()
|
||||||
@@ -0,0 +1,284 @@
|
|||||||
|
"""
|
||||||
|
Defines widgets for displaying a campaign's rewards.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Optional, TYPE_CHECKING
|
||||||
|
import asyncio
|
||||||
|
import datetime as dt
|
||||||
|
|
||||||
|
import discord
|
||||||
|
from discord.ui import Modal
|
||||||
|
from discord.ui.select import select, Select, SelectOption
|
||||||
|
from discord.ui.button import button, Button, ButtonStyle
|
||||||
|
from discord.ui.text_input import TextInput, TextStyle
|
||||||
|
|
||||||
|
from meta import LionBot
|
||||||
|
from meta.errors import UserInputError
|
||||||
|
from meta.config import conf
|
||||||
|
from utils.lib import tabulate, utc_now, MessageArgs, parse_duration
|
||||||
|
from utils.ui import MessageUI
|
||||||
|
from utils.ui.micros import FastModal
|
||||||
|
|
||||||
|
from ...campaign import RewardCampaign
|
||||||
|
from ...data import EarnedReward
|
||||||
|
|
||||||
|
from .. import logger
|
||||||
|
|
||||||
|
|
||||||
|
class RewardEditor(FastModal):
|
||||||
|
# Title is the reward we are editing
|
||||||
|
|
||||||
|
# Block of text with dates and user info
|
||||||
|
blurb = discord.ui.TextDisplay(content='placeholder')
|
||||||
|
|
||||||
|
# Fulfilled is a checkbox, and supports a fulfilled note
|
||||||
|
flufbox = discord.ui.Label(
|
||||||
|
text="Fulfilled",
|
||||||
|
description="Whether this reward has been completed",
|
||||||
|
component=discord.ui.Checkbox(
|
||||||
|
default=False
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Reference and further notes need to be editable,
|
||||||
|
# and need to set the default text correctly
|
||||||
|
reference = discord.ui.Label(
|
||||||
|
text="Reference",
|
||||||
|
description="Reference URL or other information",
|
||||||
|
component=discord.ui.TextInput(
|
||||||
|
style=discord.TextStyle.long,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
notes = discord.ui.Label(
|
||||||
|
text="Notes",
|
||||||
|
description="Further notes for this user/reward",
|
||||||
|
component=discord.ui.TextInput(
|
||||||
|
style=discord.TextStyle.long,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_reward(cls, reward: EarnedReward):
|
||||||
|
title = f"Reward #{reward.earned_id} for {reward.twitch_user_name or reward.twitch_user_id}"
|
||||||
|
self = cls(title=title)
|
||||||
|
|
||||||
|
# blurb
|
||||||
|
table = {
|
||||||
|
'Earned At': discord.utils.format_dt(reward.earned_at, 'F'),
|
||||||
|
'Earned From': reward.earned_from,
|
||||||
|
}
|
||||||
|
prop_table = '\n'.join(tabulate(*table.items()))
|
||||||
|
self.blurb.content = prop_table
|
||||||
|
|
||||||
|
|
||||||
|
# Fulfilled
|
||||||
|
self.flufbox.component.default = (reward.fulfilled_at is not None)
|
||||||
|
|
||||||
|
# Notes
|
||||||
|
self.reference.component.default = reward.reference or ''
|
||||||
|
self.notes.component.default = reward.modnote or ''
|
||||||
|
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
class RewardList(MessageUI):
|
||||||
|
blocklen = 10
|
||||||
|
|
||||||
|
def __init__(self, bot: LionBot, campaign: RewardCampaign, **kwargs):
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
self.bot = bot
|
||||||
|
self.campaign = campaign
|
||||||
|
|
||||||
|
# UI state
|
||||||
|
self.pagen = 0
|
||||||
|
self.pages = []
|
||||||
|
self.reward_blocks = [[]]
|
||||||
|
|
||||||
|
self._rewards: list[EarnedReward] = []
|
||||||
|
# self._reward_viewer: Optional[RewardViewer] = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def page(self):
|
||||||
|
self.pagen %= self.page_count
|
||||||
|
return self.reward_blocks[self.pagen]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def page_count(self):
|
||||||
|
return len(self.reward_blocks)
|
||||||
|
|
||||||
|
# ----- UI API -----
|
||||||
|
|
||||||
|
# ----- UI Components -----
|
||||||
|
@select(
|
||||||
|
cls=Select,
|
||||||
|
placeholder="Select reward to edit",
|
||||||
|
min_values=0,
|
||||||
|
max_values=1,
|
||||||
|
)
|
||||||
|
async def reward_menu(self, selection: discord.Interaction, selected):
|
||||||
|
"""
|
||||||
|
Select reward to view and edit.
|
||||||
|
|
||||||
|
Maybe just edit for a start?
|
||||||
|
"""
|
||||||
|
if selected.values:
|
||||||
|
# Hopefully this is a list of reminderids
|
||||||
|
values = selected.values
|
||||||
|
# Create detail widget, add to children
|
||||||
|
# Spawn editor modal for this reward, callback to refresh detail widget
|
||||||
|
# Also want to refresh this widget
|
||||||
|
# Show detail widget as followup
|
||||||
|
# TODO: Dedicated editor maybe, could e.g. embed refs. Maybe.
|
||||||
|
value = int(selected.values[0])
|
||||||
|
reward = next(r for r in self._rewards if r.earned_id == value)
|
||||||
|
modal = RewardEditor.from_reward(reward)
|
||||||
|
|
||||||
|
currently_fluffed = (reward.fulfilled_at is not None)
|
||||||
|
|
||||||
|
@modal.submit_callback()
|
||||||
|
async def on_editor_submit(interaction: discord.Interaction):
|
||||||
|
update_args = {}
|
||||||
|
|
||||||
|
if modal.flufbox.component.value and not currently_fluffed:
|
||||||
|
# Reward has been fluffed
|
||||||
|
update_args['fulfilled_at'] = utc_now()
|
||||||
|
elif currently_fluffed and not modal.flufbox.component.value:
|
||||||
|
# Reward has been unfluffed
|
||||||
|
update_args['fulfilled_at'] = None
|
||||||
|
|
||||||
|
new_ref_value = modal.reference.value or None
|
||||||
|
if new_ref_value != reward.reference:
|
||||||
|
update_args['reference'] = new_ref_value
|
||||||
|
|
||||||
|
new_notes_value = modal.notes.value or None
|
||||||
|
if new_notes_value != reward.modnote:
|
||||||
|
update_args['modnote'] = new_notes_value
|
||||||
|
|
||||||
|
if update_args:
|
||||||
|
await interaction.response.defer(thinking=True, ephemeral=True)
|
||||||
|
await self.campaign.update_reward(reward.earned_id, **update_args)
|
||||||
|
await self.refresh(thinking=interaction)
|
||||||
|
else:
|
||||||
|
await interaction.response.defer(thinking=False)
|
||||||
|
|
||||||
|
|
||||||
|
await selection.response.send_modal(modal)
|
||||||
|
|
||||||
|
await self.refresh()
|
||||||
|
else:
|
||||||
|
await selection.response.defer()
|
||||||
|
|
||||||
|
async def reward_menu_refresh(self):
|
||||||
|
menu = self.reward_menu
|
||||||
|
rewards = self.page
|
||||||
|
if rewards:
|
||||||
|
menu.options = [
|
||||||
|
self._format_reward_option(r) for r in rewards
|
||||||
|
]
|
||||||
|
menu.disabled = False
|
||||||
|
else:
|
||||||
|
menu.options = [
|
||||||
|
SelectOption(label='DUMMY')
|
||||||
|
]
|
||||||
|
menu.disabled = True
|
||||||
|
|
||||||
|
# Meta buttons
|
||||||
|
@button(emoji=conf.emojis.getemoji("forward"))
|
||||||
|
async def next_page_button(self, press: discord.Interaction, pressed):
|
||||||
|
await press.response.defer()
|
||||||
|
self.pagen += 1
|
||||||
|
await self.refresh()
|
||||||
|
|
||||||
|
@button(emoji=conf.emojis.getemoji("backward"))
|
||||||
|
async def prev_page_button(self, press: discord.Interaction, pressed):
|
||||||
|
await press.response.defer()
|
||||||
|
self.pagen -= 1
|
||||||
|
await self.refresh()
|
||||||
|
|
||||||
|
@button(emoji=conf.emojis.cancel, style=ButtonStyle.red)
|
||||||
|
async def quit_button(self, press: discord.Interaction, pressed: Button):
|
||||||
|
"""Close the UI and all children."""
|
||||||
|
await press.response.defer(thinking=False)
|
||||||
|
await self.quit()
|
||||||
|
|
||||||
|
@button(emoji=conf.emojis.refresh)
|
||||||
|
async def refresh_button(self, press: discord.Interaction, pressed: Button):
|
||||||
|
await press.response.defer()
|
||||||
|
await self.refresh()
|
||||||
|
|
||||||
|
# ----- UI Flow -----
|
||||||
|
def _format_reward_section(self, reward: EarnedReward) -> tuple[str, str]:
|
||||||
|
"""
|
||||||
|
Format the earned reward as an embed section.
|
||||||
|
"""
|
||||||
|
# Title is Reward #n earned by twitch_username
|
||||||
|
# Reward, Earned at, Earned by, Fulfilled at (not fulfilled/date), Ref, Added Notes
|
||||||
|
name = f"Reward #{reward.earned_id} earned by {reward.twitch_user_name or reward.twitch_user_id}"
|
||||||
|
|
||||||
|
if reward.fulfilled_at is not None:
|
||||||
|
fat = discord.utils.format_dt(reward.fulfilled_at, 'F')
|
||||||
|
if reward.fulfilled_note is not None:
|
||||||
|
fluf = f"{fat} ({reward.fulfilled_note})"
|
||||||
|
else:
|
||||||
|
fluf = fat
|
||||||
|
else:
|
||||||
|
fluf = "*Not Fulfilled*"
|
||||||
|
|
||||||
|
table = {
|
||||||
|
'Reward': "Plus Campaign Sketch",
|
||||||
|
'Earned At': discord.utils.format_dt(reward.earned_at, 'F'),
|
||||||
|
'Earned From': reward.earned_from,
|
||||||
|
'Fulfilled At': fluf,
|
||||||
|
'Reference': reward.reference or "*No reference set*",
|
||||||
|
'Further notes': reward.modnote or "*No notes*",
|
||||||
|
}
|
||||||
|
prop_table = '\n'.join(tabulate(*table.items()))
|
||||||
|
|
||||||
|
return (name, prop_table)
|
||||||
|
|
||||||
|
def _format_reward_option(self, reward: EarnedReward) -> SelectOption:
|
||||||
|
"""
|
||||||
|
Format the earned reward as a selectable option
|
||||||
|
"""
|
||||||
|
# Reward #n earned by twitch_username
|
||||||
|
# Value is the reward id
|
||||||
|
name = f"Reward #{reward.earned_id} earned by {reward.twitch_user_name or reward.twitch_user_id}"
|
||||||
|
value = reward.earned_id
|
||||||
|
|
||||||
|
return SelectOption(
|
||||||
|
label=name, value=value
|
||||||
|
)
|
||||||
|
|
||||||
|
async def refresh_layout(self):
|
||||||
|
to_refresh = (
|
||||||
|
self.reward_menu_refresh(),
|
||||||
|
)
|
||||||
|
await asyncio.gather(*to_refresh)
|
||||||
|
|
||||||
|
if self.page_count > 1:
|
||||||
|
self.prev_page_button.disabled = True
|
||||||
|
self.next_page_button.disabled = True
|
||||||
|
else:
|
||||||
|
self.prev_page_button.disabled = False
|
||||||
|
self.next_page_button.disabled = False
|
||||||
|
|
||||||
|
self.set_layout(
|
||||||
|
(self.prev_page_button, self.refresh_button, self.next_page_button, self.quit_button),
|
||||||
|
(self.reward_menu,),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def make_message(self) -> MessageArgs:
|
||||||
|
embed = discord.Embed(title=f"{self.campaign.row.campaign_name} Reward List")
|
||||||
|
# embed.set_footer with last update
|
||||||
|
|
||||||
|
return MessageArgs(embed=embed)
|
||||||
|
|
||||||
|
async def reload(self):
|
||||||
|
rewards = self._rewards = await self.campaign.get_rewards()
|
||||||
|
|
||||||
|
# TODO: Consider filter by unfulfilled or reward type
|
||||||
|
self.reward_blocks = [
|
||||||
|
rewards[i:i+self.blocklen]
|
||||||
|
for i in range(0, len(rewards), self.blocklen)
|
||||||
|
] or [[]]
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
import asyncio
|
||||||
|
import discord
|
||||||
from psycopg import sql
|
from psycopg import sql
|
||||||
|
from urllib.parse import urlparse, parse_qs
|
||||||
|
|
||||||
from data import RawExpr, Expression
|
from data import RawExpr, Expression
|
||||||
|
|
||||||
@@ -19,3 +22,44 @@ def asexpr(value: Any) -> RawExpr:
|
|||||||
Turn a value into an expression.
|
Turn a value into an expression.
|
||||||
"""
|
"""
|
||||||
return RawExpr(sql.Placeholder(), (value,))
|
return RawExpr(sql.Placeholder(), (value,))
|
||||||
|
|
||||||
|
async def fire_and_forget(awaitable, do_in=1, ignorable=(discord.HTTPException)):
|
||||||
|
await asyncio.sleep(do_in)
|
||||||
|
try:
|
||||||
|
await awaitable
|
||||||
|
except ignorable:
|
||||||
|
pass
|
||||||
|
except Exception as e:
|
||||||
|
# TODO: Log unexpected exceptions
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ThreadedWebhook(discord.Webhook):
|
||||||
|
__slots__ = ("thread_id",)
|
||||||
|
|
||||||
|
def __init__(self, *args, thread_id=None, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.thread_id = thread_id
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_url(cls, url: str, *args, **kwargs):
|
||||||
|
self = super().from_url(url, *args, **kwargs)
|
||||||
|
parse = urlparse(url)
|
||||||
|
if parse.query:
|
||||||
|
args = parse_qs(parse.query)
|
||||||
|
if "thread_id" in args:
|
||||||
|
self.thread_id = int(args["thread_id"][0])
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def send(self, *args, **kwargs):
|
||||||
|
if self.thread_id is not None:
|
||||||
|
kwargs.setdefault("thread", discord.Object(self.thread_id))
|
||||||
|
return await super().send(*args, **kwargs)
|
||||||
|
|
||||||
|
async def test_webhook(self):
|
||||||
|
embed = discord.Embed(
|
||||||
|
title="Testing",
|
||||||
|
description="Testing logging webhook, feel free to delete."
|
||||||
|
)
|
||||||
|
result = await self.send(embed=embed, wait=True, silent=True)
|
||||||
|
asyncio.create_task(fire_and_forget(result.delete()))
|
||||||
|
await result
|
||||||
|
|||||||
+12
-11
@@ -73,14 +73,14 @@ 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 NOTICE): User subscribed for {duration} months at tier {tier}",
|
earned_reason=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"],
|
||||||
)
|
)
|
||||||
await self.dispatch_update(campaign)
|
await self.dispatch_update(campaign)
|
||||||
# TODO: Webhook logging maybe..
|
# TODO: Webhook logging maybe.. (do this in campaign)
|
||||||
# Or just general logging.
|
# Or just general logging. (also do this in campaign, but we can do again here)
|
||||||
|
|
||||||
@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):
|
||||||
@@ -136,9 +136,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 = "{name}: {given} rewards earned out of {cap}!"
|
response = f"{name}: {given} rewards earned out of {cap}!"
|
||||||
else:
|
else:
|
||||||
response = "{name}: {given} rewards earned so far!"
|
response = f"{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,
|
||||||
@@ -175,7 +175,7 @@ class CampaignComponent(cmds.Component):
|
|||||||
await ctx.reply(
|
await ctx.reply(
|
||||||
f"Your campaign '{existing_name}' is already active! "
|
f"Your campaign '{existing_name}' is already active! "
|
||||||
"Sorry, we don't yet support multiple active campaigns, "
|
"Sorry, we don't yet support multiple active campaigns, "
|
||||||
"please finish your campaign before starting a new one."
|
"please use '!campaign finish' to end your current campaign before starting a new one."
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -230,17 +230,16 @@ 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 = "Completed {name}: {given} rewards earned out of {cap}!"
|
response = f"Completed {name}: {given} rewards earned out of {cap}!"
|
||||||
else:
|
else:
|
||||||
response = "Completed {name}: {given} rewards earned so far!"
|
response = f"Completed {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,
|
||||||
cap=reward_cap,
|
cap=reward_cap,
|
||||||
)
|
)
|
||||||
await ctx.reply(formatte
|
await ctx.reply(formatted)
|
||||||
await self.dispatch_update(campaign)
|
await self.dispatch_update(campaign)
|
||||||
)
|
|
||||||
|
|
||||||
@group_campaign.command(name="reward")
|
@group_campaign.command(name="reward")
|
||||||
@cmds.is_moderator()
|
@cmds.is_moderator()
|
||||||
@@ -259,6 +258,8 @@ class CampaignComponent(cmds.Component):
|
|||||||
name = user.display_name or profile.nickname or "Unknown"
|
name = user.display_name or profile.nickname or "Unknown"
|
||||||
pid = profile.profileid
|
pid = profile.profileid
|
||||||
|
|
||||||
|
reason = f"(Given by {ctx.author.mention}) {reward_reason}"
|
||||||
|
|
||||||
campaigns = await self.campaigns.fetch_campaigns(cid, active=True)
|
campaigns = await self.campaigns.fetch_campaigns(cid, active=True)
|
||||||
if len(campaigns) > 1:
|
if len(campaigns) > 1:
|
||||||
names = ", ".join(camp.row.campaign_name for camp in campaigns)
|
names = ", ".join(camp.row.campaign_name for camp in campaigns)
|
||||||
@@ -270,7 +271,7 @@ class CampaignComponent(cmds.Component):
|
|||||||
|
|
||||||
await campaign.add_reward(
|
await campaign.add_reward(
|
||||||
pid,
|
pid,
|
||||||
reward_reason,
|
reason,
|
||||||
twitch_user_id=user.id,
|
twitch_user_id=user.id,
|
||||||
twitch_user_name=user.name,
|
twitch_user_name=user.name,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user