python - 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()
解决方案
要获取文本通道,您必须使用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()
推荐阅读
- c# - DI容器每次都提供新实例?
- amazon-web-services - 如何使用 API Gateway 将 Cognito 用户池组传递到 Lambda 上下文?
- c++ - Crypto++:CFB_Mode_ExternalCipher 不工作
- xamarin - iOS 13 披露按钮 (UITableViewCellAccessory.DetailDisclosureButton) 未触发事件 Xamarin
- r - 在具有条件的 dplyr 中使用 mutate
- apache-storm - 我在 Windows 上运行 nimbus apachestorm 2.0.0 时遇到问题
- go - 如何在 Go 中创建共享队列?
- c++ - 使用模运算符在偶数和奇数对象之间交替
- reactjs - React Component vs Element 在同一文件中的可重用性
- c# - 通过 Entity Framework Core 将多个数据库连接到 .NET Core 项目