1

I'm getting traceback with my code in the channel. The command is supposed to send a dm of my choice to a user, YET it just replies to my message with that traceback error below! Can anyone help?

Source code:

@client.command(aliases=["dm"])
async def DM(ctx, user: discord.User, *, message=None,):
    message = message or "This Message is sent via DM"
    try:
        await ctx.user.send(f"{message}.\n\nRegards,\Real_IceyDev")
        await ctx.channel.send(f"{ctx.user.mention}, check your DMs.")
    except Exception as jsonError:
        await ctx.channel.send(f"{ctx.author.mention}, an error ocurred!\nDeveloper Details:\n```fix\n{repr(jsonError)}\n```\nRecommended fixes: **enable your DMs if you haven't**.")

Traceback: AttributeError("'Context' object has no attribute 'user'")

TheFungusAmongUs
  • 1,423
  • 3
  • 11
  • 28
Real_IceyDev
  • 23
  • 1
  • 3
  • As per the traceback, there's no such thing as `ctx.user`. [Here](https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.Context) are the docs for `Context` – TheFungusAmongUs Mar 15 '22 at 00:13
  • What shall I replace ctx with? Or what shall I do? (Self-taught python coder, dont know much) – Real_IceyDev Mar 15 '22 at 00:38

2 Answers2

0

Try this:

@client.command(aliases=["dm"])
async def DM(ctx, user: discord.User, *, message=None,):
    message = message or "This Message is sent via DM"
    try:
        await user.send(f"{message}.\n\nRegards,\Real_IceyDev")
        await ctx.channel.send(f"{ctx.author.mention}, check your DMs.")
    except Exception as jsonError:
        await ctx.channel.send(f"{ctx.author.mention}, an error ocurred!\nDeveloper Details:\n```fix\n{repr(jsonError)}\n```\nRecommended fixes: **enable your DMs if you haven't**.")

You would just use user.send instead of ctx.user.send because that does not exist.

Bagle
  • 2,326
  • 3
  • 12
  • 36
CRM000
  • 35
  • 10
0

First thing, this is a self-explained error. Second thing you didn't read the docs.

Basically, ctx don't have user object. Now, if you want to mention/DM the invoked user, use this:

@client.command(aliases=["dm"]) #Don't use nornmal command, use / command instead
async def DM(ctx, user: discord.User, *, message=None,):
    message = message or "This Message is sent via DM"
    try:
        await user.send(f"{message}.\n\nRegards,\Real_IceyDev") #DM the user in the command
        await ctx.channel.send(f"{user.mention}, check your DMs.") #Mention the user in the command
    except Exception as jsonError: #Not always error about json but work same so...
        await ctx.channel.send(f"{ctx.author.mention}, an error ocurred!\nDeveloper Details:\n```fix\n{repr(jsonError)}\n```\nRecommended fixes: **enable your DMs if you haven't**.")
BrainFl
  • 127
  • 1
  • 8