Discord bot in Python for role creation

I’m working on a Discord bot that should create roles, but it’s not responding to my commands. I’ve tried different approaches, but I can’t get it to work properly. Here’s what I’ve got so far:

import discord

PREFIX = '>'

async def make_new_role(server, role_title):
    new_role = await server.create_role(name=role_title)
    return new_role

client = discord.Client()

@client.event
async def on_message(msg):
    if msg.content.startswith(f'{PREFIX}new_role'):
        parts = msg.content.split()
        if len(parts) > 1:
            role_title = parts[1]
            print(f'Attempting to create role: {role_title}')
            try:
                role = await make_new_role(msg.guild, role_title)
                await msg.author.add_roles(role)
                await msg.channel.send(f'Role {role_title} created and given to {msg.author.name}')
            except Exception as e:
                await msg.channel.send(f'Error creating role: {str(e)}')
        else:
            await msg.channel.send('Please provide a role name')

client.run('YOUR_BOT_TOKEN_HERE')

Can someone help me figure out why the bot isn’t responding to commands? I’m pretty new to Discord bot development, so any advice would be great. Thanks!

As someone who’s been tinkering with Discord bots for a while, I can tell you that the intents system can be a real headache when you’re starting out. I ran into this exact issue a few months back.

Here’s what worked for me:

  1. Make sure you’re using the latest discord.py version. The library has gone through some changes.

  2. In your code, explicitly define the intents you need. For role management, you’ll want:

intents = discord.Intents.default()
intents.message_content = True
intents.guild_messages = True
intents.guilds = True
  1. When initializing your client, pass these intents:
client = discord.Client(intents=intents)
  1. Double-check your bot’s permissions in the server. It needs the ‘Manage Roles’ permission.

  2. Finally, ensure your bot token is correct and you’ve invited the bot to your server with the right scopes.

Give these a shot and let me know if you’re still having trouble. Discord bot development can be tricky, but it’s rewarding once you get the hang of it!

hey man, i had the same problem. make sure u enabled the message content intent in ur discord developer portal. also, try using the commands extension like this:

@bot.command()
async def new_role(ctx, *, role_name):
# ur code here

it makes things way easier. good luck!

I’ve encountered similar issues while developing Discord bots. The problem might be that you’re using an older version of the discord.py library. Recent versions require you to explicitly enable message content intent.

Try updating your code like this:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix='>', intents=intents)

@bot.command()
async def new_role(ctx, *, role_name):
    try:
        role = await ctx.guild.create_role(name=role_name)
        await ctx.author.add_roles(role)
        await ctx.send(f'Role {role_name} created and given to {ctx.author.name}')
    except Exception as e:
        await ctx.send(f'Error creating role: {str(e)}')

bot.run('YOUR_BOT_TOKEN_HERE')

This approach uses the commands extension, which simplifies command handling. Don’t forget to update your bot’s intents in the Discord Developer Portal as well.