代码任务:
使用该命令时,机器人会显示所有服务器参与者的列表。
我的问题:
我的问题是我的列表是根据我的未知排序发布的。
我希望它看起来像什么:
我希望参与者按登录服务器的用户进行排序。还有,如果不难,就在昵称旁边加一个对应的数字。
为了更详细的理解,我在下面添加了现在的样子和应该是什么样子的照片。
代码:
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
答案 0 :(得分:0)
这两件事都可以相对容易地完成。
sorted
函数并根据joined_at
属性对成员进行排序members = [str(m) for m in sorted(ctx.guild.members, key=lambda m: m.joined_at)]
enumerate
函数。members = [f"{i}. {m}" for i, m in enumerate(members, start=1)]
您可以将所有内容合并为一行
members = [f"{i}. {m}" for i, m in enumerate(sorted(ctx.guild.members, key=lambda m: m.joined_at), start=1)]