Reputation: 21
So I need my bot to get a user from a UserID. I tried it like this:
import discord
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix = '&')
@client.command(pass_context = True)
async def test(ctx, id : int):
user = client.get_user(id)
print(user)
TOKEN = ""
client.run(TOKEN)
but it always returns None, but why? :C Thanks for helping.
Upvotes: 1
Views: 3868
Reputation: 642
Depending on what you're trying to do you're probably missing intents.
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='&', intents=intents) # renamed bot because you're using an instance of Bot
@bot.command() # pass_context is not necesary since more than few versions back
async def test(ctx, user_id): # renamed id to user_id to make it more readable
user = bot.get_user(user_id)
print(user)
TOKEN = ""
bot.run(TOKEN)
Take into consideration the code above retreives a "user" object not a "member". The distinction is important if your leaderboard is related to a specific server. Also, the user object doen't have attributes such as "roles", "nickname", etc. If you wish to retreive that information you need to get a member object using ctx.guild.get_member
instead of bot.get_user
Upvotes: 1
Reputation: 15689
async def test(id: int):
user = client.get_user(id)
if user is None: # Maybe the user isn't cached?
user = await client.fetch_user(id)
return user
# Inside the loop
user = await test(12312312313)
Also remember to enable intents.members
, here's how
Upvotes: 2