1
votes

I am trying to make a discord bot that creates a role when someone sends a message. For some reason, It keeps saying 'ctx' is not defined. How would I fix this error?

@client.event
async def on_message(message):
    server=ctx.message.server
    perms=discord.Permissions(administrator=true)
    await client.create_role(server,name='testrole', colour=discord.Colour(0x0000FF),permissions=perms)

when i change

async def on_message(message):

to

async def on_message(ctx):

it says 'message' is not defined. I do not understand how I could define both of these because when I add the undefined part as shown below, It still says 'ctx' is not defined or 'message' is not defined.

async def on_message(message,ctx):
async def on_message(ctx,message):

update: I didn't have (pass_context=True) but now I know more

1
Do you know which version of the discord.py library you are using? - qspitzer
on_message does not have ctx . ctx . You can get the guild (not server) or the channel for the message object. Also read the docs - InsertCheesyLine

1 Answers

1
votes

Try this:

@client.event
async def on_message(message):
    guild=message.guild
    perms=discord.Permissions(administrator=True)
    await guild.create_role(name='testrole', colour=discord.Colour(0x0000FF),permissions=perms)

P.S. your bot need some permissions to create this Role.