且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Discord.py:获取具有ID的用户对象

更新时间:2023-11-30 12:20:52

有两种方法可以从id中获取用户:

There are two methods to get a user from an id:

以下是一些示例(同时使用 Client Bot ):

Here are some examples (using both Client and Bot):

#Get a discord.User object
@client.event
async def on_message(message):
    content = message.content[6:]
    if message.content.startswith('!find'):
        user = await client.fetch_user(int(content))
        print(user)
    else:
        pass

@bot.command()
async def find(ctx, id: int):
    user = await bot.fetch_user(id)
    print(user)

#Get a discord.Member object
@client.event
async def on_message(message):
    content = message.content[6:]
    if message.content.startswith('!find'):
        member = await message.guild.fetch_member(int(content))
        print(member)
    else:
        pass

@bot.command()
async def find(ctx, id: int):
    member = await ctx.guild.fetch_member(id)
    print(member)

在这些示例中,您只需要以不和谐的方式编写!find [id] ,它将打印从终端中获取的对象

With those examples, you'd just have to write !find [id] in discord and it would print the object it fetched in your terminal