From 45b225a26ab31926faaec1283ab0310972aae11b Mon Sep 17 00:00:00 2001 From: Alex Zhang Date: Sun, 15 Dec 2024 21:23:22 -0800 Subject: [PATCH 1/2] Add UI for GPU type on leaderboard create --- .../cogs/leaderboard_cog.py | 70 +++++++++++++++++-- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/src/discord-cluster-manager/cogs/leaderboard_cog.py b/src/discord-cluster-manager/cogs/leaderboard_cog.py index 2d31c82..8836b61 100644 --- a/src/discord-cluster-manager/cogs/leaderboard_cog.py +++ b/src/discord-cluster-manager/cogs/leaderboard_cog.py @@ -2,6 +2,7 @@ from datetime import datetime import discord +from discord import ui, SelectOption, Interaction from consts import GitHubGPU, ModalGPU from discord import app_commands from discord.ext import commands @@ -179,7 +180,11 @@ async def submit_github( "submission_score": score, }) - user_id = interaction.user.global_name if interaction.user.nick is None else interaction.user.nick + user_id = ( + interaction.user.global_name + if interaction.user.nick is None + else interaction.user.nick + ) await interaction.followup.send( "Successfully ran on GitHub runners!\n" + f"Leaderboard '{leaderboard_name}'.\n" @@ -194,6 +199,31 @@ async def submit_github( ) +class GPUSelectionView(ui.View): + def __init__(self, available_gpus: list[str]): + super().__init__() + + # Add the Select Menu with the list of GPU options + select = ui.Select( + placeholder="Select GPUs for this leaderboard...", + options=[SelectOption(label=gpu, value=gpu) for gpu in available_gpus], + min_values=1, # Minimum number of selections + max_values=len(available_gpus), # Maximum number of selections + ) + select.callback = self.select_callback + self.add_item(select) + + async def select_callback(self, interaction: Interaction): + # Retrieve the selected options + select = interaction.data["values"] + self.selected_gpus = select + await interaction.response.send_message( + f"Selected GPUs: {', '.join(self.selected_gpus)}", + ephemeral=True, + ) + self.stop() + + class LeaderboardCog(commands.Cog): def __init__(self, bot): self.bot: commands.Bot = bot @@ -256,6 +286,24 @@ async def leaderboard_create( # Read the template file template_content = await reference_code.read() + # Ask the user to select GPUs + view = GPUSelectionView([gpu.name for gpu in GitHubGPU]) + + if interaction.response.is_done(): + await interaction.followup.send( + "Please select GPUs for this leaderboard.", + view=view, + ephemeral=True, + ) + else: + await interaction.response.send_message( + "Please select GPUs for this leaderboard.", + view=view, + ephemeral=True, + ) + + await view.wait() + with self.bot.leaderboard_db as db: print( leaderboard_name, @@ -268,12 +316,20 @@ async def leaderboard_create( "reference_code": template_content.decode("utf-8"), }) - await interaction.response.send_message( - f"Leaderboard '{leaderboard_name}' created.\n" - + f"Reference code: {reference_code}.\n" - + f"Submission deadline: {date_value}", - ephemeral=True, - ) + if interaction.response.is_done(): + await interaction.followup.send( + f"Leaderboard '{leaderboard_name}' created.\n" + + f"Reference code: {reference_code}.\n" + + f"Submission deadline: {date_value}", + ephemeral=True, + ) + else: + await interaction.response.send_message( + f"Leaderboard '{leaderboard_name}' created.\n" + + f"Reference code: {reference_code}.\n" + + f"Submission deadline: {date_value}", + ephemeral=True, + ) except ValueError: await interaction.response.send_message( "Invalid date format. Please use YYYY-MM-DD or YYYY-MM-DD HH:MM", From 16af11f3ea7b91efeba7030b32c24ae259e2a15a Mon Sep 17 00:00:00 2001 From: Alex Zhang Date: Sun, 15 Dec 2024 21:28:32 -0800 Subject: [PATCH 2/2] fix: ruff checks --- src/discord-cluster-manager/cogs/leaderboard_cog.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/discord-cluster-manager/cogs/leaderboard_cog.py b/src/discord-cluster-manager/cogs/leaderboard_cog.py index 8836b61..d9a8896 100644 --- a/src/discord-cluster-manager/cogs/leaderboard_cog.py +++ b/src/discord-cluster-manager/cogs/leaderboard_cog.py @@ -2,9 +2,8 @@ from datetime import datetime import discord -from discord import ui, SelectOption, Interaction from consts import GitHubGPU, ModalGPU -from discord import app_commands +from discord import Interaction, SelectOption, app_commands, ui from discord.ext import commands from utils import extract_score, get_user_from_id