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



我想获得我的服务器上所有成员的公会信息,然后显示公会成员中拥有最多金币的前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的内容

总结一下注释的结果;

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

File "main.py", line !!501年! !, in guild

:)

最新更新