簡體   English   中英

我如何在 discord 排行榜中提及 discord.py discord 機器人的人

[英]How do I mention people in a discord leaderboard for discord.py discord bot

我一直在用 discord.py 開發一個 discord 機器人,我在嘗試顯示排行榜時遇到了一些麻煩。

排行榜目前看起來像: 在此處輸入圖像描述

但是,我希望用戶 ID 顯示提及。 這是我的 Python 代碼:

import discord 
from discord.ext import commands
import json
import os

os.chdir('My File Path')

@client.command(aliases = ["lb"])
async def leaderboard(ctx,x = 5):
    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"Top {x} Richest People" , description = "This is decided on the basis of raw money in the bank and wallet",color = discord.Color(0xfa43ee))
    index = 1
    for amt in total:
        id_ = leader_board[amt]
        member = client.get_user(id_)
        memberName = f"<@{user}>" #Here is where I'm having trouble
        em.add_field(name = f"{index}. {memberName}" , value = f"{amt}",  inline = False)
        if index == x:
            break
        else:
            index += 1

    await ctx.send(embed = em)


async def get_bank_data():
    with open("main-bank.json", "r") as f:
        users = json.load(f)

    return users

這是我的 json 文件代碼:

{"User ID of someone": {"wallet": 66.0, "bank": 2000} "User ID of someone": {"wallet": 0, "bank": 1969}, "User ID of someone": {"wallet": 10, "bank": 0}, "User ID of someone": {"wallet": 89, "bank": 0}}

我試過memberName = member.mention但這似乎不起作用。

我的 Python 版本是 3.8.5,我在 MacOS Catalina 上運行。

因此,基本上,您不能在嵌入字段的名稱中提及用戶,而只需將值與嵌入的名稱進行切換即可獲得滿意的結果。 你的代碼看起來像這樣 -

for amt in total:
        id_ = leader_board[amt]
        member = client.get_user(id_)
        memberName = f"<@{user}>" #Here is where I'm having trouble
        em.add_field(value = f"{index}. {memberName}" , name = f"{amt}",  inline = False)
        if index == x:
            break
        else:
            index += 1

一個discord。會員mention 你的問題是你沒有從成員 object 中獲取它,而是從memberName中獲取它,它是一個字符串。

member = client.get_user(ID_HERE)
em.add_field(name="User", value = member.mention)

將其作為字符串手動執行可能有效,但這是一個壞習慣。

您可以標記(他們不會收到通知)嵌入的人。

檢查這個,有可能: https://gyazo.com/118f0251798afd32bdd23d806c694544

我相信您只需將消息的作者添加到 object 的末尾即可。

for user in board:
    description += f"**{board.index(user) + 1}.** {user[0].mention} | Level: {user[1]} | XP: {user[2]}\n"
msg = discord.Embed(
    color=discord.Color.green(),
    title=f"{str(ctx.guild)}" "s Valley Leaderboard",
    description=description,
)

await ctx.send(embed=msg)
@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"Top {x} Richest People", description = "This is decided on the basis of raw money in the bank and wallet", color = discord.Color(0xfa43ee))
    
    index = 10
    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"{amt}", inline = False)
        if index == x:
            break
        else:
            index += 1

    await ctx.send(embed = em)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM