python - 无法使用他们的 ID 向 discord.py 中的用户发送消息
问题描述
我有一个问题。我正在制作一个使用用户 ID 的 mod-mail 机器人。但是当我使用 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")
推荐阅读
- python - 使用不关心任何输入参数的闪存创建 API
- functional-programming - 无法过滤单声道地图
- javascript - 在 amcharts 强制定向网络中的两个节点之间添加多个链接
- node.js - 为什么我在 Heroku Postgresql 插件中的密码是空白的?
- vb.net - 将加密文本文件的内容解密并复制到 MemoryStream
- java - 在 JavaFx 应用程序中未正确显示 Swing 内容
- java - Java string.replace 不能作为 string.replaceAll
- flutter - 在没有谷歌服务的华为 p40 lite 上编译我的应用程序时出错
- laravel - 使用 medialibrary spatie 运行测试后如何删除图像上传?
- javascript - 基于布尔值重新排列现有选项