我想创建一个 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)
谁能告诉我代码的问题在哪里。
提前致谢
答案 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)
因此,您提供的代码存在一些问题:
async def copych(ctx, *, channame, id: int = None)
。正如我在之前的评论 relating to the docs 中提到的,您使用 *
的方式不正确。string
的地方写了一个 int
而不是 id: int=None
,如果它不起作用,你会得到一个大错误。那么我们如何解决这些问题?
int
,您的 ID,以及一个频道名称,您的 channame
变量。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)