How do I create a new private text channel and add

2019-08-21 07:51发布

问题:

I'm creating a discord bot where a user will message the bot and

  1. the bot will create a new PRIVATE text channel; preferably on the same server as the bot
  2. the bot will add only the messaging user and an admin to the channel

I have been able to make a new channel using this question as a guide. I have not been able to make a private text channel or find a command that will allow me to do so. Does anyone know how to create a private text channel in discord.py and add 2 people (messaging user and an admin) to it?

回答1:

You can use Guild.create_text_channel to create a text channel with certain permissions overwrites. The below creates a channel that is visible only to the caller, the bot, and members with the "Admin" role (You'll need to change that to the appropriate role for your server)

from discord.utils import get

@bot.command()
async def make_channel(ctx):
    guild = ctx.guild
    member = ctx.author
    admin_role = get(guild.roles, name="Admin")
    overwrites = {
        guild.default_role: discord.PermissionOverwrite(read_messages=False),
        guild.me: discord.PermissionOverwrite(read_messages=True),
        admin_role: discord.PermissionOverwrite(read_messages=True)
    }
    channel = await guild.create_text_channel('secret', overwrites=overwrites)