【问题标题】:Discord.py Copy channel with permissionsDiscord.py 复制具有权限的频道
【发布时间】:2021-05-02 08:53:01
【问题描述】:

我想做一个 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)

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

【问题讨论】:

  • 使用* 与您使用seen in the docs 的方式不同。您可能想要这样做:(ctx, id:int=None, *, channame)
  • 啊,是的,这不是我在我的机器人中的代码。我只是写了它,并没有提到。

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


【解决方案1】:

有一个非常方便的复制频道的方法,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) 时它也可以工作。

参考:

【讨论】:

  • 是的,我知道有 Channel.clone 选项,但问题是我想更改频道名称(是的,有 await channel.edit(name))但我可能想将频道转移到另一个公会。
  • 如果你想用另一个名字克隆它,你可以在克隆它时传递名字kwarg,await channel.close(name="new name")
  • 名称不是主要问题。我可能想将频道转移到另一个公会。(如果可以使用channel.clone 将频道克隆到另一个公会,我想知道。
【解决方案2】:

所以你提供的代码有一些问题:

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

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

  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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-16
    • 2023-03-21
    • 2021-09-01
    • 2021-12-10
    相关资源
    最近更新 更多