2021-07-29

Discord.py | Sorting member's

Code Task:
When using the command, the bot displays a list of all server participants.

My problem:
My problem is that my list is issued according to an unknown sorting for me.

How do I want it to look like:
I want the participants to be sorted by which user logged in to the server. And also, if it is not difficult, then add a number corresponding to their place near their nicknames.

For a more detailed understanding, I have added photos below of how it looks now and how it should look.

Code:

from discord.ext import commands
import discord
import asyncio
import math

Bot = commands.Bot(command_prefix=commands.when_mentioned_or("c:"), intents=discord.Intents.all())

@Bot.command()
@commands.is_owner()
async def members(ctx):
    members = [str(m) for m in ctx.guild.members]
    per_page = 10 
    pages = math.ceil(len(members) / per_page)
    cur_page = 1
    chunk = members[:per_page]
    linebreak = "\n"
    memberCount = str(ctx.guild.member_count)
    embed = discord.Embed(title=f"Page {cur_page}/{pages}:\n", description = f"{linebreak.join(chunk)}")
    embed.set_footer(text = f'Members: {memberCount}')
    message = await ctx.send(embed = embed)
    await message.add_reaction("◀️")
    await message.add_reaction("▶️")
    active = True

    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ["◀️", "▶️"]

    while active:
        try:
            reaction, user = await Bot.wait_for("reaction_add", timeout=60, check=check)

            if str(reaction.emoji) == "▶️" and cur_page != pages:
                cur_page += 1
                if cur_page != pages:
                    chunk = members[(cur_page-1)*per_page:cur_page*per_page]
                    memberCount = str(ctx.guild.member_count)
                    embed = discord.Embed(title=f"Page {cur_page}/{pages}:\n", description = f"{linebreak.join(chunk)}")
                    embed.set_footer(text = f'Members: {memberCount}')
                else:
                    chunk = members[(cur_page-1)*per_page:]
                    memberCount = str(ctx.guild.member_count)
                    embed = discord.Embed(title=f"Page {cur_page}/{pages}:\n", description = f"{linebreak.join(chunk)}")
                    embed.set_footer(text = f'Members: {memberCount}')
                await message.edit(embed = embed)
                await message.remove_reaction(reaction, user)

            elif str(reaction.emoji) == "◀️" and cur_page > 1:
                cur_page -= 1
                chunk = members[(cur_page-1)*per_page:cur_page*per_page]
                memberCount = str(ctx.guild.member_count)
                embed = discord.Embed(title=f"Page {cur_page}/{pages}:\n", description = f"{linebreak.join(chunk)}")
                embed.set_footer(text = f'Members: {memberCount}')
                await message.edit(embed=embed) 
                await message.remove_reaction(reaction, user)
        except asyncio.TimeoutError:
            await message.delete()
            active = False

How should I be on the list

How does it show me in the list

How it should look like



from Recent Questions - Stack Overflow https://ift.tt/3l1ywv8
https://ift.tt/eA8V8J

No comments:

Post a Comment