更新 Discord 中已存在的嵌入消息

Posted

技术标签:

【中文标题】更新 Discord 中已存在的嵌入消息【英文标题】:Update an embed message that already exists in Discord 【发布时间】:2021-10-16 21:02:32 【问题描述】:

我想更新我的机器人已经在特定频道中写入的嵌入消息,我该怎么做?如果我尝试使用 message.edit 它会出现以下错误:discord.errors.Forbidden: 403 Forbidden (error code: 50005): Cannot edit a message authored by another user

import discord
import json
from discord.ext import commands

def write_json(data,filename="handle.json"):
    with open (filename, "w") as f:
        json.dump(data,f,indent=4)

class Handle(commands.Cog):
    def __init__(self, client):
        self.client=client

    @commands.Cog.listener()
    async def on_ready(self):
        print("Modulo_Handle: ON")

    @commands.command()
    async def Handlers(self,ctx):
            with  open('handle.json','r') as file:
                data = json.load(file)
                embed = discord.Embed.from_dict(data)
                await ctx.channel.send(embed=embed)
                file.close()

    @commands.Cog.listener()
    async def on_message(self, message):    
        if str(message.channel) == "solaris™-handle":
            author = message.author
            content = message.content
            user = str(author)
            with open ("handle.json") as file:
                data = json.load(file)
                temp = data['fields']
                y="name":user[:-5],"value":content
                temp.append(y)
                write_json(data)
                updated_embed = discord.Embed.from_dict(data)
                await message.edit(embed=updated_embed)
                file.close()
            

def setup(client):
    client.add_cog(Handle(client))

【问题讨论】:

这能回答你的问题吗? Embed message doesn't update 【参考方案1】:
msg_id = 875774528062640149
channel = self.client.get_channel(875764672639422555)
msg = await channel.fetch_message(msg_id)
with  open('handle.json','r') as file:
    data = json.load(file)
    embed = discord.Embed.from_dict(data)
await msg.edit(embed=embed)

修复了在write_json(data) 下方添加此代码并删除 updated_embed = discord.Embed.from_dict(data)await message.edit(embed=updated_embed)

【讨论】:

以上是关于更新 Discord 中已存在的嵌入消息的主要内容,如果未能解决你的问题,请参考以下文章

Discord.js 机器人的代码响应错误“TypeError:”

使用新生成的图像自动更新/编辑嵌入消息 - Discord.js

Discord.js 使用来自其他消息的附件来更新嵌入中的图像

自动更新嵌入 (discord.js)

如何检查消息是不是存在? (discord.js)

discord.js 如何编辑/更新嵌入?