运行命令 Discord.py 时"AttributeError: 'Context' object has no attribute 'user'"



我正在通道中使用代码进行回溯。该命令应该向用户发送我选择的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}.nnRegards,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```fixn{repr(jsonError)}n```nRecommended fixes: **enable your DMs if you haven't**.")

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

首先,这是一个自我解释的错误。第二件事是你没有看过文件。

基本上,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}.nnRegards,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```fixn{repr(jsonError)}n```nRecommended fixes: **enable your DMs if you haven't**.")

试试这个:

@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}.nnRegards,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```fixn{repr(jsonError)}n```nRecommended fixes: **enable your DMs if you haven't**.")

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

相关内容

最新更新