【问题标题】:Discord.py: Get user object with idDiscord.py:获取带有 id 的用户对象
【发布时间】:2020-12-25 08:21:53
【问题描述】:

我有一个只保存用户 ID 的机器人(这样工作会更好)。现在我想从那个 id 中获取用户对象。

我尝试了以下对我不起作用的方法,也许我做错了什么..

client = discord.Client
client.get_user(id)

client.get_user_info(id)

代码如下:

import discord
class MyClient(discord.Client):
    ntd = name_table.get_all_records() #google sheets api: returns a list of dicts
        async def on_message(self, message):
             print(message.content[1])
             if message.author == client.user:
                return
             if isinstance(message.channel, discord.channel.DMChannel):
                await self.handle_DM(message)
             if message.content[0] == "/":
                if message.content.split(" ")[0].lower() == "/rank":
                   await self.rank_command(message)
       
       async def rank_command(self, message):
        if len(message.content.split(" ")) < 2:
            await message.channel.send(
                message.author.mention + "/rank takes 2 or 3 arguments. Do '/rank ?' for help.")
        try:
            if message.content.split(" ")[1] == "?":
                await self.help("rank", message)
                return
        except:
            pass
        try:
            if message.content.split(" ")[1] == "top":
                await self.top_rank_command(message)
                return
        except:
            pass
     async def top_rank_command(message):
          print("Top rank entered")
          _user = await client.fetch_user(372746847393021953) #my id: Cluebo#2312
        print(_user)
        print("Top rank end")

执行这个,它只会打印"Top rank entered" 而不是user"Top rank end" 但是,我没有收到错误消息。该方法就停在那里(机器人在 Heroku 上运行)。 提前致谢!

【问题讨论】:

    标签: python heroku discord discord.py


    【解决方案1】:

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

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

    #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)
    

    有了这些例子,你只需要在 discord 中写 !find [id],它就会打印它在你的终端中获取的对象

    【讨论】:

    • 我用我自己的 id 尝试了你的第一个建议。它应该在终端中打印结果,但我遇到和以前一样的问题..我不知道为什么它会停止而没有错误..
    • 整个代码太长,但我会包括必要的东西
    • 我的错,忘了等待方法,我已经编辑了帖子
    • 我猜旧方法是 client.get_user_info... 更改为 bot.fetch_user?无论哪种方式..这对我有用!
    【解决方案2】:

    client.fetch_user(id(int)) 是一个协程。这意味着我需要添加一个await,所以它需要是_user = await client.fetch_user(id)。确保 id 正确,因为脚本可能会在没有错误的情况下停止。

    【讨论】:

      猜你喜欢
      • 2019-06-27
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      相关资源
      最近更新 更多