Log embed thinks it's empty (on_message_edit) - python

To start with, I'm trying to make my bot log message editing. Basically that code works and logs messages that were edited, but it causes some problems in the terminal:
discord.errors.HTTPException: 400 BAD REQUEST (error code: 50035): Invalid Form Body
In embed.fields.0.value: This field is required
In embed.fields.1.value: This field is required
Here's my code:
#client.event
async def on_message_edit(before, after):
editembed = discord.Embed(
timestamp=after.created_at,
description = f"<#!{before.author.id}>**'s message was edited in** <#{before.channel.id}>.",
colour = discord.Colour(0x00FF00)
)
editembed.set_author(name=f'{before.author.name}#{before.author.discriminator}', icon_url=before.author.avatar_url)
editembed.set_footer(text=f"Author ID:{before.author.id} • Message ID: {before.id}")
editembed.add_field(name='Before:', value=before.content, inline=False)
editembed.add_field(name="After:", value=after.content, inline=False)
channel = client.get_channel(665307525897519105)
await channel.send(embed=editembed)
I can't understand why it decides that the value is empty. Can someone help to fix these errors?

I had the same problem but I searched around on google for some time and found a solution which was by adding + "\u200b" after both value=before.content and value=after.content. I also used embed instead of editembed. So try this:
#bot.event
async def on_message_edit(before, after):
embed = discord.Embed(
timestamp=after.created_at,
description = f"<#!{before.author.id}>**'s message was edited in** <#{before.channel.id}>.",
colour = discord.Colour(0x00FF00)
)
embed.set_author(name=f'{before.author.name}#{before.author.discriminator}', icon_url=before.author.avatar_url)
embed.set_footer(text=f"Author ID:{before.author.id} • Message ID: {before.id}")
embed.add_field(name='Before:', value=before.content + "\u200b", inline=False)
embed.add_field(name="After:", value=after.content + "\u200b", inline=False)
channel = bot.get_channel(665307525897519105)
await channel.send(embed=embed)
Link to the github page that helped me: https://github.com/Rapptz/discord.py/issues/643

You are using the wrong variables try this:
#bot.event
async def on_message_edit(before, after):
embed = discord.Embed(
timestamp=after.created_at,
description = f"<#!{before.author.id}>**'s message was edited in** <#{before.channel.id}>.",
colour = discord.Colour(0x00FF00)
)
embed.set_author(name=f'{before.author.name}#{before.author.discriminator}', icon_url=before.author.avatar_url)
embed.set_footer(text=f"Author ID:{before.author.id} • Message ID: {before.id}")
embed.add_field(name='Before:', value=before.content, inline=False)
embed.add_field(name="After:", value=after.content, inline=False)
channel = bot.get_channel(665307525897519105)
await channel.send(embed=embed)

Related

Discord.py logging edited and deleted messages in a specific channel

I want to have a discord bot for my server that logs deleted messages in a specific channel. I have tried but I'm new to coding this is what I came up with and it doesn't work. The code is shown below:
async def on_message_delete(message):
embed=discord.Embed(title="{} deleted a message".format(member.name.message), description="", color="teal")
embed.add_field(name= message.content ,value="Deleted message", inline=True)
channel=bot.get_channel(channel_id)
await channel.send(embed=embed)
Can anyone tell me what's wrong and the correct code please, it would be greatly appreciated and how to do logging for edited messages too
Take a look to this code:
#bot.event
async def on_message_delete(message):
embed = discord.Embed(
title="{}'s message deleted.".format(message.author.name), #message.author is sender of the message
description=message.content,
color="teal"
)
channel=bot.get_channel(log_channel_id)
await channel.send(embed=embed)

discord.py - adding reaction as part of command

I'm writing a custom help command that sends a embed DM to a user, that all works fine. As part of the command, I'm trying to make the bot react to the command message but I cannot get it to work, I've read the documentation but I still can't seem to get it to react as part of a command. Below is the what I'm trying to achieve:
#client.command(pass_context=True)
async def help(ctx):
# React to the message
author = ctx.message.author
help_e = discord.Embed(
colour=discord.Colour.orange()
)
help_e.set_author(name='The bot\'s prefix is !')
help_e.add_field(name='!latency', value='Displayes the latency of the bot', inline=False)
help_e.add_field(name='!owo, !uwu, !rawr', value='Blursed commands', inline=False)
await author.send(embed=help_e)```
You can use message.add_reaction() and also you can use ctx.message to add reaction to the message. Here is what you can do:
#client.command(pass_context=True)
async def help(ctx):
# React to the message
await ctx.message.add_reaction('✅')
author = ctx.message.author
help_e = discord.Embed(
colour=discord.Colour.orange()
)
help_e.set_author(name='The bot\'s prefix is !')
help_e.add_field(name='!latency', value='Displayes the latency of the bot', inline=False)
help_e.add_field(name='!owo, !uwu, !rawr', value='Blursed commands', inline=False)
sent_embed = await author.send(embed=help_e)

Discord.Py adding reactions to an embeded message

So I am trying to add three different reactions (emojis), to a message that the bot has sent in a text channel.
The user fills in a form in their DM, and the message then get sent into a text-channel called "admin-bug", the admins of the server can then react to three different emojis:
fixed
will not be fixed
not-a-bug
And then, depending on what emoji the admin press on, the message will be transferred to a text channel.
But! I can't seem to figure out how you actually add the reactions to the message itself, I have done a bunch of googling, but can't find the answer.
code:
import discord
from discord.ext import commands
TOKEN = '---'
bot = commands.Bot(command_prefix='!!')
reactions = [":white_check_mark:", ":stop_sign:", ":no_entry_sign:"]
#bot.event
async def on_ready():
print('Bot is ready.')
#bot.command()
async def bug(ctx, desc=None, rep=None):
user = ctx.author
await ctx.author.send('```Please explain the bug```')
responseDesc = await bot.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
description = responseDesc.content
await ctx.author.send('````Please provide pictures/videos of this bug```')
responseRep = await bot.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
replicate = responseRep.content
embed = discord.Embed(title='Bug Report', color=0x00ff00)
embed.add_field(name='Description', value=description, inline=False)
embed.add_field(name='Replicate', value=replicate, inline=True)
embed.add_field(name='Reported By', value=user, inline=True)
adminBug = bot.get_channel(733721953134837861)
await adminBug.send(embed=embed)
# Add 3 reaction (different emojis) here
bot.run(TOKEN)
Messagable.send returns the message it sends. So you can add reactions to it using that message object. Simply, you have to use a variable to define the message you sent by the bot.
embed = discord.Embed(title="Bug report")
embed.add_field(name="Name", value="value")
msg = await adminBug.send(embed=embed)
You can use msg to add reactions to that specific message
await msg.add_reaction("💖")
Read the discord.py documentation for detailed information.
Message.add_reaction
The discord.py docs have an FAQ post about adding reactions, it has multiple exampes and an indepth description, furthermore Messageable.send returns the message send so you can use Message.add_reaction on that. https://discordpy.readthedocs.io/en/neo-docs/faq.html#how-can-i-add-a-reaction-to-a-message
You need to save the embed as a variable, in this way you can add a reaction.
message = await adminBug.send(embed=embed) # save embed as "message"
await message.add_reaction('xxx') # add reaction to "message"
I'm not sure because I use nextcord (and it worked), but I think this can work :
#bot.command
async def testembed(ctx):
embed = discord.Embed(title='Test !', description='This is a test embed !')
msg = await ctx.send("", embed=embed)
msg = msg.fetch() # Notice this line ! It's important !
await msg.add_reaction('emoji_id')

How do I get the message class using the message's ID? [duplicate]

#client.command()
async def edit(ctx, message_id, *, new_message) :
channel, chan, guild = None, list_channels("saved_channels.csv"), ctx.guild.id
message = await fetch_message(message_id)
await message.edit(new_message)
embed=discord.Embed(color=0x00ca1f)
embed.add_field(name="Success :white_check_mark:", value=f"Le message a bien été édité dans {message.channel.mention}", inline=False)
await ctx.send(embed = embed)
I'm trying to edit a specific message my bot sent in another channel by taking the message id with fetch_message() (supposed to be in the new discord API) but it gives me the NameError: name 'fetch_message' is not defined... is there something I did wrong ?
You need to specify the channel where bot should edit the message from.
For example,
channel = client.get_channel(channel_id)
message = await channel.fetch_message(message_id)
Also, when editing a message you need to specify the content as follows:
await message.edit(content=new_message)

How to retrieve a specific message by ID (tried fetch_message(id))

#client.command()
async def edit(ctx, message_id, *, new_message) :
channel, chan, guild = None, list_channels("saved_channels.csv"), ctx.guild.id
message = await fetch_message(message_id)
await message.edit(new_message)
embed=discord.Embed(color=0x00ca1f)
embed.add_field(name="Success :white_check_mark:", value=f"Le message a bien été édité dans {message.channel.mention}", inline=False)
await ctx.send(embed = embed)
I'm trying to edit a specific message my bot sent in another channel by taking the message id with fetch_message() (supposed to be in the new discord API) but it gives me the NameError: name 'fetch_message' is not defined... is there something I did wrong ?
You need to specify the channel where bot should edit the message from.
For example,
channel = client.get_channel(channel_id)
message = await channel.fetch_message(message_id)
Also, when editing a message you need to specify the content as follows:
await message.edit(content=new_message)

Categories