Reputation: 67
So, I've making some code that will allow me to log edited messages to a certain channel. Here is the code:
async def on_message_edit(message_before, message_after):
embed=discord.Embed(title="{} edited a message".format(message_before, message.author), description="", color=0xFFFF00)
embed.add_field(name= message_before.content ,value="This is the message before the edit:", inline=True)
embed.add_field(name= message_after.content ,value="This is the message after the edit", inline=True)
channel=bot.get_channel(747057143630528563)
await channel.send(embed=embed)
However, when I run the code, I get the error message:
Ignoring exception in on_message_edit Traceback (most recent call last): File "C:\Users\jackt\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 333, in _run_event await coro(*args, **kwargs) File "C:\Users\jackt\Desktop\bot.py", line 92, in on_message_edit embed=discord.Embed(title="{} edited a message".format(message_before, message.author), description="", color=0xFFFF00) NameError: name 'message' is not defined
What part of the code do I need to change, and what do I need to change about it? Thanks.
Upvotes: 0
Views: 674
Reputation: 2145
discord.Embed(title="{} edited a message".format(message_before, message.author), description="", color=0xFFFF00)
should actually be
discord.Embed(title="{} edited a message".format(message_before.author.name), description="", color=0xFFFF00)
The reason you got the message is not defined
error before is because you added a comma here: message_before, message.author
. This makes python think that they're two different statements, making it try to access the message variable (which does not exist).
Instead, you should access the message_before object. I added .name
to retrieve the name as just using .author
would just add a User object. If you want to just mention the user, then you can replace .name
with .mention
.
Edit: Also add @bot.event
at the top of the function so it gets called on a message edit. The code should look like this:
@bot.event
async def on_message_edit(message_before, message_after):
discord.Embed(title="{} edited a message".format(message_before.author.name), description="", color=0xFFFF00)
embed.add_field(name= message_before.content ,value="This is the message before the edit:", inline=True)
embed.add_field(name= message_after.content ,value="This is the message after the edit", inline=True)
channel=bot.get_channel(747057143630528563)
await channel.send(embed=embed)
Upvotes: 1
Reputation: 503
Your using message.author
in the first line of you definition:
embed=discord.Embed(title="{} edited a message".format(message_before, message.author), description="", color=0xFFFF00)
However message
was never declared, hence the "not defined" error your getting
One fix for this could be using:
message_before.author.name
Upvotes: 0