改变不和谐角色的颜色

时间:2021-03-30 00:24:27

标签: python discord.py

一切都设置正确,机器人处于不和谐状态,已连接等。 这段代码:

import discord
import random

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))
    color2 = "%06x" % random.randint(0, 0xFFFFFF)
    print(color2)

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('!randomcolor'):
        server = client.get_guild('')
        role = "Rainbow tester"
        color = "%06x" % random.randint(0, 0xFFFFFF)
        await role.edit(server=server, role=role, colour=color)
        await message.channel.send('Trying that...')

client.run('TOKEN')

出现此错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 22, in on_message
    await role.edit(server=server, role=role, colour=color)
AttributeError: 'str' object has no attribute 'edit'

如果有人能找到我哪里出错或我犯了什么错误,请帮忙!

2 个答案:

答案 0 :(得分:0)

试试:

{}

答案 1 :(得分:0)

我会为此使用 discord.Color.random() 函数,因为问题似乎出现在您的 color 行中。

将您的代码重写为以下内容:

from discord.utils import get

    if message.content.startswith('!randomcolor'):
        guild = client.get_guild(GuildID)
        role = get(guild.roles, name="Rainbow tester") # Get the role
        color = discord.Color.random() # Choose a random color
        await role.edit(server=guild, role=role, colour=color)
        await message.channel.send('Trying that...')

我们做了什么?

  • 进口from discord.utils import get
  • 改变了我们获得角色的方式:(get(guild.roles, name="Rainbow tester")role = discord.utils.get(message.guild.roles, name='Rainbow tester'
  • 使用 discord.Color.random() 函数选择随机颜色
相关问题