This repository has been archived on 2025-04-28. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
off-cooldown-bot/off_cooldown_bot/open_raid.py
T
imcompiling f79d63c78d
build_develop / build (push) Successful in 22s
Add timestamp to description.
2025-03-20 09:27:36 -04:00

152 lines
5.5 KiB
Python

"""Contains functions for open raid sign-ups."""
import datetime
import nextcord
from off_cooldown_bot.logger import ocd_logger
MAX_TANKS = 2
MAX_HEALERS = 2
MAX_DPS = 25
class OpenRaid(nextcord.ui.View):
"""Class defines view for buttons on an open raid schedule message."""
def __init__(self) -> None:
super().__init__(timeout=None, auto_defer=True)
@nextcord.ui.button(label="Tank", style=nextcord.ButtonStyle.blurple, emoji="🛡️", custom_id="open_raid_tank")
async def tank(self, button: nextcord.ui.Button, interaction: nextcord.Interaction) -> None: # noqa: ARG002
"""
Signs a user up as a tank.
Removes them if they are already signed up.
Args:
button (nextcord.ui.Button): Un-used.
interaction (nextcord.Interaction): Interaction with command.
"""
if interaction.message is None or interaction.user is None:
ocd_logger.warning("Command was called without a message or user.")
return
embed = interaction.message.embeds[0]
user_str = f"<@{interaction.user.id}>"
for index, field in enumerate(embed.fields):
if field.name == "Tanks":
values = [] if field.value is None else field.value.split("\n")
if user_str in values:
values.remove(user_str)
else:
if len(values) >= MAX_TANKS:
await interaction.response.send_message(
"Enough tanks have signed up, please choose another role.", ephemeral=True
)
return
values.append(user_str)
embed.set_field_at(index, name=field.name, value="\n".join(values), inline=False)
break
await interaction.edit(embed=embed)
@nextcord.ui.button(label="Healer", style=nextcord.ButtonStyle.green, emoji="❤️", custom_id="open_raid_healer")
async def healer(self, button: nextcord.ui.Button, interaction: nextcord.Interaction) -> None: # noqa: ARG002
"""
Signs a user up as a healer.
Removes them if they are already signed up.
Args:
button (nextcord.ui.Button): Un-used.
interaction (nextcord.Interaction): Interaction with command.
"""
if interaction.message is None or interaction.user is None:
ocd_logger.warning("Command was called without a message or user.")
return
embed = interaction.message.embeds[0]
user_str = f"<@{interaction.user.id}>"
for index, field in enumerate(embed.fields):
if field.name == "Healers":
values = [] if field.value is None else field.value.split("\n")
if user_str in values:
values.remove(user_str)
else:
if len(values) >= MAX_HEALERS:
await interaction.response.send_message(
"Enough healers have signed up, please choose another role.", ephemeral=True
)
return
values.append(user_str)
embed.set_field_at(index, name=field.name, value="\n".join(values), inline=False)
break
await interaction.edit(embed=embed)
@nextcord.ui.button(label="DPS", style=nextcord.ButtonStyle.red, emoji="⚔️", custom_id="open_raid_dps")
async def dps(self, button: nextcord.ui.Button, interaction: nextcord.Interaction) -> None: # noqa: ARG002
"""
Signs a user up as dps.
Removes them if they are already signed up.
Args:
button (nextcord.ui.Button): Un-used.
interaction (nextcord.Interaction): Interaction with command.
"""
if interaction.message is None or interaction.user is None:
ocd_logger.warning("Command was called without a message or user.")
return
embed = interaction.message.embeds[0]
user_str = f"<@{interaction.user.id}>"
for index, field in enumerate(embed.fields):
if field.name == "DPS":
values = [] if field.value is None else field.value.split("\n")
if user_str in values:
values.remove(user_str)
else:
if len(values) >= MAX_DPS:
await interaction.response.send_message(
"Enough dps have signed up, please choose another role.", ephemeral=True
)
return
values.append(user_str)
embed.set_field_at(index, name=field.name, value="\n".join(values), inline=False)
break
await interaction.edit(embed=embed)
def build_raid_embed() -> nextcord.Embed:
"""
Build the open raid embed.
Returns:
nextcord.Embed: Embed that shows the raid's sign-ups.
"""
today = datetime.datetime.now(tz=datetime.UTC).replace(hour=20)
saturday_idx = 5
days_delta = saturday_idx - today.weekday()
if days_delta < 0:
days_delta += 7
raid_time = today + datetime.timedelta(days_delta)
embed = nextcord.Embed(
title="Off Cooldown Open Raid", description=f"Raid time: t:{int(raid_time.timestamp())}:F", timestamp=raid_time
)
embed.add_field(name="Tanks", value="", inline=False)
embed.add_field(name="Healers", value="", inline=False)
embed.add_field(name="DPS", value="", inline=False)
return embed