Skip to content

Commit

Permalink
Added editing shift type functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikey committed Dec 31, 2023
1 parent 38ade89 commit 5273ef3
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 5 deletions.
6 changes: 4 additions & 2 deletions cogs/ShiftLogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ async def duty_admin(self, ctx, member: discord.Member, type: str = "Default"):
title="Access Denied",
description="This individual does not have access to this shift type.",
color=BLANK_COLOR
)
),
view=None
)

shift = await self.bot.shift_management.get_current_shift(member, ctx.guild.id)
Expand Down Expand Up @@ -386,7 +387,8 @@ async def duty_manage(self, ctx, *, type: str = "Default"):
title="Access Denied",
description="You do not have access to this shift type.",
color=BLANK_COLOR
)
),
view=None
)


Expand Down
121 changes: 118 additions & 3 deletions menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3491,6 +3491,37 @@ async def _create(self, interaction: discord.Interaction, button: discord.ui.But
self.value = 'create'
self.stop()

@discord.ui.button(label="Edit Shift Type")
async def _edit(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user.id not in [self.user_id]:
return await interaction.response.send_message(embed=discord.Embed(
title="Not Permitted",
description="You are not permitted to interact with these buttons.",
color=blank_color
))
# await interaction.response.defer(thinking=False)
self.modal = CustomModal(
"Edit Shift Type",
[
(
"shift_type_name",
discord.ui.TextInput(
label="Name",
placeholder="Name of Shift Type"
)
)
]
)
await interaction.response.send_modal(self.modal)
await self.modal.wait()
if self.modal.shift_type_name.value:
self.name_for_creation = self.modal.shift_type_name.value
else:
return
self.value = 'edit'
self.stop()


@discord.ui.button(label="Delete Shift Type")
async def _delete(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user.id not in [self.user_id]:
Expand Down Expand Up @@ -4330,6 +4361,81 @@ async def shift_types(self, interaction: discord.Interaction, button: discord.ui
)

await view.wait()

if view.value == "edit":
selected_item = None
for item in shift_types:
if item['name'] == view.name_for_creation:
selected_item = item
break

if not selected_item:
return await interaction.edit_original_response(
embed=discord.Embed(
title="Incorrect Shift Type",
description="This shift type is incorrect or invalid.",
color=BLANK_COLOR
),
view=None
)

data = selected_item

embed = discord.Embed(
title="Edit a Shift Type",
description=(
f"<:replytop:1138257149705863209> **Name:** {data['name']}\n"
f"<:replymiddle:1138257195121791046> **ID:** {data['id']}\n"
f"<:replymiddle:1138257195121791046> **Shift Channel:** {'<#{}>'.format(data.get('channel', None)) if data.get('channel', None) is not None else 'Not set'}\n"
f"<:replymiddle:1138257195121791046> **Nickname Prefix:** {data.get('nickname') or 'None'}\n"
f"<:replymiddle:1138257195121791046> **On-Duty Roles:** {', '.join(['<@&{}>'.format(r) for r in data.get('role', [])]) or 'Not set'}\n"
f"<:replybottom:1138257250448855090> **Access Roles:** {', '.join(['<@&{}>'.format(r) for r in data.get('access_roles', [])]) or 'Not set'}\n\n\n"
f"*Access Roles are roles that are able to freely use this Shift Type and are able to go on-duty as this Shift Type. If an access role is selected, an individual must have it to go on-duty with this Shift Type.*"
),
color=BLANK_COLOR
)

roles = list(filter(lambda x: x is not None, [discord.utils.get(interaction.guild.roles, id=i) for i in data.get('role', [])]))
access_roles = list(filter(lambda x: x is not None, [discord.utils.get(interaction.guild.roles, id=i) for i in data.get('access_roles', [])]))
shift_channel = list(filter(lambda x: x is not None, [discord.utils.get(interaction.guild.channels, id=data.get('channel', 0))]))



view = ShiftTypeCreator(interaction.user.id, data, "edit", {
"On-Duty Roles": roles,
"Access Roles": access_roles,
"Shift Channel": shift_channel
})
view.restored_interaction = interaction
msg = await interaction.original_response()
await msg.edit(view=view, embed=embed)
await view.wait()
if view.cancelled is True:
return

dataset = settings.get('shift_types', {}).get('types', [])

for index, item in enumerate(dataset):
if item['id'] == view.dataset['id']:
dataset[index] = view.dataset
break
if not settings.get('shift_types'):
settings['shift_types'] = {}
settings['shift_types']['types'] = dataset
else:
settings['shift_types']['types'] = dataset

await self.bot.settings.update_by_id(settings)
await msg.edit(
embed=discord.Embed(
title="<:success:1163149118366040106> Shift Type Edited",
description="Your shift type has been edited!",
color=GREEN_COLOR
),
view=None
)
return

if view.value == "create":
data = {
'id': next(generator),
Expand All @@ -4351,7 +4457,7 @@ async def shift_types(self, interaction: discord.Interaction, button: discord.ui
color=BLANK_COLOR
)

view = ShiftTypeCreator(interaction.user.id, data)
view = ShiftTypeCreator(interaction.user.id, data, "create")
view.restored_interaction = interaction
msg = await interaction.original_response()
await msg.edit(view=view, embed=embed)
Expand Down Expand Up @@ -4379,6 +4485,7 @@ async def shift_types(self, interaction: discord.Interaction, button: discord.ui
),
view=None
)
return
elif view.value == "delete":
try:
type_id = int(view.selected_for_deletion.strip())
Expand Down Expand Up @@ -5424,12 +5531,20 @@ async def _reload(self, interaction: discord.Interaction, button: discord.Button


class ShiftTypeCreator(discord.ui.View):
def __init__(self, user_id: int, dataset: dict):
def __init__(self, user_id: int, dataset: dict, option: typing.Literal['create', 'edit'], preset_values: dict | None = None):
super().__init__(timeout=900.0)
self.user_id = user_id
self.restored_interaction = None
self.dataset = dataset
self.cancelled = None
self.option = option

for key, value in (preset_values or {}).items():
for item in self.children:
if isinstance(item, discord.ui.RoleSelect) or isinstance(item, discord.ui.ChannelSelect):
if item.placeholder == key:
item.default_values = value


async def interaction_check(self, interaction: Interaction, /) -> bool:
if interaction.user.id == self.user_id:
Expand All @@ -5444,7 +5559,7 @@ async def interaction_check(self, interaction: Interaction, /) -> bool:

async def refresh_ui(self, message: discord.Message):
embed = discord.Embed(
title="Shift Type Creation",
title=f"{self.option.title()} a Shift Type",
description=(
f"<:replytop:1138257149705863209> **Name:** {self.dataset['name']}\n"
f"<:replymiddle:1138257195121791046> **ID:** {self.dataset['id']}\n"
Expand Down

0 comments on commit 5273ef3

Please sign in to comment.