我有一个问题。我正在制作一个使用用户 ID 的 mod-mail bot。但是当我使用 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")
答案 0 :(得分:0)
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")
答案 1 :(得分:0)
发生这种情况是因为 bot.get_user()
无法检索 Member
,因为您向它传递了实际 ID 的 str
表示。
在将 user_id 传递给函数之前对其进行转换:bot.get_user(int(user_id))
但是,您为什么要执行所有这些操作,而不只是从 ctx
中提取成员?
答案 2 :(得分:0)
错误消息告诉您,您尝试调用不存在的对象的 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 获取正确的用户。如果您能够这样做,那么它应该也适用于真实场景。