如何获取我服务器所有成员的公会信息

Posted

技术标签:

【中文标题】如何获取我服务器所有成员的公会信息【英文标题】:How to get guild info of all the members of my server 【发布时间】:2021-11-09 07:49:03 【问题描述】:

我想获取我服务器所有成员的公会信息,然后显示公会成员中金币最多的前10名成员

@client.event
async def on_guild_join(guild):
    
    with open("mainbank.json", "r") as f:
        guildInfo = json.load(f)

    guildInfo[guild.id] = guild.text_channels[0]
    
    
    with open("mainbank.json"), "w" as f:
        json.dump(guildInfo, f)
         

@client.command(aliases = ['gd'])
async def guild(ctx):
    with open("mainbank.json", "r") as f:
      guildInfo = json.load(f)

    channel = guildInfo[ctx.message.guild.id]
    await ctx.send (embed=channel)

上面的代码给出了这个错误

Ignoring exception in command guild:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 501, in guild
    channel = guildInfo[ctx.message.guild.id]
KeyError: 884811895884894218

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 884811895884894218

我正在将此代码用于排行榜,它对我服务器中的所有成员都非常有效,但我希望它显示公会成员之间的前 10 名

@client.command(aliases = ["lb"])
async def leaderboard(ctx,x = 10):
    users = await get_bank_data()
    leader_board = 
    total = []
    for user in users:
        name = int(user)
        total_amount = users[user]["wallet"] + users[user]["bank"]
        leader_board[total_amount] = name
        total.append(total_amount)

    total = sorted(total,reverse=True)    

    em = discord.Embed(title = f"<:emoji_23:886988033872764938> : TOP x RICHEST PEOPLES OF ALL TIME..." , description = "This is decided on the basis of raw money in the bank and wallet",color = discord.Color(0x00FFFC))
    index = 1
    for amt in total:
        id_ = leader_board[amt]
        member = await client.fetch_user(id_)
        name = member.name
        em.add_field(name = f"index. name  " , value = f" 「Balance」 ❯❯ amt ????",  inline = False)
        if index == x:
            break
        else:
            index += 1

    await ctx.send(embed = em)

我正在将此代码用于排行榜,它对我服务器中的所有成员都非常有效,但我希望它显示公会成员之间的前 10 名

"555463650358329354": "wallet": 87, "bank": 6969, "bag": ["item": "watch", "amount": 0], "886271151729414154": "wallet": 0, "bank": 6969, "525961784297914378": "wallet": 0, "bank": 6969, "680988467270123522": "wallet": 0, "bank": 6969, "725722170206060624": "wallet": 0, "bank": 6969, "517879304562933780": "wallet": 0, "bank": 6969, "706831502859698226": "wallet": 0, "bank": 7400, "bag": ["item": "watch", "amount": 1], "781446116955914260": "wallet": 0, "bank": 6969, "bag": ["item": "watch", "amount": 1], "875681501696643102": "wallet": 0, "bank": 6969, "599611694746042380": "wallet": 0, "bank": 6969, "786899180240502804": "wallet": 0, "bank": 6969, "537275315530104832": "wallet": 0, "bank": 6969, "632848175992012825": "wallet": 0, "bank": 6969, "704319968183058492": "wallet": 0, "bank": 7099, "864813571501195315": "wallet": 183, "bank": 6969, "755771702947872818": "wallet": 0, "bank": 6969, "bag": ["item": "watch", "amount": 0], "570601939809599489": "wallet": 0, "bank": 6969, "789018134228762645": "wallet": 0, "bank": 6969, "742642664129953822": "wallet": 0, "bank": 6969, "886651528469971025": "wallet": 0, "bank": 0, "768162376003878994": "wallet": 0, "bank": 6969, "854389490021564456": "wallet": 0, "bank": 51332

以上是mainbank.json的内容

【问题讨论】:

发送mainbank.json的内容 添加@WasiMaster 确保您已启用所有意图 json 键是字符串,而您用来访问的键是整数。首先将它们转换为 str 到底在哪一行? @WasiMaster 【参考方案1】:

只是总结一下cmets的结果;

您首先必须将ctx.message.guild.id 转换为带有channel = guildInfo[str(ctx.message.guild.id)] 的字符串,因为JSON 文件还将公会ID 作为字符串(例如"555463650358329354": "wallet": 87,...

顺便说一句

文件“main.py”,行!!第501章!!,在公会里

:)

【讨论】:

以上是关于如何获取我服务器所有成员的公会信息的主要内容,如果未能解决你的问题,请参考以下文章

如何获取公会 Discord.js 中的成员列表

无法通过 Discordjs v13 获取所有公会成员

试图获取公会中的所有成员(discord.py 重写)

如何在 discord.js v12 中列出所有公会成员

Discord Py - 如何使用文本命令向公会的所有成员添加多个角色

Discord 使用 php curl 获取公会成员列表