Skip to content

Commit

Permalink
Disable reports button when not active (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrxyz authored Mar 15, 2024
1 parent d2adc71 commit 069b136
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ async def on_ready(self):
if not self.change_status.is_running():
self.change_status.start()
await self.fetch_vars()
await self.reports_cog.update_report_channel.run_immediately()

async def close(self):
await self.session.close()
Expand Down
48 changes: 40 additions & 8 deletions src/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,31 @@ async def on_submit(self, interaction: discord.Interaction) -> None:
)


class ReportsView(MILBotView):
class SubmitButton(discord.ui.Button):
def __init__(self, bot: MILBot):
self.bot = bot
super().__init__(timeout=None)
if not is_active():
super().__init__(
label="Reports are not currently active.",
style=discord.ButtonStyle.grey,
disabled=True,
custom_id="reports_view:submit",
)
elif datetime.datetime.today().weekday() in [0, 1]:
super().__init__(
label="Reports can only be submitted between Wednesday and Sunday.",
style=discord.ButtonStyle.red,
disabled=True,
custom_id="reports_view:submit",
)
else:
super().__init__(
label="Submit your report!",
style=discord.ButtonStyle.green,
custom_id="reports_view:submit",
)

@discord.ui.button(
label="Submit your report!",
style=discord.ButtonStyle.green,
custom_id="reports_view:submit",
)
async def submit(self, interaction: discord.Interaction, _: discord.ui.Button):
async def callback(self, interaction: discord.Interaction):
# If button is triggered on Sunday or Monday, send error message
if datetime.datetime.today().weekday() in [0, 1]:
return await interaction.response.send_message(
Expand All @@ -182,6 +196,13 @@ async def submit(self, interaction: discord.Interaction, _: discord.ui.Button):
await interaction.response.send_modal(ReportsModal(self.bot))


class ReportsView(MILBotView):
def __init__(self, bot: MILBot):
self.bot = bot
super().__init__(timeout=None)
self.add_item(SubmitButton(bot))


@dataclass
class Student:
name: str
Expand Down Expand Up @@ -217,6 +238,7 @@ def __init__(self, bot: MILBot):
self.post_reminder.start(self)
self.last_week_summary.start(self)
self.individual_reminder.start(self)
self.update_report_channel.start(self)

def date_to_column(self, date: datetime.date) -> int:
"""
Expand Down Expand Up @@ -332,6 +354,16 @@ async def last_week_summary(self):
team_leads_ch = self.bot.team_leads_ch(team)
await team_leads_ch.send(embed=embed)

@run_on_weekday([calendar.MONDAY, calendar.WEDNESDAY], 0, 0)
async def update_report_channel(self):
channel_history = [m async for m in self.bot.reports_channel.history(limit=1)]
if not channel_history:
return

last_message = channel_history[0]
if last_message.author == self.bot.user:
await last_message.edit(view=ReportsView(self.bot))

@commands.is_owner()
@commands.command()
async def reportview(self, ctx):
Expand Down

0 comments on commit 069b136

Please sign in to comment.