1
votes

I'm trying to make a completly new role in discord (with discord.py) but everything I try, doesn't work. I allready tried

await guild.create_role(name("R3KT")) #NameError: name 'guild' not defined

and

author = ctx.message.author
await client.create_role(author.server, name="role name") #NameError: name 'ctx' not defined

I've tried changing ctx and guild to 'client' but it still didn't work. if needed I'll send the whole code (without the discord name and bot key)

here's the entire code:

import discord

token = "bot-key"
client = discord.Client()


@client.event
async def on_ready():
    print("Bot is online and connected to Discord")


@client.event
async def on_message(message):

    message.content = message.content.upper()

    if message.author == client.user:
        return

    if message.content.startswith("AS HELLO"):
        await message.channel.send("works")

    elif message.content.startswith("AS CREATEROLE"):
        if str(message.author) == "myDiscName#6969":
            author = message.author
            await discord.create_role(author.server, name="R3KT")

    elif message.content.startswith("AS GIVEROLE"):
        if str(message.author) == "myDiscName#6969":
            print("give role")


client.run(token)
2
Can you post in your entire code (including your on_message or command events)Eric Jin
here, I posted the whole code. Hope you'll be able to find any flaws. (also as I've said, I replaced the disc name and bot key)Tilen

2 Answers

0
votes

In the second example you provided, it looks like you're using a mish-mash between the old d.py (0.16.x) docs and d.py rewrite (1.x).

Make sure you have the most up-to-date version (rewrite) installed, as async is no longer being maintained.

Here's an example with the command decorator (usage: !newrole Member)

@client.command()
async def newrole(ctx, *, rolename=None):
    if not rolename:
        await ctx.send("You forgot to provide a name!")
    else:
        role = await ctx.guild.create_role(name=rolename, mentionable=True)
        await ctx.author.add_roles(role)
        await ctx.send(f"Successfully created and assigned {role.mention}!")

The mentionable kwarg isn't compulsory - it defaults to False if not specified - I just set it to True for the example. You're also able to write your own permissions for the role.


And another example using on_message - recommended to use command decorator instead, as args are easier to deal with

async def on_message(message):
    args = message.content.split(" ")[2:] # 2: because prefix contains space
    if message.content.lower().startswith("as createrole"):
        role = await message.guild.create_role(name=" ".join(args))
        await message.author.add_roles(role)
        await message.channel.send(f"Successfully created and assigned {role.mention}!")

References:

0
votes

You don't want to check by name and number (as you can change that if you have nitro), you should check by ID which is unique to every user and immutable.

Also note that server is referred to as guild in discord.py.

    elif message.content.startswith("AS CREATEROLE"):
        if message.author.id == YOUR_ID_GOES_HERE: # find your id by right clicking on yourself and selecting "copy id" at the bottom of the menu of options
            author = message.author
            # create a role named "R3KT" that has the permissions "Administrator" that has the color "white" that is not displayed separately and is not mentionable by everyone
            await message.guild.create_role(name="R3KT", permissions=discord.Permissions(8), colour=discord.Colour.from_rgb(255, 255, 255), hoist=False, mentionable=False)

Permissions integer calculator

discord.Guild.create_role

With giving yourself a role:

    elif message.content.startswith("AS GIVEROLE"):
        if message.author.id == YOUR_ID_GOES_HERE:
            user = message.author
            await user.add_roles(message.guild.get_role(ROLE_ID_GOES_HERE), reason='Someone did a giverole bot command') # left click on the role and click "copy id"

discord.Member.add_roles

discord.Guild.get_role