簡體   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)

function“aurevoir”從不發送消息,我收到很多錯誤,例如“上下文”object 沒有屬性“send_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)

我還添加了一個ctx.defer和一個ctx.followup.send

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM