【问题标题】:discord.py Get the ban reason of a banned userdiscord.py 获取被封禁用户的封禁原因
【发布时间】:2023-03-18 23:10:01
【问题描述】:

我使用 Python 制作了一个 Discord 机器人,它具有简单的命令,如“kick”、“ban”、“mute”、“warn”等。我还想添加“list warns”和“list bans”命令。

我可以使用以下代码获取所有被禁止的用户:

bans = await client.get_bans(message.guild.id)
for user in bans:
    print(f"User: {user.name} | User ID: {user.id}")

但在 Discord 中,禁止成员时也会保存禁止原因:

如何从被禁止的用户那里检索被禁止的原因?我在 discord.py 文档中找不到与此相关的任何内容。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您使用的是哪个版本的库?

    client.get_bans() 返回guild.Ban 的列表,userreason 附带:

        def get_bans(self, guild_id: Snowflake) -> Response[List[guild.Ban]]:
            return self.request(Route('GET', '/guilds/{guild_id}/bans', guild_id=guild_id))
    
    class Ban(TypedDict):
        reason: Optional[str]
        user: User
    

    https://github.com/Rapptz/discord.py/blob/45d498c1b76deaf3b394d17ccf56112fa691d160/discord/http.py#L1119

    所以,也许是这样的

    bans = await client.get_bans(message.guild.id)
    for ban in bans:
        user = ban['user']
        reason = ban['reason']
    

    【讨论】:

    • client.get_bans() 已过时 (
    【解决方案2】:

    您可以使用bans() 方法。

    await guild.bans() => 它返回BanEntry 对象。 而这个对象包含两个属性,分别是ReasonUser

    【讨论】:

      【解决方案3】:

      使用 guild.bans() 代替 client.get_bans()。 找个理由也很简单。 有用信息:https://discordpy.readthedocs.io/en/stable/api.html?highlight=bans#discord.BanEntry

      这是我的代码:

      bans = await message.guild.bans() #Getting a list of all ban entries
      for ban_entry in bans: #Looping through all entries
         user = ban_entry.user #Getting user
         reason = ban_entry.reason #Getting Reason
      

      【讨论】:

      • 感谢您的回答和示例!
      猜你喜欢
      • 1970-01-01
      • 2022-08-06
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多