Can't get message from message response

Question:

Pycord version:
2.0.0b4

I want to make a bot that sends a message, then adds two reactions to that message when the slash command is used. I tried to get the message and add a reaction but the line message = await ctx.channel.fetch_message(message.id) produces the error. How do I get the specific message to add the reaction in Pycord?

Code

  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("✅")

I tried to get the message.id from the response, but it gives the following error:

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
Asked By: Blue Robin

||

Answers:

First of all, you already have the right solution to fix it, just in the wrong place. Move the try-catch to the part where you define message – this can return this error. You can remind yourself that message fetching is a direct API call and can run such kinds of errors.

And bot.get_channel never returns such an error. If the bot can’t find the channel with the desired ID, will NoneType be returned. So you should check here if the channel is 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')

Sources

Answered By: Razzer
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)

This should do the trick. You should be aware that if you were to add a ctx.response.defer() this will no longer work, since ctx.respond returns a WebhookMessage instead of an Interaction, which you can’t add emoji to.

Answered By: jab416171
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.