• <i id='txy7Z'><tr id='txy7Z'><dt id='txy7Z'><q id='txy7Z'><span id='txy7Z'><b id='txy7Z'><form id='txy7Z'><ins id='txy7Z'></ins><ul id='txy7Z'></ul><sub id='txy7Z'></sub></form><legend id='txy7Z'></legend><bdo id='txy7Z'><pre id='txy7Z'><center id='txy7Z'></center></pre></bdo></b><th id='txy7Z'></th></span></q></dt></tr></i><div id='txy7Z'><tfoot id='txy7Z'></tfoot><dl id='txy7Z'><fieldset id='txy7Z'></fieldset></dl></div>
        <bdo id='txy7Z'></bdo><ul id='txy7Z'></ul>

    1. <tfoot id='txy7Z'></tfoot>
      <legend id='txy7Z'><style id='txy7Z'><dir id='txy7Z'><q id='txy7Z'></q></dir></style></legend>
      1. <small id='txy7Z'></small><noframes id='txy7Z'>

        Discord.py:获取带有 id 的用户对象

        时间:2023-09-10
        • <bdo id='Fww6C'></bdo><ul id='Fww6C'></ul>
              <tbody id='Fww6C'></tbody>
              <legend id='Fww6C'><style id='Fww6C'><dir id='Fww6C'><q id='Fww6C'></q></dir></style></legend>
              <i id='Fww6C'><tr id='Fww6C'><dt id='Fww6C'><q id='Fww6C'><span id='Fww6C'><b id='Fww6C'><form id='Fww6C'><ins id='Fww6C'></ins><ul id='Fww6C'></ul><sub id='Fww6C'></sub></form><legend id='Fww6C'></legend><bdo id='Fww6C'><pre id='Fww6C'><center id='Fww6C'></center></pre></bdo></b><th id='Fww6C'></th></span></q></dt></tr></i><div id='Fww6C'><tfoot id='Fww6C'></tfoot><dl id='Fww6C'><fieldset id='Fww6C'></fieldset></dl></div>

                <tfoot id='Fww6C'></tfoot>

                  <small id='Fww6C'></small><noframes id='Fww6C'>

                  本文介绍了Discord.py:获取带有 id 的用户对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I have a bot which only saves the id of a user (it's better working like that). Now I want to get the user-object from that id.

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

                  I tried following which doesn't work for me, maybe I have made something wrong..

                  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 enter",而不是userTop rank end"但是,我没有收到错误消息.该方法就停在那里(机器人在 Heroku 上运行).提前致谢!

                  Executing this, it will only print "Top rank entered" but not the user or "Top rank end" However, I did not get an error message. The method just stops there (The bot is running on Heroku). Thanks in advance!

                  推荐答案

                  从一个id获取用户有两种方法:

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

                  • Client.fetch_user(id)(或 Bot.fetch_user(id)) → 返回一个 discord.User 对象
                  • Guild.fetch_member(id) → 返回一个 discord.Member 对象

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

                  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)
                  

                  使用这些示例,您只需在 discord 中编写 !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

                  这篇关于Discord.py:获取带有 id 的用户对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我怎么做才能让你不能用 echo 命令@everyone 下一篇:我如何知道用户何时在 Discord.py 中加入了 Discor

                  相关文章

                  最新文章

                1. <tfoot id='FjAgm'></tfoot>
                  1. <i id='FjAgm'><tr id='FjAgm'><dt id='FjAgm'><q id='FjAgm'><span id='FjAgm'><b id='FjAgm'><form id='FjAgm'><ins id='FjAgm'></ins><ul id='FjAgm'></ul><sub id='FjAgm'></sub></form><legend id='FjAgm'></legend><bdo id='FjAgm'><pre id='FjAgm'><center id='FjAgm'></center></pre></bdo></b><th id='FjAgm'></th></span></q></dt></tr></i><div id='FjAgm'><tfoot id='FjAgm'></tfoot><dl id='FjAgm'><fieldset id='FjAgm'></fieldset></dl></div>

                      <legend id='FjAgm'><style id='FjAgm'><dir id='FjAgm'><q id='FjAgm'></q></dir></style></legend>

                      <small id='FjAgm'></small><noframes id='FjAgm'>

                      • <bdo id='FjAgm'></bdo><ul id='FjAgm'></ul>