按字母顺序列出角色

问题描述 投票:-2回答:1

嗨我想按字母顺序列出角色到目前为止我能够在我的discord服务器上列出角色我不确定如何按字母顺序在.py中列出它我在任何地方搜索都没有任何成功。

这是我正在使用的。

import re
import discord
from .utils import checks
from discord.ext import commands
from __main__ import send_cmd_help


class Roles:
    def __init__(self, bot):
    self.bot = bot


@commands.command(pass_context=True, no_pm=True, name='add', aliases=['iam','iplay'])
async def add(self, context, *role_name):
    """Add a role"""
    server = context.message.server
    author = context.message.author
    name = ' '.join(role_name)
    roles = [role.name.lower() for role in server.roles]
    if name.lower() in roles:
        for role in server.roles:
            if role.name.lower() == name.lower():
                if role.permissions.value < 1:
                    try:
                        await self.bot.add_roles(author, role)
                        message = '{} added the role **{}**.'.format(author.display_name, role.name)
                        embed = discord.Embed(description=message.format(author.display_name, role.name), colour=0x00ff00)
                        embed.set_footer(text="Tip: type the command !roles or !list to list all roles.")
                        break
                    except discord.Forbidden:
                        message = 'I have no permissions to do that. Please give me role managing permissions.'
                        embed = discord.Embed(description=message)
                else:
                    message = 'You cannot use this role'
                    embed = discord.Embed(description=message)
            else:
                message = 'No such role'
                embed = discord.Embed(description=message)
    else:
        message = 'I cannot find that role :frowning2:'
        embed = discord.Embed(description=message)
        embed.set_footer(text="Tip: type the command !list to list all roles.")
    await self.bot.say(embed=embed)

@commands.command(pass_context=True, no_pm=True, name='remove')
async def remove(self, context, *role_name):
    """Remove a role"""
    server = context.message.server
    author = context.message.author
    name = ' '.join(role_name)
    roles = [role.name.lower() for role in server.roles]
    if name.lower() in roles:
        for role in server.roles:
            if role.name.lower() == name.lower():
                try:
                    await self.bot.remove_roles(author, role)
                    message = '{} removed the role **{}**'.format(author.display_name, role.name)
                    embed = discord.Embed(description=message.format(author.display_name, role.name), colour=0xff0000)
                    break
                except discord.Forbidden:
                    message = 'I have no permissions to do that. Please give me role managing permissions.'
                    embed = discord.Embed(description=message)
            else:
                message = '`Something went wrong...`'
                embed = discord.Embed(description=message)
    else:
        message = 'There is no such role on this server'
        embed = discord.Embed(description=message)
    await self.bot.say(embed=embed)

@commands.command(pass_context=True, no_pm=True, name='list', aliases=['roles', 'role'])
async def _list(self, context):
    """List of all available roles """
    server = context.message.server
    author = context.message.author
    message = '\n**Hey {}, here is a list of roles you can add:**\n'.format(author.display_name)
    for role in server.roles:
        if role.permissions.value < 1:
            message += '\n{} **({})**'.format(role.name, len([member for member in server.members if ([r for r in member.roles if r.name == role.name])]))
    message += ''
    embed = discord.Embed(description=message.format(), colour=0x0080c0)
    embed.set_footer(text="Tip: to add a role from the list type the command !add/remove followed by the role.")
    await self.bot.say(embed=embed)



def setup(bot):
    n = Roles(bot)
    bot.add_cog(n)

如果有人可以帮助我如何工作,那将非常感谢。

python python-3.x discord discord.py
1个回答
0
投票

这都是为了寻找你需要的东西。

您希望在列出角色时更改某些内容,因此您应该在def _list()函数下进行检查。在那里,我们看到它遍历for role in server.roles:内的所有角色。

这可能是你想要改变的。通过在迭代它们之前对列表进行排序,您将能够根据需要完成它:

for role in sorted(server.roles, key=lambda r: r.name):

如果您将代码调整为上述代码,您基本上将使用内置的sorted()函数对列表进行排序。通过使用key参数,您可以为函数提供排序的基础,这将是嵌套对象的names属性。

© www.soinside.com 2019 - 2024. All rights reserved.