简体   繁体   中英

'Message' object has no attribute msg

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("⚠")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM