【问题标题】:Get user id from username Discord.py从用户名 Discord.py 获取用户 ID
【发布时间】:2021-07-18 17:28:15
【问题描述】:

我想制作一个程序来获取您输入的用户名的用户 ID。 这是我目前所拥有的:

from discord.ext import commands

client = commands.Bot(description="a", command_prefix=".", self_bot = False)

client.remove_command('help')

@client.event
async def on_ready():
    user = await client.fetch_user("pirace#4637")
    print(user)

client.run('token')

它给了我这个错误:

400 Bad Request (error code: 50035): Invalid Form Body
In user_id: Value "pirace#4637" is not snowflake.

有人知道我会怎么做吗?

【问题讨论】:

  • 您已经暴露了您的机器人令牌。请编辑您的帖子以将其删除,您应该在 discord 开发者仪表板上重置它。

标签: python discord discord.py bots


【解决方案1】:

fetch_user 接受雪花或更简单的说法,id。可以通过以下方式获取具有用户名的用户:

@bot.command()
async def info(ctx, user:discord.User):
    return await ctx.send(user.name)

这仅在用户与您的机器人共享公会并且区分大小写时才有效。因此,要么将有效的用户 ID 作为整数放入 bot.fetch_user,要么使用我提供的代码

【讨论】:

    【解决方案2】:

    如果你想使用用户名,你必须使用discord.utils.get(),在这种情况下结合client.get_guild(GUILD_ID)

    from discord.ext import commands
    
    client = commands.Bot(description="a", command_prefix=".", self_bot = False)
    
    client.remove_command('help')
    
    @client.event
    async def on_ready():
        guild = client.get_guild(GUILD_ID)
        
        user = discord.utils.get(guild.members, name="pirace", discriminator="4637")
    
        print(user)
    
    client.run('token')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      相关资源
      最近更新 更多