modmail not working AttributeError: 'ClientUser' object has no attribute 'send'

Viewed 258

so i was making a discord bot and made a modmail system and got this error. can someone plz help and tell me where i am going wrong i searched many similar errors but there it was showing due to send_message which was changed in discord.py rewrite.

Error:

Ignoring exception in on_message
Traceback (most recent call last):
File "C:\\discord bot\venv\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\\discord bot\cogs\modmail.py", line 35, in on_message
msg = await message.author.send(embed=embed)
AttributeError: 'ClientUser' object has no attribute 'send'

My code:

import discord
import asyncio
from discord.ext.commands import Cog
client = discord.Client()
sent_users = []
class modmail(Cog):

    def __init__(self, client):
        self.client = client

    @Cog.listener()
    async def on_ready(self):
            print("modmail ready")


    @Cog.listener()
    async def on_message(self,message):
        if message.guild:  # ensure the channel is a DM
            return

        if message.author == client.user:
            return

        if message.author.id in sent_users:  # Ensure the intial message hasn't been sent before
            return

        modmail_channel = discord.utils.get(client.get_all_channels(), name="modmail")

        embed = discord.Embed(color=0x00FFFF)
        embed.set_author(name=f"Olympia Gaming Modmail System",
                         icon_url="https://cdn.discordapp.com/icons/690937143522099220/34fbd058360c3d4696848592ff1c5191.webp?size=1024")
        embed.add_field(name='Report a member:', value=f"React with 1️⃣ if you want to report a member.")
        embed.add_field(name='Report a Staff Member:', value=f"React with 2️⃣ if you want to report a Staff Member.")
        embed.add_field(name='Warn Appeal:', value=f"React with 3️⃣ if you would like to appeal a warning.")
        embed.add_field(name='Question:',
                        value=f"React with 4️⃣ if you have a question about our moderation system or the server rules.")
        embed.set_footer(text="Olympia Gaming | Modmail")
        msg = await message.author.send(embed=embed)
        await msg.add_reaction("1️⃣")
        await msg.add_reaction("2️⃣")
        await msg.add_reaction("3️⃣")
        await msg.add_reaction("4️⃣")

        sent_users.append(message.author.id)  # add this user to the list of sent users

        try:
            def check(reaction, user):
                return user == message.author and str(reaction.emoji) in ["1️⃣", "2️⃣", "3️⃣", "4️⃣"]

            reaction, user = await client.wait_for("reaction_add", timeout=60, check=check)

            if str(reaction.emoji) == "1️⃣":
                embed = discord.Embed(color=0x00FFFF)
                embed.set_author(name=f"Olympia Gaming Modmail System",
                                 icon_url="https://cdn.discordapp.com/icons/690937143522099220/34fbd058360c3d4696848592ff1c5191.webp?size=1024")
                embed.add_field(name='How to Report:',
                                value="Send the ID of the person you are reporting and attach add a screen shot of them breaking a rule (can be ToS or a server rule).")
                embed.set_footer(text="Olympia Gaming | Report a member ")
                await message.author.send(embed=embed)

                message = await client.wait_for("message", timeout=60, check=lambda
                    m: m.channel == message.channel and m.author == message.author)
                embed = discord.Embed(title=f"{message.content}", color=0x00FFFF)
                await modmail_channel.send(embed=embed)


        except asyncio.TimeoutError:
            await message.delete()



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

https://cdn.discordapp.com/attachments/926071479706222622/926393218713649172/game_bot_-_Google_Chrome_12_31_2021_2_13_59_PM_LI.jpg

2 Answers

You are attempting to send a message to the bot itself (which it cannot do) this is because you have not correctly ignored yourself:

# INCORRECT
if message.author == client.user:
    return

client here is not the same as your bot instance as it this is referencing a different instance defined on line 4 (client = discord.Client()). Instead what you want to do is ignore your instance via the use of self because you are passing in the bot instance via the __init__ function, so it would be:

if message.author == self.client.user:
    return

You should also remove that client instance on line 4 as you may get confused with the actual bot instance (self.client)

EDIT: This can be used for responding to the message in the same channel.

await message.channel.send(embed=embed)

The below code is only valid if message.author returns a User or Member. The ClientUser model doesn't have dm_channel attribute or a send() method.


You need to specify that you want to send the message to the user's DM channel

https://discordpy.readthedocs.io/en/stable/api.html#discord.User.dm_channel

await message.author.dm_channel.send(embed=embed)
Related