1
votes

I'm trying to do a discord bot that creates a category and sets permissions to roles and users, taking the users id as arguments:

for x in range(2, len(args)):
member= client.get_user(int(args[x]))
await message.guild.categories[-1].set_permissions(member, send_messages = True)

where args is the string array that contains the command, the name of the category, and the user IDs. the usage should be:

!create category-name 0000000 1111111

I have some problems with that, because is like the bot can't see the members of the server, and the only user he can add is me, the owner of the server if specified in the first argument (000000 in my example). If i put someone else's ID, the bot is not gonna add permissions for that user in the category. I figured out that maybe the bot can't see other users, in fact if i put the line:

print(message.guild.members)

it is gonna print only the bot as member,

prints this: [<Member id=762749337700007946 name='RoomBzot' discriminator='1334' bot=True nick=None guild=>]

I have no idea of the reason why it counts the members but can't see the other users, what can I do?

3
What version of Discord.py are you using? (pip show Discord.py in console to see)Allister
It is likely you need to enable gateway intents, please see this answerderw
Bro thank you! It was the intent, i resolved adding this to the code: intents = discord.Intents.default() intents.members = TruePeppeBz

3 Answers

4
votes

Since the answer linked by derw is no longer available;

As OP indicates, I managed to solve this by instantiating the discord bot client as follows:

intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)

In addition I had to enable the "SERVER MEMBERS INTENT" Privileged Gateway Intent under my bot settings in the Discord developer portal:

Enabling Server Members Intent

0
votes

install version 1.4.2 of the library: pip install -U discord.py==1.4.2

0
votes

You don't need to split it like that, I don't think you know but you should really check out commands from here, specify your arguments for the async def function and then you can do stuff like this:

from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
token = "YourBotsTokenHere"
bot = commands.Bot(command_prefix='!', intents=intents) # Or whatever prefix you want

@bot.event
async def on_ready():
    print(f"Bot {bot.user} has connected to Discord!")

@bot.command()
async def create(ctx, type_, member: discord.Member, categoryName=None, *, name):
    # member is going to return a member object so u can do stuff like member.id or member.name, etc...
    if type_.lower() == "category":
        # Code to create a category with the name variable
        # some pseudo-code that might look similar to what u need
        role = get(member.server.roles, name="NameOfTheRole")
        await bot.add_roles(member, role)
        # insert a part of ur own code here for category thingy

    elif type_.lower() == "channel":
        # Code to create a channel with the name variable, category name must be also specified so we can check to what category the user is reffering to, also check if the user has the role to create that channel
        if categoryName == None: return
        catgName = categoryName.replace('-', ' ') # Replaces "-" with an empty space

bot.run(token)

As for the other stuff, enabling members intents works fine and I already wrote it in the example