get_user(id) cant find user - returns None (self bot discord.py)(get_user(id) 找不到用户 - 返回无(self bot discord.py))
问题描述
我正在尝试使用自我机器人与自己联系.我正在尝试在我的代码中使用 get_user()
函数.
I am trying to DM myself using a self bot. I am trying to use the get_user()
function in my code.
bot = commands.Bot(command_prefix='', self_bot=True)
counter = 0
userID = 695724603406024726
@bot.event
async def dm(userID):
print('Running Function')
global counter
if counter <= 0:
print('Finding user.')
counter += 1
user = bot.get_user(userID)
print('user:',user)
await user.send("Hello")
print('message sent')
return
bot.loop.create_task(dm(userID))
bot.run(token, bot=False)
相反,我返回此错误:
File "<ipython-input-1-90e5e962a6e9>", line 24, in dm
await user.send("Hello")
AttributeError: 'NoneType' object has no attribute 'send'
机器人找不到用户并返回 None
值.我已经测试了多个 ID,但不确定是什么问题.
The bot can't find the user and returns a None
value. I have tested multiple ID's and am unsure what the problem is.
推荐答案
你总是可以使用协程 client.fetch_user(id)
来完成它.get_user()
从缓存中获取它,所以当新鲜时,大多数时候都不起作用.
You could always use the coroutine client.fetch_user(id)
to get it done. get_user()
takes it from cache so when fresh, doesn't work most of the times.
在你的情况下:
bot = commands.Bot(command_prefix='', self_bot=True)
counter = 0
userID = 695724603406024726
async def dm(userID):
print('Running Function')
global counter
if counter <= 0:
print('Finding user.')
counter += 1
user = await bot.fetch_user(userID)
print('user:',user)
await user.send("Hello")
print('message sent')
return
bot.loop.create_task(dm(userID))
bot.run(token, bot=False)```
这篇关于get_user(id) 找不到用户 - 返回无(self bot discord.py)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:get_user(id) 找不到用户 - 返回无(self bot discord.py)


基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01