按字母顺序列出角色

时间:2018-08-30 17:44:15

标签: python python-3.x discord discord.py

您好,到目前为止,我正在尝试按字母顺序列出角色,但是我仍然能够在不和谐的服务器上列出角色。我不确定如何在.py中按字母顺序列出角色。我到处搜索都没有成功。 / p>

这就是我正在使用的。

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)

如果有人可以帮助我解决这个问题,将不胜感激。

1 个答案:

答案 0 :(得分:0)

这一切都是为了寻找您需要的东西。

您要在列出角色时进行一些更改,因此应在def _list()功能下进行检查。在其中,我们看到它迭代了for role in server.roles:内部的所有角色。

这可能是您要更改的内容。通过在遍历列表之前对列表进行排序,您将能够根据需要完成列表:

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

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