EN VI

Python - Two choices with the same name but different value both result in same value (discord.py)?

2024-03-10 04:30:04
How to Python - Two choices with the same name but different value both result in same value (discord.py)
choices = []
choices.append(app_commands.Choice(name="Foo", value=1))
choices.append(app_commands.Choice(name="Foo", value=2))

@bot.tree.command(name="abc", description="123")
@app_commands.choices(myarg=choices)
async def abc(ctx: discord.Interaction, myarg: int):
     assert(myarg==1) # will always be triggered

Two choices will appear with the same visual name, but both will yield one, or I assume whatever is added first. Changing the name of one or both choices will resolve this issue, but I need two choices with the same name.

I am expecting two options with the same name but different values.

Solution:

Discord's interface requires each choice's name to be unique within a given command option because it uses the name to display the options to the user. When you have multiple choices with the same name, Discord has no way to display them as distinct options, leading to confusion or unintended command behavior. So there is no way to achieve what you require other than changing the name, something like:

"Foo (Option 1)" and "Foo (Option 2)" OR "Foo - 1" and "Foo - 2"

Also there are other ways like adding some not visible characters to the options, which I guess is more like what you want to achieve.

Here are some options you can try:

  • Zero Width Space (ZWSP): A zero-width space (U+200B) is a non-printing character used in text to indicate word boundaries to text processing systems when using scripts that do not use explicit spacing. It has no width and is not visible.

  • Zero Width Non-Joiner (ZWNJ): The zero-width non-joiner (U+200C) is a non-printing character used in some cursive scripts to prevent the connection of letters that would normally connect. Like ZWSP, it is not visible.

  • Zero Width Joiner (ZWJ): The zero-width joiner (U+200D) is used to display characters as a single glyph or to join two emojis into one. Although its primary purpose is not for invisibility, it has no visible width on its own.

  • Hair Space: A hair space (U+200A) is a very thin space used for fine typography. It's typically narrower than a thin space.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login