我有个问题。我正在制作一个mod邮件机器人,与用户的id一起工作。但是当我使用id时,我会得到以下错误:
raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'send'
这很奇怪,因为经过研究,我发现这是最好的方法。目标是将消息发送给用户。我检查了一下,错误不在user_id
部分,因为这是正确的id。我需要做什么来修复这个问题?
这个命令只是为了测试。这不是的实际命令
这是我的代码:
@bot.command()
async def id(ctx):
# take the id of the user it needs to send the message to
channel_name = ctx.channel.name
user_id = channel_name
# declare the member it needs to send it to
member = bot.get_user(user_id)
# printing some things so I can check what It returns
print (user_id)
print (member)
print('------')
# send the message to the user
await member.send("Confirmed")
user_id
是一个字符串,它应该是一个整数
@bot.command()
async def id(ctx):
# take the id of the user it needs to send the message to
channel_name = ctx.channel.name
user_id = int(channel_name)
member = bot.get_user(user_id)
print(user_id)
print(member)
await member.send("Confirmed")
错误消息告诉您试图调用不存在对象的send
方法,该方法由NoneType
表示。作为一种通用方法,您需要检查调用send的位置。从你的代码中,我可以找到一个例子:
await member.send("Confirmed")
因此,member
是NoneType
。member
就是这样定义的
member = bot.get_user(user_id)
既然您已经明确表示user_id
是正确的,那么从逻辑上讲,bot.get_user
不会产生由正确的user_id
标识的用户。user_id
是频道名称,这听起来很奇怪,因为频道通常与用户不同。
为了弄清楚,如果可能的话,您需要调试get_user
。在那里传递一个正确的user_id
,看看是否找到它。您应该能够用您测试的确切场景来重现问题。通道名称可能与存储的user_id
值的类型不同。看看你是否能够通过id获得合适的用户。如果你能够做到这一点,那么它也应该在真实的场景中工作。
发生这种情况是因为bot.get_user()
无法检索Member
,因为您将实际ID的str
表示传递给它。
在将user_id传递给函数之前强制转换:bot.get_user(int(user_id))
然而,为什么要做所有这些,而不是仅仅从ctx
中提取成员?