首页 > 解决方案 > Discord Bot 从列表中删除频道

问题描述

我现在正在用 python 制作一个不和谐的机器人,并想添加一个功能来删除公会中频道名称列表中的所有频道。我尝试通过存储在“ctx.message.guild.channel”列表中的所有频道并检查它们是否在我的频道名称列表中,但它没有找到频道,所以没有任何反应。谢谢你已经帮助我了!


channel_names = ["channel1","channel2","channel3"]

@commands.command()
async def clear(self, ctx):
     for channel in list(ctx.message.guild.channel):
           if channel in channel_names:
                await channel.delete()

标签: pythondiscord.py

解决方案


要获取文本通道,您必须使用text_channels然后 text_channel.name 进行比较。

channel_names = ["channel1","channel2","channel3"]

@commands.command()
async def clear(self, ctx):
     for channel in list(ctx.message.guild.text_channels):
           if channel.name in channel_names:
                await channel.delete()

推荐阅读