“属性错误:“上下文”对象没有属性“用户”运行命令 Discord.py 时

发布于 2025-01-14 02:54:58 字数 720 浏览 4 评论 0原文

我正在通道中使用我的代码进行回溯。该命令应该将我选择的 dm 发送给用户,但它只是回复我的消息并显示下面的回溯错误!有人可以帮忙吗?

源代码:

@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**.")

回溯:AttributeError("'Context'对象没有属性'user'")

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晨说晚安 2025-01-21 02:54:58

首先,这是一个不言自明的错误。第二件事你没有阅读文档。

基本上,ctx 没有user 对象。现在,如果您想提及/DM 被调用的用户,请使用以下命令:

@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**.")

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**.")
旧梦荧光笔 2025-01-21 02:54:58

试试这个:

@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**.")

您只需使用 user.send 而不是 ctx.user.send,因为它不存在。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文