gpt4 book ai didi

python - get_user(id) 找不到用户 - 返回 None (self bot discord.py)

转载 作者:行者123 更新时间:2023-12-04 16:40:49 25 4
gpt4 key购买 nike

我正在尝试使用自我机器人 DM 自己。我正在尝试使用 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)

相反,我返回此错误:
  File "<ipython-input-1-90e5e962a6e9>", line 24, in dm
await user.send("Hello")
AttributeError: 'NoneType' object has no attribute 'send'

机器人找不到用户并返回 None值(value)。我已经测试了多个 ID,但不确定是什么问题。

最佳答案

你总是可以使用协程 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)```

关于python - get_user(id) 找不到用户 - 返回 None (self bot discord.py),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61112322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com