Получение имени пользователя по его ID (discord python)

У меня есть айди пользователя и следующий код:

bot = commands.Bot(command_prefix = "!") 

@bot.command() 
async def getuser(ctx, userid: int): 
 user = bot.get_user(userid)
 await ctx.send(user.name)

Этот код должен на команду !getuser [ID] отправить никнейм пользователя, чей айди указан. Но вместо этого появляется ошибка в консоли:

Ignoring exception in command getuser:
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "/storage/emulated/0/z_japan_bot/give2.py", line 103, in getuser
    user = bot.get_user(userid).name
AttributeError: 'NoneType' object has no attribute 'name'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/ext/commands/core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'name'

Помогите исправить пожалуйста.


Ответы (1 шт):

Автор решения: denisnumb

Вместо bot.get_user(id) используйте await bot.fetch_user(id).

→ Ссылка