I've been building a discord bot with pycord, and so far it's been a very well documented affair. However, when I got to the newer function of Modals, I ran into some trouble. Below is the code for my Modal and the slash command that calls it in the discord server. The modal sends fine and the callback runs fine.
When I try to get the user that filled in the modal, however, I run into trouble.
class MyModal(discord.ui.Modal):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.add_item(discord.ui.InputText(label="Enter your prompt here and click submit.", style = discord.InputTextStyle.long))
async def callback(self, interaction: discord.Interaction): # DO NOT alter the callback name
embed = discord.Embed(title="Modal Results", color=5763719)
embed.add_field(name="Suggested prompt:", value = self.children[0].value)
print(discord.Interaction.user)
# returns "<member 'user' of 'Interaction' objects>" instead of the user attribute that I expected.
# embed.set_author(name = discord.Interaction.user.name) <- error: 'member_descriptor' object has no attribute 'name'
await interaction.response.send_message(embeds=[embed])
@bot.command(description = "Call forth a test Modal.")
async def trymodal(ctx):
modal = MyModal(title = "Modal via Slash Command")
await ctx.send_modal(modal)
I tried calling on discord.Interaction.user, which, according to the API doc should have the attribute 'name', among other things. When I debug and halt the code right after the print, I can indeed see a local variable 'user' in the object discord.interactions.Interaction.
If I try to address the attribute, I get the error 'member_descriptor' object has no attribute 'name'
.
I haven't had formal training (yet), but if I had to guess I'd say it's seeing discord.Interaction.user as a member, and not as the class that it is supposed to be? I read this in the API documentation under the 'user' attribute of the class Interaction: https://docs.pycord.dev/en/stable/api/models.html#discord.Interaction.user
It says Type: Union[User, Member]
. I have no clue what that means? I know User
and Member
are both classes with a lot of overlapping data in them. Does my problem have anything to do with this? Or am I missing a basic class thing?
Bottom line: how can I access the attributes that I know User has?
You're accessing the wrong thing.
discord.Interaction.user
is getting the user property on the Interaction class. Your interaction variable is called interaction
- you need to access the user attribute on that variable.
async def callback(self, interaction: discord.Interaction):
print(interaction.user)
discord.Interaction
is the Object type and is a Class. You even use it as a type hint for the variable in the function definition. Just make sure to access the variable attributes.