Search code examples
pythonpycord

How to edit an embed using Message ID, (Pycord)


I'm trying to edit an embed in another channel for example Rules channel...

The uprules command is meant to add a field to the previous embed without making another embed.

I'm using Pycord latest version. Things I've tried are below.

Code:

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"```css\n[{number}] {text}\n```",
            inline=False
        )

        await msg.edit(embed=self.Rules)

def setup(client):
    client.add_cog(Rules(client))

Error:

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

What I've looked at: https://docs.pycord.dev/en/master/api.html?highlight=message%20id#discord.Message.id

That's what I've tried. I'm thinking I could edit the embed using the messages ID but I don't know how exactly. Any help is appreciated.

Thank you.


Solution

  • I think that this method just doesn't work. I never saw it working before and can't find an entry for it in the documentation.

    You need to fetch the message directly with an await call in order to edit it.

    Like this:

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

    And for the embed error, you should move the variable to the __init__ function, in order to use it everywhere without blocking. Like this:

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

    After that, can you use the variable self.Rules for the required Embed.


    Sources