Mika Tan
Mika Tan

Reputation: 128

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

I've been developing a discord bot with discord.py, and I've run into some trouble trying to display a leaderboard.

The leaderboard currently looks like: enter image description here

However, I want the user ids to display mentions. This is my Python code:

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

This is my json file code:

{"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}}

I've tried doing memberName = member.mention but that does not seem to work.

My Python version is 3.8.5, I run on a MacOS Catalina.

Upvotes: 0

Views: 964

Answers (4)

OverlySolemn
OverlySolemn

Reputation: 1

@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)

Upvotes: 0

Navemics
Navemics

Reputation: 68

You can tag (they won't get a notification) people in embeds.

Check this, it is possible: https://gyazo.com/118f0251798afd32bdd23d806c694544

I believe you just take the message's author and add a .mention to the end of the 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)

Upvotes: 0

Abdulaziz
Abdulaziz

Reputation: 3426

a discord.Member has mention. your issue is that you didn't take it from the member object it self rather from the memberName which is a string.

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

Doing it manually as a string might work but is a bad habit.

Upvotes: 1

Harjot
Harjot

Reputation: 943

So, basically, you cannot mention a user in an Embed field's name, rather, you can just switch the value with the name of the embed and get satisfactory results. Your code will look like so -

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

Upvotes: 1

Related Questions