如何使用 discord.py 用一个命令删除 discord 服务器中的所有文本和语音频道?

时间:2021-01-14 23:58:51

标签: discord.py

我需要使用 discord.py 发出命令以删除不和谐服务器中的每个频道(文本和语音)。有人可以给我提供代码来做到这一点。现在我有这个,它只会删除文本频道。我如何同时为两者制作它?

@bot.command(name="deleteall")
async def delete_channels(context):
    [await channel.delete() 
    for channel in context.guild.text_channels]

2 个答案:

答案 0 :(得分:0)

这似乎是一个奇怪的破坏性命令,您应该小心使用它,并且一定要添加权限检查。

要删除语音通道,您只需重复您已经编写的代码,但只需使用 voice_channels 列表。 例如

[await channel.delete() for voiceChannel in context.guild.voice_channels]

答案 1 :(得分:0)

@bot.command()
async def delchannels(ctx):
   for c in ctx.guild.channels: # iterating through each guild channel
      await c.delete()

您还可以使用以下方法专门针对文本频道或语音频道进行更改:

@bot.command()
async def delchannel(ctx, channel: discord.TextChannel):
   await channel.delete()

并更改特定频道类型的参数类型:“VoiceChannel”或“CategoryChannel”。