Everything is set up right, the bot is in the discord, connected, etc. This code:
JavaScript
x
25
25
1
import discord
2
import random
3
4
client = discord.Client()
5
6
@client.event
7
async def on_ready():
8
print('We have logged in as {0.user}'.format(client))
9
color2 = "%06x" % random.randint(0, 0xFFFFFF)
10
print(color2)
11
12
@client.event
13
async def on_message(message):
14
if message.author == client.user:
15
return
16
17
if message.content.startswith('!randomcolor'):
18
server = client.get_guild('')
19
role = "Rainbow tester"
20
color = "%06x" % random.randint(0, 0xFFFFFF)
21
await role.edit(server=server, role=role, colour=color)
22
await message.channel.send('Trying that...')
23
24
client.run('TOKEN')
25
Gives this error:
JavaScript
1
8
1
Ignoring exception in on_message
2
Traceback (most recent call last):
3
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
4
await coro(*args, **kwargs)
5
File "main.py", line 22, in on_message
6
await role.edit(server=server, role=role, colour=color)
7
AttributeError: 'str' object has no attribute 'edit'
8
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:
JavaScript
1
9
1
from discord.utils import get
2
3
if message.content.startswith('!randomcolor'):
4
guild = client.get_guild(GuildID)
5
role = get(guild.roles, name="Rainbow tester") # Get the role
6
color = discord.Color.random() # Choose a random color
7
await role.edit(server=guild, role=role, colour=color)
8
await message.channel.send('Trying that...')
9
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