簡體   English   中英

無法在 discord.py 中使用反應命令編輯嵌入消息

[英]Cannot edit embed message with reaction command in discord.py

我已經在 discord 機器人上工作了一段時間,現在想要讓它計算反應/投票的平均值並將其更改為消息頁腳。 我已經完成了所有其他工作,但是我似乎無法弄清楚如何編輯消息。

代碼簡化。

#imports

intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.dm_messages = True

#bot client
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'Logged in as {client.user}')

divider=0
sumofnumbers=0
average=0

@client.event
async def on_message(message):
      if message.content.startswith('!viikonlista'):
        embed=discord.Embed(color=0x76b52f)
        embed.add_field(name="Vote now!", value="Votes appear on the footer")
        embed.set_footer(text=average)
        msg=await message.channel.send("Here's the embed!",embed=embed)
        await msg.add_reaction("1️⃣")
        await msg.add_reaction("2️⃣")
        await msg.add_reaction("3️⃣")
        await msg.add_reaction("4️⃣")
        await msg.add_reaction("5️⃣")


@client.event
async def on_reaction_add(reaction, user):
    global divider
    global sumofnumbers
    global average
    if reaction.emoji == "1️⃣" and not user.bot:
      divider+=1
      sumofnumbers+=1
      average=(sumofnumbers)/divider
      embed.set_footer(text=average)
      msg.edit(embed=embed)
      print(average)
#same for the other numbers

#same but minus

client.run("token")

每當我做出反應時,都會出現以下錯誤:

NameError: name 'embed' is not defined

錯誤消息幾乎說明了這里的問題。 embed未在 on_reaction_add function 的on_reaction_add中定義。

@client.event
async def on_reaction_add(reaction, user):
    global divider
    global sumofnumbers
    global average
    if reaction.emoji == "1️⃣" and not user.bot:
        divider += 1
        summat += 1
        keskiarvo = (sumofnumbers) / divider
        embeds = reaction.message.embeds
        if embeds:
            embed = embeds[0]
        else:
            # no embed
            return

        embed.set_footer(text=average)
        msg.edit(embed=embed)
        print(average)

您可以通過reaction.message屬性查看消息 object 上的embeds屬性來獲取消息嵌入。

或者,或者,在此處創建嵌入的新實例,然后執行您想執行的操作。

文檔:

暫無
暫無

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

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