Search code examples
pythondiscorddiscord.py

discord.py - TypeError: unsupported type annotation <class 'discord.interactions.Interaction'>


I'm currently writing a Discord Bot and I want people to be able to execute the slash command /hitokoto and have a drop down menu to choose what they need, the code snippet below:

@app_commands.command(name="hitokoto")
@app_commands.choices(choices=[
    app_commands.Choice(name="動畫", value="a"),
    app_commands.Choice(name="漫畫", value="b"),
    app_commands.Choice(name="遊戲", value="c"),
    app_commands.Choice(name="文學", value="d"),
    .......
    ])

async def hitokoto(self,interaction: discord.Interaction, choices: app_commands.Choice):
    url = f"https://v1.hitokoto.cn/?c={choices.value}&encode=json"
    header = {"User-Agent":'Mozilla/5.0'}
    response = requests.get(url, headers = header)
    response.raise_for_status()
    json_hitokoto = response.json()
    if (choices.value == 'a'):
    embed=discord.Embed(title="一言 - 動漫分類", description = json_hitokoto["hitokoto"], color=0x05c5eb)
        embed.add_field(name="上傳者", value=json_hitokoto["creator"] , inline=False)
        embed.add_field(name="來自", value=json_hitokoto["from"], inline=True)
        embed.add_field(name="角色", value=json_hitokoto["from_who"], inline=True)
        await interaction.response.send(embed=embed)
    elif (choices.value == 'b'):
        ......
    elif (choices.value == 'c'):
        ......
  ......
    else:
        return

When I try to execute him, I get the following error, but I don't have any clue......

Traceback (most recent call last):
  File "c:\Users\huang\Desktop\Project\Python\Discord Bot -Lycoris Recoil\main.py", line 69, in <module>
    async def hitokoto(self,interaction: discord.Interaction, choices: app_commands.Choice):
  File "C:\Users\huang\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\app_commands\commands.py", line 2019, in decorator
    return Command(
  File "C:\Users\huang\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\app_commands\commands.py", line 685, in __init__
    self._params: Dict[str, CommandParameter] = _extract_parameters_from_callback(callback, callback.__globals__)
  File "C:\Users\huang\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\app_commands\commands.py", line 393, in _extract_parameters_from_callback
    param = annotation_to_parameter(resolved, parameter)
  File "C:\Users\huang\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\app_commands\transformers.py", line 832, in annotation_to_parameter
    (inner, default, validate_default) = get_supported_annotation(annotation)
  File "C:\Users\huang\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\app_commands\transformers.py", line 791, in get_supported_annotation
    raise TypeError(f'unsupported type annotation {annotation!r}')
TypeError: unsupported type annotation <class 'discord.interactions.Interaction'>

How can I fix it?


Solution

  • The error is caused by

    await interaction.response.send(embed=embed)
    

    Interaction doesn't have a method called send. Its send_message.

    Example:

    await interaction.response.send_message(embed=embed)
    

    Try changing your code to this code and it should work.