'Message' object has no attribute msg - python

so i was trying to find a way to have a message deleted if it contained bad words (i mean by finding i just copied some code)
#client.event
async def on_message(message):
if message.author.id == client.user.id:
return
msg_content = message.content.lower()
curseWord = ["cursewords i put"]
if any(word in msg_content for word in curseWord):
await message.delete()
msg = "**`Watch your mouth boiii`**"
await message.channel.send(msg)
await message.msg.add_reaction("⚠")
i was trying to add a reaction to the message but i said
'Message' object has no attribute msg

The error probably also points you to the line
await message.msg.add_reaction("⚠")
As the message says message doesn't have an attribute called msg. You can use add_reaction directly on your message object.
await message.add_reaction("⚠")
But message is referring to the message that you deleted, so you don't want to add a reaction to that message. Instead, to add the reaction to the message you just sent you need to get that message object and use it. To get it, when you did message.channel.send the message object was sent back, we just need to save it.
sent_msg = await message.channel.send(msg)
And now we can use it to add a reaction. Altogether that looks like
if any(word in msg_content for word in curseWord):
await message.delete()
msg = "**`Watch your mouth boiii`**"
sent_msg = await message.channel.send(msg)
await sent_msg.add_reaction("⚠")

Related

Allow only a message in a chat discord.py

I'm trying to make a only allowed message in a chat. The code I tried deletes every message even if is the correct one. What am I doing wrong?
#bot.event
async def on_message(message):
if message != '!d bump':
await message.delete()
You're comparing a Message instance to a string, to get the actual content of the message use the content attribute
#bot.event
async def on_message(message):
if message.content != '!d bump':
await message.delete()

Bot don't want to remove member's reaction

I have an authorization system on my server. When member reacts to message, he's getting access to stuff like chats etc. I want bot to remove reaction left by member, so number of reactions on the message will always=1
#client.event
async def on_raw_reaction_add(payload):
message=payload.reaction.message
if payload.channel_id==804320454152028170:
if str(payload.emoji) == '✅':
await message.remove_reaction("✅", payload.member)
else:
return
When I am leaving reaction under the message, I am getting this error:
message=payload.reaction.message
AttributeError: 'RawReactionActionEvent' object has no attribute 'reaction'
Ignoring exception in on_message
Read your error message! Looking at the docs you can easily find out that payload simply does not have such attribute.
To get a message from payload you will need to do something like this
guild = client.get_guild(payload.guild_id)
channel = guild.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)

Remove a message that the bot waited for using discord.py

Basically, im trying to make an autorole bot that takes a message from the user and then deletes the message and updates the role message after. The code I have so far is:
#commands.command()
#commands.has_any_role(786342585479593984, 787715167437324330)
async def rrtest(self, ctx, *, msg):
loop = True
j = "\n"
messagetext = [msg]
rolemessage = await ctx.send(f"```{j.join(messagetext)}```")
message = await ctx.send("Hold on!")
time.sleep(2)
while loop:
await message.edit(content="```Type the role you want to assign \n\nNo need to mention, just type the role name```")
msg = await self.client.wait_for('message', check=self.check(ctx.author), timeout=30)
# ^^^ this is the message i want to delete
what code would i use to delete msg?
Use the method .delete()
await msg.delete()
Also use await asyncio.sleep instead of time.sleep so it doesn't block you whole code.

Create DM on message [discord.py]

I'm trying to make a command that deletes the message content and send it back to the user if it's not the word "role".
Code is working fine. The message gets deleted if it's not "role" and I do received a dm with the deleted message, but I don't know if I'm doing it right because I get this error "AttributeError: 'ClientUser' object has no attribute 'create_dm'".
Here's my code:
#commands.Cog.listener()
async def on_message(self, message):
dm = await message.author.create_dm()
if not message.guild:
return
if not message.channel.name == 'roles':
return
elif message.channel.name == 'roles' and message.content != 'role':
if message.author.bot:
pass
else:
await dm.send(f'{message.content}')
await message.delete()
Discord bots can't send DM's to eachother, only to Users. Your bot is trying to send a DM to itself, which it can't. You need to check with an if-statement if the message's author is a bot or not.
You're already doing this later on in your code, to see if it should send the message or not, but the create_dm() function at the top will have already failed at that point (as it can't create a dm), so it will never get there.
#commands.Cog.listener()
async def on_message(self, message):
if message.author.bot:
return # Ignore bot messages
if not message.guild:
return
if not message.channel.name == 'roles':
return
elif message.content != 'role':
await message.author.send(message.content)
await message.delete()
PS. A few more things to note here:
You can just use User.send() instead of create_dm() & dm.send().
An f-string with nothing but the content can just be the content itself (f"{word}" == word, or str(word) in case it's not a string).
You have an if checking if the channel name is not "roles", and afterwards in the elif you check if it is - if it wouldn't be "roles" then it would've gotten into the first if & returned so the second one is unnecessary.

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')

Categories