如何使用消息ID(Pycord)编辑嵌入



我正在尝试编辑另一个频道中的嵌入,例如规则频道。。。

uprules命令旨在向上一个嵌入添加一个字段,而不进行另一个嵌入。

我使用的是Pycord的最新版本。下面是我尝试过的东西。

代码:

import discord
from discord.ext import commands
class Rules(commands.Cog):
def __init__(self, client):
self.client = client
self.Rules = discord.Embed(
title = "__**Test Embed Rules**__",
description = """
Rules
"""
)
@commands.command()
@commands.is_owner()
async def rules(self, ctx):
channel = self.client.get_channel(954750221261373471)
await channel.send(embed=self.Rules)
@commands.command()
async def uprules(self, ctx, number, *, text):
channel = self.client.get_channel(954750221261373471)
msg = await channel.fetch_message(955501538766381066)
Rules.add_field(
name="||Test Embed||n",
value=f"```cssn[{number}] {text}n```",
inline=False
)
await msg.edit(embed=self.Rules)
def setup(client):
client.add_cog(Rules(client))

错误:

Command raised an exception: AttributeError: type object 'Rules' has no attribute 'add_field'

我所看到的:https://docs.pycord.dev/en/master/api.html?highlight=message%20id#discord.Message.id

这就是我尝试过的。我想我可以使用消息ID编辑嵌入,但我不知道具体如何。感谢您的帮助。

谢谢。

我认为这个方法不起作用。我以前从未见过它工作,在文档中也找不到它的条目。

您需要通过await调用直接获取消息才能对其进行编辑

像这样:

channel = self.client.get_channel(954750221261373471)
msg = await channel.fetch_message(5389540224354334)

对于嵌入错误,您应该将变量移动到__init__函数,以便在任何地方使用它而不阻塞。像这样:

def __init__(self, client):
self.client = client
self.Rules = discord.Embed(
title = "__**Test Embed Rules**__",
description = "")

之后,您可以使用变量self.Rules进行所需的嵌入吗。


来源

  • https://discordpy.readthedocs.io/en/latest/api.html?highlight=#discord.TextChannel.fetch_message

最新更新