带有 mysql discord.py 的排行榜命令 [英] leaderboard command with mysql discord.py
问题描述
我正在尝试使用 mysql 制作排行榜,但我不知道如何实际操作.我到处搜索,但不幸的是我什么也没找到.我做的唯一一件事是排序查询,但我不知道如何在 python 上显示它.建议?
@client.command()异步定义排行榜(ctx):游标 = levelsystem_db.cursor()cursor.execute(SELECT users.user_level, users.user_xp, users.client_id\nFROM users\nORDER BY users.user_level DESC, users.user_xp DESC")排行榜 = cursor.fetchall()等待 ctx.send(排行榜)
<预> <代码> [(13,17500,356536794646446080),(10,10255,488826524791734275),(10,10160,523967502665908227),(7,2500,572852913664163881),(6,1000,640479176280899584),(5,500, 702691407034318920)]
我假设格式是下一个
(等级,经验,member_id)
很简单,你可以简单地使用一个for循环
for i, pos in enumerate(leaderboard, start=1):lvl, xp, member_id = pos打印(f{i}.{member_id},级别:{lvl},XP:{xp}")# 结果# 1. 356536794646446080,等级:13,经验值:17500# 2. 488826524791734275,等级:10,经验值:10255# 3. 523967502665908227,等级:10,经验值:10160# 4. 572852913664163881,等级:7,经验值:2500# 5. 640479176280899584,等级:6,经验值:1000# 6. 702691407034318920,等级:5,经验值:500
您可以将其放入嵌入内容、消息...这取决于您的创造力.
将 ID 转换为 discord.Member
实例
# 首先你需要一个 `discord.Guild` 实例(你可以使用 `ctx.guild` 或 `bot.get_guild`公会 = ctx.guild# 获取实例成员 = guild.get_member(member_id)
参考:
I am trying to make a leaderboard using mysql but I have no idea how to actually do it. I searched everywhere but unfortunately I found nothing. The only thing I did was a sort query however I don't know how to show it on python. Advice?
@client.command()
async def leaderboard(ctx):
cursor = levelsystem_db.cursor()
cursor.execute("SELECT users.user_level, users.user_xp, users.client_id\nFROM users\nORDER BY users.user_level DESC , users.user_xp DESC")
leaderboard = cursor.fetchall()
await ctx.send(leaderboard)
[(13, 17500, 356536794646446080), (10, 10255, 488826524791734275), (10, 10160, 523967502665908227), (7, 2500, 572852913664163881), (6, 1000, 640479176280899584), (5, 500, 702691407034318920)]
I'm assuming the format is the next one
(level, xp, member_id)
It's pretty simple, you can simply use a for loop
for i, pos in enumerate(leaderboard, start=1):
lvl, xp, member_id = pos
print(f"{i}. {member_id}, level: {lvl}, XP: {xp}")
# Result
# 1. 356536794646446080, level: 13, XP: 17500
# 2. 488826524791734275, level: 10, XP: 10255
# 3. 523967502665908227, level: 10, XP: 10160
# 4. 572852913664163881, level: 7, XP: 2500
# 5. 640479176280899584, level: 6, XP: 1000
# 6. 702691407034318920, level: 5, XP: 500
You can put this in an embed, a message... That depends on your creativity really.
To convert an ID to a discord.Member
instance
# First you need a `discord.Guild` instance (you can either use `ctx.guild` or `bot.get_guild`
guild = ctx.guild
# Getting the instance
member = guild.get_member(member_id)
Reference:
这篇关于带有 mysql discord.py 的排行榜命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!