Discord.py 使用权限复制频道

时间:2021-01-28 12:53:00

标签: python discord.py discord.py-rewrite

我想创建一个 CopyChannel(Permissions) 命令,但我的代码有问题:

@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)

没有错误,频道正在创建,但没有覆盖权限。

我也在这里尝试过,但同样的事情:

@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)

谁能告诉我代码的问题在哪里。
提前致谢

2 个答案:

答案 0 :(得分:0)

复制频道有一个非常方便的方法,TextChannel.clone

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

    await channel.clone()

此外,您可以使用 TextChannelConverter 代替在调用命令时传递频道 ID,这样在您提及频道 (#channel) 时它也可以使用。

参考:

答案 1 :(得分:0)

因此,您提供的代码存在一些问题:

  1. 这一行async def copych(ctx, *, channame, id: int = None)。正如我在之前的评论 relating to the docs 中提到的,您使用 * 的方式不正确。
  2. 如果你在写了 string 的地方写了一个 int 而不是 id: int=None,如果它不起作用,你会得到一个大错误。

那么我们如何解决这些问题?

  1. 再次参考文档,您需要重新排列这一行。如下面的代码所示,您可以按照我所示重新排列它。您只需要一个 int,您的 ID,以及一个频道名称,您的 channame 变量。
  2. 使用typing.Optional,如果执行命令的人没有指定频道ID,您可以使其指向当前频道,ctx.channel。你可以看看another example of this being used here
@bot.command()
async def copych(ctx, id: typing.Optional[int], *, channame="Channel"):
    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)