Search code examples
pythonpycord

Can't get message from message response


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

Solution

  • 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.