简体   繁体   English

如何使用 nextcord python 在回调 function 中调用 function

[英]How call a function in a callback function with nextcord python

@bot.slash_command(guild_ids=[1061756784567656448])
async def oui(ctx):
    button = Button(label="A + 1", style=ButtonStyle.blurple)
    myview = View(timeout=180)
    myview.add_item(button)
    
    
    async def aurevoir(ctx):
        await ctx.send_message("aurevoir")
        
    
    async def bonjour(interaction: discord.Interaction):
        await interaction.response.send_message("bonjour")
        await aurevoir(ctx)
        
    button = Button(label="A + 1", style=ButtonStyle.blurple)
    myview = View(timeout=180)
    myview.add_item(button)
        
    
    button.callback = bonjour
    await ctx.send(f"hello",view= myview)

The function "aurevoir" never sends the message, and I got a lot of errors like 'Context' object has no attribute 'send_message'. function“aurevoir”从不发送消息,我收到很多错误,例如“上下文”object 没有属性“send_message”。 I don't know how to fix it.我不知道如何解决它。

The issue is that you were passing the ctx object through - it'd be better to use the interaction object that bonjour gets when you press the button and using the followup property on the interaction to send the message.问题是您正在传递ctx object - 最好使用当您按下按钮时bonjour获得的交互 object 并使用交互上的followup属性发送消息。

async def oui(ctx: discord.ApplicationContext):
    await ctx.defer()
    button = Button(label="A + 1", style=ButtonStyle.blurple)
    myview = View(timeout=180)
    myview.add_item(button)
    
    async def aurevoir(interaction: discord.Interaction):
        await interaction.followup.send("aurevoir")
        
    async def bonjour(interaction: discord.Interaction):
        await interaction.response.send_message("bonjour")
        await aurevoir(interaction)
        
    button = Button(label="A + 1", style=ButtonStyle.blurple)
    myview = View(timeout=180)
    myview.add_item(button)
        
    button.callback = bonjour
    await ctx.followup.send("hello", view=myview)

I also added a ctx.defer and a ctx.followup.send .我还添加了一个ctx.defer和一个ctx.followup.send

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM