Discord.py bot that creates channel via given arguments and gives specific permissions to the user for that channel

Viewed 344

I'm looking to make a personal discord bot that will be used by moderators to create channels for specific users. It will be called in the form of "$create firstName lastName category user" which category is the category the channel is being created in and user is the user's username with the tag. I have managed to get it to create the channel but I am clueless as to how to give specific permissions to a user for that newly created channel. Here is my current def for creating the channel from the command

@bot.command()
async def create(ctx, firstName, lastName, category, user):
    print(ctx)
    role = discord.utils.get(ctx.guild.roles, name="Moderator")
    if role in ctx.author.roles:
        cat = discord.utils.get(ctx.guild.categories, name=category)
        await ctx.guild.create_text_channel(firstName + ' ' + lastName, category=cat)
        #insert permission changes for user id: user id is the actual username and tag

    else:
        await ctx.send("You do not have permissions to create a channel. Only Moderators do")```
1 Answers

One thing you might want to do, unrelated to your question, is to add a check as to whether the category was found. The moderator could enter a nonexistent category name, and your command would fail without an error message in that case. You can do that similar to your other check:

cat = discord.utils.get(ctx.guild.categories, name=category)
if not cat:
    await ctx.send("This category does not exist.")
    return
await ctx.guild.create_text_channel(firstName + ' ' + lastName, category=cat)

Now, to add permissions, you have two options. You could create the channel and then edit its permissions, but you can also pass a dict of permission overwrites when you create the channel. That's the approach we'll take.

When we're passing overwrites in a dict, the key should be either a Member or Role, and a PermissionOverwrite should be the value. In this case, assuming we want to keep all the default category permissions but add the user, we'll want a dict with one item: the Member who is specified in the user parameter and the corresponding PermissionOverwrite to grant access

First, we need to convert your user to a Member object. The "easy" way to do this would be to use a discord.ext.commands.MemberConverter. The downside to this is that it searches for a Member not only through username#discrim but also through other methods like user ID and mention. If you strictly require that only the username#discrim format can be used, you'll need to work around this. We'll also want to check to see if the user was found:

try:
    member = discord.ext.commands.MemberConverter().convert(ctx, user)
except discord.ext.commands.MemberNotFound:
    await ctx.send("Member not found.")

Now, with our member object, we can create the dict of PermissionOverwrites. Then, we can create our text channel:

overwrites = {member: discord.PermissionOverwrite(read_messages=True)}
await ctx.guild.create_text_channel(firstName + ' ' + lastName, category=cat, overwrites=overwrites)

The channel should now be created with the overwrites passed (i.e. allowing read access for a user).

Related