why, discord py sending PM with send_message erroring

Question:

i’ve been trying for hours to get a fix for this issue from looking and hundreds of others overflow posts to documentation forums etc i cannot figure this out i’m trying to send an embed as a PM to the user specified in my function everything works except the part where it should send the private message, please help

error

bot has no attribute send_message
bot has no attribute send

code bellow

@bot.command()
@commands.has_role("mod")
async def mute(ctx, user: discord.Member, duration_arg: str = "30", unit: str = "s", reason: str = "null"):
    await ctx.channel.purge(limit=1)
    moderator = ctx.message.author
    #await ctx.invoke(bot.get_command('ask.{user}'.format(use), length=duration_arg))
    channel = bot.get_channel(1060922421491793981)
    duration = int(duration_arg)
    role = discord.utils.get(ctx.message.guild.roles, id=1060693595964842165)
    units = " Null"
    if unit == "s":
        units = " seconds"
    elif unit == "m":
        units = " minutes"
    else:
        return
    length = f"{duration_arg}{units}"

    
    if unit == "s":
        wait = 1 * duration
        await asyncio.sleep(wait)
    elif unit == "m":
        wait = 60 * duration
        
    await ctx.send(f":mute: Muted {user} for {duration}{units}", delete_after=wait)
    await user.add_roles(role)
        #button embed---------
    view = Confirm()
    #await ctx.send('you have been **Muted**', view=view, delete_after=45)
    embedVar = discord.Embed(title="You have been Muted",description=f"this message will expire in **{duration_arg}** {units}",color=0x0ff0000)
    embedVar.add_field(name="Temporary Suspension for:", value=f"{user.mention}",inline=False)
    embedVar.add_field(name="Durration:", value=f"{length}", inline=False)
    embedVar.add_field(name="Reason:", value=f"{reason}", inline=False)
    embedVar.add_field(name="Moderator:", value=f"{moderator.display_name}", inline=False)
    embedVar.add_field(name="Summary",value=f"you can confirm this in the next {duration} {units}nn **if you believe this is in error appeal it and a moderator will review your temporary suspension**",inline=False)
    embedVar.set_footer(text='Booty Police | mute management',icon_url="http://canvaswrite.com/PI/pybot/attachments/server-icon-full.png")
    await bot.send_message(user.id, embed=embedVar, view=view, delete_after=wait)
    # Wait for the View to stop listening for input...
    await view.wait()
    if view.value is None:
        print('Timed out...')
    elif view.value:
        print('appealing...')
    elif view.value is appeal:
        print('Appealing...')
        channel.send("this is a test to appeal...")
    else:
        print('Cancelled...')
    #end buttons----------
    if unit == "s":
        wait = 1 * duration
        await asyncio.sleep(wait)
    elif unit == "m":
        wait = 60 * duration
        await asyncio.sleep(wait)
    await user.remove_roles(role)
    await ctx.send(f":sound: {user} was unmuted",delete_after=15)

the value for user is defined in the command run as the user being muted not the author of the command e.g. ??mute user#1234 30 s "reason"

Asked By: newtboot

||

Answers:

You are sending the embed to the bot it self!

await bot.send_message(...)

Try:

await user.send_message(...)

If this doesn’t work please share the error!

Answered By: iRyan23

As the other answer pointed out, you’re indeed sending it to yourself instead of the user. However, the solution in that answer is still wrong.

User.send_message() is not a thing. You’re using a function that doesn’t exist, and you’re surprised that it errors. The error message that you didn’t include in your post should also tell you that it doesn’t exist.

To send something to a channel or a user, use Messageable.send().

Keep in mind that people can close their DMs to prevent you from sending them anything, which can throw another error for you, and mass-DMing will get your bot flagged for spam.

Answered By: stijndcl
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.