【问题标题】:Listing roles alphabetically按字母顺序列出角色
【发布时间】:2019-02-05 17:40:02
【问题描述】:

您好,到目前为止,我正在尝试按字母顺序列出角色我能够在我的不和谐服务器上列出角色位我不确定如何在 .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)

如果有人可以帮助我了解这是如何工作的,那将不胜感激。

【问题讨论】:

  • 嗨,山,欢迎来到 SO!一些建议,以便您获得更好的响应。 1)“我已经到处搜索了”——如果您可以包含一些搜索结果(“您尝试过的内容”),那么这对我们有很大帮助。 2) 感谢您提供代码!一个很大的改进,IMO,将使它“最小化”,并告诉我们它做什么以及你想要它做什么。这是一个 非常 大的代码转储,用于像字母排序这样简单的事情。 3)你试过内置的“排序”功能吗?

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


【解决方案1】:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2011-05-03
    • 2012-02-17
    相关资源
    最近更新 更多