【问题标题】:Discord Python Delete Category and Channels within using IDDiscord Python 使用 ID 删除类别和频道
【发布时间】:2021-08-28 20:04:22
【问题描述】:
#deleted unused registered channels
@bot.command(pass_context=True)
#so that only an admin can run this command
@commands.has_role('Admin')
async def unregister(ctx, catID):
  cat = discord.utils.get(ctx.guild.categories, id = catID)

  #await ctx.channel.send(cat)

  for channel in cat.voice_channels:
    await channel.delete()

  for channel in cat.text_channels:
    await channel.delete()
  
  cat.delete()

这是我目前拥有的代码。我也用 client.get_channel() 尝试过。我似乎收到类别无法访问 .voice_channels 和 .text_channels 的错误,或者 cat 是“NoneType”。我是 python 和编写不和谐机器人的新手,所以对这个命令的任何帮助都会很棒。谢谢!

该命令旨在获取您可以通过右键单击并在 discord 上复制类别来获得的类别 ID。然后删除它下面的每个语音和文本通道。然后删除自己。

【问题讨论】:

  • 究竟是什么错误?

标签: python discord discord.py


【解决方案1】:

您的代码本身几乎是正确的,但可以在很多地方进行简化。

cat完全没有必要,所以也不需要定义,因为我们可以简单地说如下:

category: discord.CategoryChannel

现在我们只需要输入 ID,剩下的就会自己处理了。根据定义,我们现在可以进一步构建代码:

@bot.command(pass_context=True)
@commands.has_role('Admin')
async def unregister(ctx, category: discord.CategoryChannel):
    delcategory = category # delcategory is our ID (category)
    channels = delcategory.channels # Get all channels of the category

    for channel in channels: # We search for all channels in a loop
        try:
            await channel.delete() # Delete all channels
        except AttributeError: # If the category does not exist/channels are gone
            pass
    await delcategory.delete() # At the end we delete the category, if the loop is over

【讨论】:

    猜你喜欢
    • 2020-04-18
    • 2021-01-12
    • 2021-09-21
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 2021-05-14
    • 2021-07-13
    • 2022-01-12
    相关资源
    最近更新 更多