克隆和删除频道:Discord.py

时间:2020-10-10 09:00:23

标签: python discord discord.py

@client.command()
@commands.is_owner()
async def nuke(ctx, channel_name):
    existing_channel = discord.utils.get(guild.channels, name=channel_name)
    if existing_channel is not None:
        await clone(name=channel_name,reason="Has been nuked")
        await existing_channel.delete()
    else:
        await ctx.send(f'No channel named **{channel_name}** was found')

我已经看到不和谐的多个机器人可以通过nuke命令执行此操作,但是我想自己学习如何执行此操作。问题是,我的机器人无法检测到我提到了一个频道(如附图所示)。我发现了一个与此类似的问题:“正确地设置频道”,但他们使用的是嵌齿轮。救命? This is the attached picture

2 个答案:

答案 0 :(得分:1)

您使用的clone()错误,其Channel.clone()不仅clone()

下面是修改后的代码

@client.command()
@commands.is_owner()
async def nuke(ctx, channel_name):
    existing_channel = discord.utils.get(guild.channels, name=channel_name)
    if existing_channel is not None:
        await existing_channel.clone(reason="Has been nuked")
        await existing_channel.delete()
    else:
        await ctx.send(f'No channel named **{channel_name}** was found')

答案 1 :(得分:1)

Poojan已经指出您使用了错误的命令来克隆。 我正在使用他们的代码,但在更改中您希望通道名称成为可点击的#example-channel。

标记频道时,机器人会看到:<#123456789>,而不是#example-channel。我们只需要数字即可获取频道ID。代码的前两行。

@client.command()
@commands.is_owner()
async def nuke(ctx, channel_name):
    channel_id = int(''.join(i for i in channel_name if i.isdigit())) 
    existing_channel = client.get_channel(channel_id)
    if existing_channel:
        await existing_channel.clone(reason="Has been nuked")
        await existing_channel.delete()
    else:
        await ctx.send(f'No channel named **{channel_name}** was found')


尝试调试时,一个好主意是print()不同的对象,以便您可以看到它们是什么。

print(channel_name)将向您显示标记时机器人看到的内容。您也许可以由此看出为什么找不到任何“频道名称”。