Everything is set up right, the bot is in the discord, connected, etc. This code:
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')
Gives this error:
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'
If anyone can find where I went wrong or any errors I’ve made, please help!
Advertisement
Answer
I would use the discord.Color.random() function for that as it looks like the problem occurs in your color-line.
Re-write your code to the following:
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...')
What did we do?
- Imported
from discord.utils import get - Changed the way we get the role: (
get(guild.roles, name="Rainbow tester")orrole = discord.utils.get(message.guild.roles, name='Rainbow tester') - Used the
discord.Color.random()function to choose a random color