我正在尝试使用自助机器人给自己发私信。我正在尝试在我的代码中使用该get_user()函数。
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)
Run Code Online (Sandbox Code Playgroud)
相反,我返回此错误:
File "<ipython-input-1-90e5e962a6e9>", line 24, in dm
await user.send("Hello")
AttributeError: 'NoneType' object has no attribute 'send'
Run Code Online (Sandbox Code Playgroud)
机器人无法找到用户并返回一个None值。我已经测试了多个 ID,但不确定问题是什么。
小智 9
您始终可以使用协程client.fetch_user(id)来完成它。get_user()从缓存中获取它,因此当它是新鲜的时,大多数时候都不起作用。
在你的情况下:
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)```
Run Code Online (Sandbox Code Playgroud)