Pycord gives this error: NotFound: 404 Not Found (error code: 10008): Unknown Message

Pycord gives this error: NotFound: 404 Not Found (error code: 10008): Unknown Message

Pycord 版本: 2.0.0b4

代码:

import discord
import os
import random
import asyncio

testing_servers = [912361242985918464]
intents = discord.Intents().all()
bot = discord.Bot(intents=intents)

@bot.event
async def on_ready():
    print('Online!')

@bot.slash_command(guild_ids=testing_servers, name="announce", description="Make server announcements!")
async def announce(ctx,channel_id : discord.Option(str, description = "Copy the text channel in developer mode."),title:str,text : str):
    #response embed
  try:
    channel = bot.get_channel(int(channel_id))
  except ValueError:
    channel = channel_id
    #announcement embed
  embed_check = discord.Embed(
    colour = discord.Colour.blue(),
    title = "Is this embed shown correct?",
    description = title + "\n" * 2 + text
    
  )
  
  message = await ctx.respond(embed = embed_check)
  print(message.id)
  print(ctx.channel)
  global message_react
  message_react = await ctx.channel.fetch_message(message.id)
  print(message_react)
  message_react.add_reaction("✅")


  embed_announce = discord.Embed(
      colour = discord.Colour.blue(),
      title=str(title),
      description = text
  )
  await channel.send(embed = embed_announce)

  embed = discord.Embed(
      colour=discord.Colour.blue(),
      title = "Sent!",
      description= "Check the channel!"
  )

  await ctx.send(embed = embed)

完整错误:

Traceback (most recent call last):   
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/bot.py",
 line 520, in process_application_commands
     await ctx.command.invoke(ctx)   
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/commands/core.py",
 line 306, in invoke
     await injected(ctx)   
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/commands/core.py",
 line 116, in wrapped
     raise ApplicationCommandInvokeError(exc) from exc discord.commands.errors.ApplicationCommandInvokeError: Application
 Command raised an exception: NotFound: 404 Not Found (error code:
 10008): Unknown Message

我想做的事情:

我想制作一个发送消息的机器人,然后在使用斜杠命令时向该消息添加两个反应。我试图获取消息并添加反应,但行 message = await ctx.channel.fetch_message(message.id) 产生了错误。 如何获取在pycord中添加反应的具体消息?

首先,您已经有了正确的解决方案来修复它,只是在错误的地方。将 try-catch 移动到您定义 message 的部分 - 这可能 return 这个错误。您可以提醒自己,消息提取是直接 API 调用,可能会 运行 此类错误。

而且 bot.get_channel 从来没有 return 这样的错误。如果机器人找不到具有所需 ID 的频道,将 NoneType 编辑为 return。所以你应该在这里检查频道是否是 None.

# Get channel object

channel = bot.get_channel(int(channel_id))
if channel is None:
   channel = channel_id
   #announcement embed
else:
   embed_check = discord.Embed(colour = discord.Colour.blue(),
                               title = "Is this embed shown correct?",
                               description = title + "\n" * 2 + text)

# Get message object

message = await ctx.respond(embed = embed_check)
try:
   message = await ctx.channel.fetch_message(message.id)
except discord.NotFound:
   return await ctx.respond('message not found')

来源

import discord
import os
import random
import asyncio

testing_servers = [912361242985918464]
intents = discord.Intents().all()
bot = discord.Bot(intents=intents)

@bot.event
async def on_ready():
    print('Online!')

@bot.slash_command(guild_ids=testing_servers, name="announce", description="Make server announcements!")
async def announce(ctx,channel_id : discord.Option(str, description = "Copy the text channel in developer mode."),title:str,text : str):
    #response embed
  try:
    channel = bot.get_channel(int(channel_id))
  except ValueError:
    channel = channel_id
    #announcement embed
  embed_check = discord.Embed(
    colour = discord.Colour.blue(),
    title = "Is this embed shown correct?",
    description = title + "\n" * 2 + text
    
  )
  
  response = await ctx.respond(embed = embed_check)
  print(ctx.channel)
  global message_react
  message_react = await response.original_message()
  print(message_react)
  await message_react.add_reaction("✅")


  embed_announce = discord.Embed(
      colour = discord.Colour.blue(),
      title=str(title),
      description = text
  )
  await channel.send(embed = embed_announce)

  embed = discord.Embed(
      colour=discord.Colour.blue(),
      title = "Sent!",
      description= "Check the channel!"
  )

  await ctx.send(embed = embed)

这应该可以解决问题。你应该知道,如果你要添加一个 ctx.response.defer() 这将不再有效,因为 ctx.respond returns 一个 WebhookMes​​sage 而不是一个你不能添加表情符号的交互.