Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

python - Discord.py Copy channel with permissions

I wanted to make a CopyChannel(Permissions) command but something is wrong in my code:

@bot.command()
async def copych(ctx, *, channame, id: int = None):
    await ctx.message.delete()
    if id == None:
        chan = ctx.channel
    else:
        chan = bot.get_channel(id=id)
    chan_perm = chan.overwrites
    await ctx.guild.create_text_channel(name=channame, overwrites=chan_perm)

There are no Errors and the channel is getting created but it doesn't overwrite the permissions.

I tried this here aswell but same Thing:

@bot.command()
async def copych(ctx, *, channame, id: int = None):
    await ctx.message.delete()
    if id == None:
        chan = ctx.channel
    else:
        chan = bot.get_channel(id=id)
    chan_perm = chan.overwrites
    f = await ctx.guild.create_text_channel(name=channame)
    await f.edit(overwrites=chan_perm)

Can someone tell me where the problem in the code is.
Thanks in advance

question from:https://stackoverflow.com/questions/65937485/discord-py-copy-channel-with-permissions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There's a really handy method to copy channels, TextChannel.clone.

@bot.command()
async def copych(ctx, channel: discord.TextChannel=None):
    if channel is None:
        channel = ctx.channel

    await channel.clone()

Also instead of passing the channel ID when invoking the command you can use TextChannelConverter so it will also work when you mention the channel (#channel).

Reference:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...