Skip to content

Commit

Permalink
improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
odrling committed Dec 20, 2024
1 parent 84149de commit 57df0d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
4 changes: 3 additions & 1 deletion nanachan/discord/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ async def wait(self):
await asyncio.wait(modifiers_tasks)

@classmethod
def set_will_delete(cls, *, check: Callable[[MultiplexingContext], bool]):
def set_will_delete(
cls, *, check: Callable[[MultiplexingContext], bool]
) -> asyncio.Future[MultiplexingContext]:
fut = asyncio.get_running_loop().create_future()
cls.listeners.append((check, fut))
return fut
Expand Down
38 changes: 25 additions & 13 deletions nanachan/extensions/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import datetime, timezone
from functools import partial
from operator import itemgetter
from typing import Protocol, TypedDict
from typing import Protocol, TypedDict, override

import discord
import discord.ext
Expand Down Expand Up @@ -247,14 +247,17 @@ def __init__(self, *, title: str, profile: UpsertProfileBody):
default_birthday = (
self.profile.birthday.strftime('%Y-%m-%d') if self.profile.birthday else None
)
self.birthday = ui.TextInput(

TextInput = ui.TextInput[ProfileModal]

self.birthday = TextInput(
label='Birthdate',
placeholder='Enter your birthdate (YYYY-MM-DD)',
style=discord.TextStyle.short,
required=False,
default=default_birthday,
)
self.full_name = ui.TextInput(
self.full_name = TextInput(
label='Full Name',
placeholder='Enter you full name (First name Last name)',
style=discord.TextStyle.short,
Expand All @@ -264,36 +267,38 @@ def __init__(self, *, title: str, profile: UpsertProfileBody):
default_graduation_year = (
str(self.profile.graduation_year) if self.profile.graduation_year else ''
)
self.graduation_year = ui.TextInput(
self.graduation_year = TextInput(
label='Graduation Year',
placeholder='Enter your graduation year',
style=discord.TextStyle.short,
required=False,
default=default_graduation_year,
)
self.pronouns = ui.TextInput(
self.pronouns = TextInput(
label='Pronouns',
placeholder='Enter your prnouns',
style=discord.TextStyle.short,
required=False,
default=self.profile.pronouns,
)
self.telephone = ui.TextInput(
self.telephone = TextInput(
label='Phone Number',
placeholder='Enter your phone number',
style=discord.TextStyle.short,
required=False,
default=self.profile.telephone,
)

self.add_item(self.birthday)
self.add_item(self.full_name)
self.add_item(self.graduation_year)
self.add_item(self.pronouns)
self.add_item(self.telephone)

@override
async def on_submit(self, interaction: Interaction):
await interaction.response.defer()
errors = []
errors: list[str] = []
if not self.birthday_regex.fullmatch(self.birthday.value):
errors.append('Invalid birthday format.')
if not self.graduation_year_regex.fullmatch(self.graduation_year.value):
Expand Down Expand Up @@ -326,7 +331,8 @@ def __init__(self, bot: Bot, member: Member, profile: UpsertProfileBody):
super().__init__(bot)
self.member = member
self.profile = profile
n7_major_select = ui.Select(

n7_major_select = ui.Select[ProfileCreateOrChangeView](
placeholder='Select your major at N7',
options=[
SelectOption(emoji='⚡', label='Elec', value='Elec'),
Expand All @@ -336,15 +342,20 @@ def __init__(self, bot: Bot, member: Member, profile: UpsertProfileBody):
row=1,
)
n7_major_select.callback = self._n7_major_select_cb
form_button = ui.Button(label='Open Form', emoji='📔', row=0)

Button = ui.Button[ProfileCreateOrChangeView]

form_button = Button(label='Open Form', emoji='📔', row=0)
form_button.callback = self._instantiate_form_modal
photo_button = ui.Button(label='Upload picture', emoji='🖼️', row=0)

photo_button = Button(label='Upload picture', emoji='🖼️', row=0)
photo_button.callback = self._photo_button_cb
confirm_button = ui.Button(

confirm_button = Button(
label='Confirm changes', emoji=self.bot.get_nana_emoji('FubukiGO'), row=2
)
confirm_button.callback = self._confirm_button_cb
cancel_button = ui.Button(
cancel_button = Button(
label='Cancel changes', emoji=bot.get_nana_emoji('FubukiStop'), row=2
)
cancel_button.callback = self._cancel_button_cb
Expand All @@ -362,7 +373,7 @@ async def _edit_embed(self, profile: UpsertProfileBody, interaction: Interaction
async def _photo_button_cb(self, interaction: Interaction):
await interaction.response.send_message('Upload your profile picture', ephemeral=True)

def check(message):
def check(message: MultiplexingContext):
return all(
[
message.command is None,
Expand Down Expand Up @@ -414,6 +425,7 @@ async def _instantiate_form_modal(self, interaction: Interaction):
self.profile = modal.profile
await self._edit_embed(self.profile, interaction)

@override
async def interaction_check(self, interaction: Interaction):
return self.member.id == interaction.user.id

Expand Down

0 comments on commit 57df0d7

Please sign in to comment.