【问题标题】:Changing color roles discord改变颜色角色不和谐
【发布时间】:2018-10-12 14:53:08
【问题描述】:

首先,我想指出我是python的初学者。

我正在尝试编写一个命令,允许用户通过机器人更改其角色的颜色。但是,我遇到了很多我无法找到答案的问题。

第一个问题是我无法访问调用命令的用户角色。 然而,我决定跳过它,直接进入一个特定的角色。 所以我做了这个代码:

@client.command(pass_context=1)
async def changecolor(ctx, NewColor):
    author = ctx.message.author
    server = ctx.message.author.server
    dictOfColors = { '1' : discord.Color.default(),
                     '2' : discord.Color.teal(),
                     '3' : discord.Color.dark_teal(),
                     '4' : discord.Color.green(),
                     '5' : discord.Color.dark_green(),
                     '6' : discord.Color.blue(),
                     '7' : discord.Color.purple(),
                     '8' : discord.Color.dark_purple(),
                     '9' : discord.Color.magenta(),
                     '10' : discord.Color.dark_magenta(),
                     '11' : discord.Color.gold(),
                     '12' : discord.Color.dark_gold(),
                     '13' : discord.Color.orange(),
                     '14' : discord.Color.dark_orange(),
                     '15' : discord.Color.red(),
                     '16' : discord.Color.dark_red() }
    role = discord.utils.get(server.roles, name='New Member')
    if NewColor in dictOfColors:
        await client.edit_role(server, role, colour=NewColor)

但是当我尝试时:.changecolor 5 收到此错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'name'

你能告诉我我做错了什么吗?

【问题讨论】:

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


【解决方案1】:

您可以使用角色converter 从角色提及中获取角色。我也会这样做,以便用户传递颜色的名称而不是数字:

@client.command(pass_context=True)
async def changecolor(ctx, role: discord.Role, *, color):
    if role not in ctx.message.author.roles:
        await bot.say("You do not have the role " + role.name)
        return
    color = '_'.join(color.lower().split())
    if not hasattr(discord.Color, color):  # We could also use inspect.ismethod to only accept classmethod names
        await bot.say("I do not recognize the color " + color)
        return
    await client.edit_role(ctx.message.server, role, colour=getattr(discord.Color, color)())

然后你可以用类似的东西来调用它

!changecolor @NewMember dark gold

【讨论】:

  • 也谢谢你,它工作得很好,但我想让这个代码用户可以切换他的角色颜色,但不能切换其他角色。有什么选择吗?因为现在我需要在这里设置确切的角色名称role = discord.utils.get(server.roles, name='Role Name') 我也想保留我的代码,因为我只是不喜欢使用我不理解的代码,例如 `color = '_'.join( color.lower().split()) `我不知道它是如何工作的。
  • @ArtFiNCH 是的,您可以查看role not in ctx.message.author.roles。这是因为角色转换器给了我们一个Role 对象而不是一个字符串。
  • '_'.join(list_of_strings) 使用_ 将字符串连接在一起。所以'_'.join(['a', 'b', 'c'])'a_b_c'phrase.lower().split() 将字符串转换为小写,然后将其拆分为空格。所以'A b C' 变成了['a', 'b', 'c']。我这样做是为了将颜色作为看起来很自然的字符串并将其转换为方法名称:Dark Gold -> dark_gold 例如
  • 好的,我测试了它,它比我的代码运行得更好,但我仍然需要更多解释。我已经知道 .join 的那一行是用于转换的。怎么说呢,我真的是初学者。 async def changecolor(ctx, role: discord.Role, *, color):是什么意思? (ctx, role: discord.Role, *, color) 第一个是 ctx 好的,第二个意味着我们正在从 discord.Role(?) 赋予角色,但是第三个是什么?这是否意味着我们可以赋予多个角色?
  • @ArtFiNCH role: discord.Roleconverter。通过提供类型提示,我们告诉discord.py 截取来自用户的字符串并将其转换为我们的Role 对象。 *, color 语法告诉discord.pyinto a single string 之后合并所有输入。所以对于输入!changecolor @SomeRole a b c,我们得到代表该角色的Role对象和字符串"a b c"
【解决方案2】:

将最后一行更改为

await client.edit_role(server, role, colour=dictOfColors[NewColor])

您将字典中所需颜色的编号分配给colour 属性,而不是该键上的值,即实际颜色。

【讨论】:

  • @ArtFiNCH 您好,我注意到自从您加入 SO 以来,您一直没有接受任何问题的答案。您应该考虑选择一个对您有帮助的答案并接受它,以便其他人可以看到解决方案。查看What should I do when someone answers my question?
猜你喜欢
  • 2022-01-12
  • 2020-12-21
  • 2021-04-23
  • 2021-05-31
  • 2021-10-27
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
  • 2020-12-25
相关资源
最近更新 更多