Skip to content

Commit

Permalink
Completely use decorators for view items.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerrie-Aries committed Aug 22, 2023
1 parent dc65348 commit d15d5d0
Showing 1 changed file with 36 additions and 50 deletions.
86 changes: 36 additions & 50 deletions announcement/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,6 @@ async def on_submit(self, interaction: Interaction) -> None:
await self.view.on_modal_submit(interaction)


class ChannelSelect(ui.ChannelSelect):
"""
Channel select. Here, we just want to override the callback.
"""

async def callback(self, interaction: Interaction) -> None:
assert self.view is not None
value = self.values[0]
channel = value.resolve() or await value.fetch()
self.view.announcement.channel = channel
await interaction.response.defer()


class MentionableSelect(ui.MentionableSelect):
"""
Mentionable select. Here, we just want to override the callback.
"""

async def callback(self, interaction: Interaction) -> None:
assert self.view is not None
await interaction.response.defer()
if self.values:
self.view.announcement.content = ", ".join(v.mention for v in self.values)
else:
self.view.announcement.content = MISSING


class AnnouncementView(ui.View):
"""
Represents the AnnouncementView class. The announcement creation panel and sessions
Expand All @@ -138,7 +111,6 @@ def __init__(
self.confirmed: Optional[bool] = None
self._underlying_modals: List[Modal] = []
self.inputs: Dict[str, Any] = {}
self.mentionable_select: MentionableSelect = MISSING

@property
def modals(self) -> List[Modal]:
Expand All @@ -153,34 +125,22 @@ def current(self) -> str:
return self.input_sessions[self.index][0]

def fill_items(self, *, post: bool = False, confirmation: bool = False) -> None:
self._select_menu.options.clear()
self.select_menu.options.clear()
if self.current == "type":
for ts in type_select_maps:
option = discord.SelectOption(**ts)
option.value = ts["label"].lower()
self._select_menu.append_option(option)
self._select_menu.placeholder = "Choose a type"
self.add_item(self._select_menu)
self.select_menu.append_option(option)
self.select_menu.placeholder = "Choose a type"
self.add_item(self.select_menu)
elif self.current == "mention":
for ms in mention_select_maps:
self._select_menu.append_option(discord.SelectOption(**ms))
self._select_menu.placeholder = "Select mention"
self.add_item(self._select_menu)
self.mentionable_select = MentionableSelect(
min_values=0,
max_values=25,
placeholder="Other mentions",
disabled=True,
row=1,
)
self.select_menu.append_option(discord.SelectOption(**ms))
self.select_menu.placeholder = "Select mention"
self.add_item(self.select_menu)
self.add_item(self.mentionable_select)
elif self.current == "channel":
channel_select = ChannelSelect(
channel_types=[discord.ChannelType.news, discord.ChannelType.text],
placeholder="Select a channel",
row=0,
)
self.add_item(channel_select)
self.add_item(self.channel_select)

if confirmation:
buttons = [self._button_yes, self._button_no]
Expand Down Expand Up @@ -280,7 +240,7 @@ async def _action_next(self, *args: Tuple[Interaction, Optional[ui.Button]]) ->
await self.update_view(interaction)

@ui.select(placeholder="...", row=0)
async def _select_menu(self, interaction: Interaction, select: ui.Select) -> None:
async def select_menu(self, interaction: Interaction, select: ui.Select) -> None:
value = select.values[0]
for opt in select.options:
opt.default = opt.value == value
Expand All @@ -297,7 +257,33 @@ async def _select_menu(self, interaction: Interaction, select: ui.Select) -> Non
self.announcement.content = MISSING
await self.update_view(interaction)
else:
raise ValueError(f"Invalid session in `{self.__class__.__name__}._select_menu`: `{current}`.")
raise ValueError(f"Invalid session in `{self.__class__.__name__}.select_menu`: `{self.current}`.")

@ui.select(
cls=ui.MentionableSelect,
placeholder="Other mentions",
row=1,
min_values=0,
max_values=25,
disabled=True,
)
async def mentionable_select(self, interaction: Interaction, select: ui.MentionableSelect) -> None:
if select.values:
self.announcement.content = ", ".join(v.mention for v in select.values)
else:
self.announcement.content = MISSING
await interaction.response.defer()

@ui.select(
cls=ui.ChannelSelect,
placeholder="Select a channel",
channel_types=[discord.ChannelType.news, discord.ChannelType.text],
)
async def channel_select(self, interaction: Interaction, select: ui.ChannelSelect) -> None:
value = select.values[0]
channel = value.resolve() or await value.fetch()
self.announcement.channel = channel
await interaction.response.defer()

@ui.button(label="...")
async def _button_next_or_post(self, interaction: Interaction, button: ui.Button) -> None:
Expand Down

0 comments on commit d15d5d0

Please sign in to comment.