Discord.py,有什么方法可以编辑通过 webhook 发送的消息吗?
Discord.py, there is any method do edit a message send by a webhook?
我目前正在制作一个建议系统,但我有一个问题,我不知道如何编辑(嵌入)通过 webhook 发送的消息,这是我的代码:
async def suggestion(ctx):
async with aiohttp.ClientSession() as session:
webhook = Webhook.from_url('...', adapter=AsyncWebhookAdapter(session))
embed = discord.Embed(color=color)
embed.add_field(name="Nouvelle suggestion !", value=ctx.message.content.lstrip(f"{prefix}suggest"))
embed.set_footer(text=f"""{ctx.message.author} • {datetime.now().strftime("%d %b %Y %H:%M:%S")}""",
icon_url=str(ctx.message.author.avatar_url))
await webhook.send(embed=embed, username=ctx.author.name, avatar_url=ctx.author.avatar_url)
用于添加反应:
@bot.event
async def on_message(message):
if message.channel.id == 650397901893140481 and message.author.id not in [296687703851008002, 639138362673987584, 632575363830120448]:
await message.add_reaction("<:yes:710947936372129854>")
await message.add_reaction("<:neutral:710949706296983603>")
await message.add_reaction("<:no:710947944823652362>")
用于编辑消息:
@bot.command()
async def test(ctx, *args):
message = await ctx.channel.fetch_message(args[0])
embed = discord.Embed(color=color)
embed.add_field(name="Nouvelle suggestion !", value=args[1])
await message.edit(embed=embed)
错误:discord.ext.commands.errors.CommandInvokeError:命令引发异常:禁止:403 禁止(错误代码:50005):无法编辑由其他用户撰写的消息
对于我之前回答中的错误信息,我们深表歉意。
Webhook 无法编辑消息 - 它们只能发送消息。这意味着如果您想给人一种您已编辑消息的错觉,则需要删除消息并使用新内容重新发送它们。
参考文献:
discord.Webhook()
- Discord docs - webhook - 具体说他们是 "low-effort way to post messages to channels in Discord",没有任何地方提到能够编辑消息。
- Discord docs - editing messages
也许在您提出问题时这是不可能的,但现在可以了。
因此,作为参考,我在 Ruby 中做了一个例子,没有可用的 Python 例子,但它会很相似。最重要的部分是构建 json 并通过 API.
发送它
require 'discordrb/webhooks'
SECONDEN = ARGV[0] || 3
WEBHOOK_URL = "your discord webhook url"
WAIT = true
client = Discordrb::Webhooks::Client.new(url: WEBHOOK_URL)
@my_builder = Discordrb::Webhooks::Builder.new
@my_builder.content = "test"
@my_builder.username = 'discordrb' # you can use whatever
message = client.execute(@my_builder, WAIT).body
message_id = JSON.parse(message)["id"]
while true
sleep SECONDEN
@my_builder.content = Time.now().strftime("%H:%M:%S")
RestClient.patch("#{WEBHOOK_URL}/messages/#{message_id}", @my_builder.to_json_hash.to_json, content_type: :json)
end
我目前正在制作一个建议系统,但我有一个问题,我不知道如何编辑(嵌入)通过 webhook 发送的消息,这是我的代码:
async def suggestion(ctx):
async with aiohttp.ClientSession() as session:
webhook = Webhook.from_url('...', adapter=AsyncWebhookAdapter(session))
embed = discord.Embed(color=color)
embed.add_field(name="Nouvelle suggestion !", value=ctx.message.content.lstrip(f"{prefix}suggest"))
embed.set_footer(text=f"""{ctx.message.author} • {datetime.now().strftime("%d %b %Y %H:%M:%S")}""",
icon_url=str(ctx.message.author.avatar_url))
await webhook.send(embed=embed, username=ctx.author.name, avatar_url=ctx.author.avatar_url)
用于添加反应:
@bot.event
async def on_message(message):
if message.channel.id == 650397901893140481 and message.author.id not in [296687703851008002, 639138362673987584, 632575363830120448]:
await message.add_reaction("<:yes:710947936372129854>")
await message.add_reaction("<:neutral:710949706296983603>")
await message.add_reaction("<:no:710947944823652362>")
用于编辑消息:
@bot.command()
async def test(ctx, *args):
message = await ctx.channel.fetch_message(args[0])
embed = discord.Embed(color=color)
embed.add_field(name="Nouvelle suggestion !", value=args[1])
await message.edit(embed=embed)
错误:discord.ext.commands.errors.CommandInvokeError:命令引发异常:禁止:403 禁止(错误代码:50005):无法编辑由其他用户撰写的消息
对于我之前回答中的错误信息,我们深表歉意。
Webhook 无法编辑消息 - 它们只能发送消息。这意味着如果您想给人一种您已编辑消息的错觉,则需要删除消息并使用新内容重新发送它们。
参考文献:
discord.Webhook()
- Discord docs - webhook - 具体说他们是 "low-effort way to post messages to channels in Discord",没有任何地方提到能够编辑消息。
- Discord docs - editing messages
也许在您提出问题时这是不可能的,但现在可以了。 因此,作为参考,我在 Ruby 中做了一个例子,没有可用的 Python 例子,但它会很相似。最重要的部分是构建 json 并通过 API.
发送它require 'discordrb/webhooks'
SECONDEN = ARGV[0] || 3
WEBHOOK_URL = "your discord webhook url"
WAIT = true
client = Discordrb::Webhooks::Client.new(url: WEBHOOK_URL)
@my_builder = Discordrb::Webhooks::Builder.new
@my_builder.content = "test"
@my_builder.username = 'discordrb' # you can use whatever
message = client.execute(@my_builder, WAIT).body
message_id = JSON.parse(message)["id"]
while true
sleep SECONDEN
@my_builder.content = Time.now().strftime("%H:%M:%S")
RestClient.patch("#{WEBHOOK_URL}/messages/#{message_id}", @my_builder.to_json_hash.to_json, content_type: :json)
end