Skip to content

Commit

Permalink
Changing to a new method for dynamic titles
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko259 committed Sep 27, 2024
1 parent 6b26c53 commit 58c75ae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
39 changes: 20 additions & 19 deletions cogs/staffings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,30 @@ def cog_unload(self):
#

# A function to dynamically fetch and return a list of titles as choices
async def get_title_choices(self) -> List[app_commands.Choice[str]]:
async def get_title_choices(self, interaction: Interaction, current: str) -> List[app_commands.Choice[str]]:
# Fetch the latest available titles from the database
titles = StaffingAsync._get_titles()
return [app_commands.Choice(name=title, value=title) for title in titles]

# Return titles that match the user's input
return [
app_commands.Choice(name=title, value=title)
for title in titles if current.lower() in title.lower()
]

# Autocomplete function to fetch and filter titles in real-time
async def avail_title_autocomplete(self, interaction: Interaction, current: str):
# Fetch the latest available titles from the database
titles = StaffingAsync._get_avail_titles()

# Return titles that match the user's input
return [
app_commands.Choice(name=title, value=title)
for title in titles if current.lower() in title.lower()
]

@app_commands.command(name="setupstaffing", description="Bot setups staffing information")
@app_commands.describe(title="What should the title of the staffing be?", week_int="What should the week interval be? eg. 1 then the date will be selected each week.", section_amount="What should the section amount be? eg. 3 then there will be 3 sections.", restrict_booking="Should the staffing restrict booking to first section before allowing other sections too?")
@app_commands.autocomplete(title=get_title_choices)
@app_commands.checks.has_any_role(*staff_roles())
async def setup_staffing(self, interaction: Interaction, title: str, week_int: app_commands.Range[int, 1, 4], section_amount: app_commands.Range[int, 1, 4], restrict_booking: Literal["Yes", "No"], channel: TextChannel):
ctx: commands.Context = await self.bot.get_context(interaction)
Expand Down Expand Up @@ -100,23 +118,6 @@ async def setup_staffing(self, interaction: Interaction, title: str, week_int: a
DB.insert(self=self, table="positions", columns=['position', 'user', 'type', 'local_booking', 'start_time', 'end_time', 'event'], values=[pos, "", j, local[section_positions[x][pos]['local_booking']], section_positions[x][pos]['start_time'], section_positions[x][pos]['end_time'], event])
j += 1

# Link this command to custom autocomplete for titles
@setup_staffing.autocomplete("title")
async def title_autocomplete(self, interaction: Interaction, current: str):
titles = await self.get_title_choices()
return [choice for choice in titles if current.lower() in choice.name.lower()][:25] # Limit to 25 choices (Discord limit)

# Autocomplete function to fetch and filter titles in real-time
async def avail_title_autocomplete(self, interaction: Interaction, current: str):
# Fetch the latest available titles from the database
titles = StaffingAsync._get_avail_titles()

# Return titles that match the user's input
return [
app_commands.Choice(name=title, value=title)
for title in titles if current.lower() in title.lower()
]

@app_commands.command(name="refreshevent", description="Bot refreshes selected event")
@app_commands.describe(title="Which staffing would you like to refresh?")
@app_commands.autocomplete(title=avail_title_autocomplete)
Expand Down
3 changes: 1 addition & 2 deletions cogs/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async def user_check(self, interaction: discord.Integration):

async def sync_commands(self, override=False):
now = datetime.now().isoformat()
guild = self.bot.get_guild(GUILD_ID)
guild = discord.Object(id=GUILD_ID)

try:
if DEBUG == True and override == False:
Expand All @@ -111,7 +111,6 @@ async def sync_commands(self, override=False):
try:
if guild:
await self.bot.tree.sync(guild=guild) # Sync commands to a specific guild for faster deployment
print(f'Commands has been synced for guild {guild}', flush=True)
else:
await self.bot.tree.sync() # Sync global commands (might take up to 1 hour to reflect globally)
except discord.HTTPException as e:
Expand Down

0 comments on commit 58c75ae

Please sign in to comment.