如何使用 discord.py 获取所有文本频道? [英] How to get all text channels using discord.py?

查看:144
本文介绍了如何使用 discord.py 获取所有文本频道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I need to get all channels to make a bunker command, which makes all channels read only.

解决方案

Assuming you are using the async branch, the Client class contains guilds, which returns a list of guild classes that the bot is connected to. Documentation here: https://discordpy.readthedocs.io/en/stable/api.html#discord.Client.guilds

Iterating over this list, each guild class contains channels, which returns a list of Channel classes that the server has. Documentation here: https://discordpy.readthedocs.io/en/stable/api.html#discord.Guild.channels

Finally, iterating over this list, you can check each Channel class for different properties. For example, if you want to check that the channel is text, you would use channel.type. Documentation here: https://discordpy.readthedocs.io/en/stable/api.html#discord.abc.GuildChannel

A rough example of how you can make a list of all Channel objects with type 'Text':

text_channel_list = []
for server in Client.guilds:
    for channel in server.channels:
        if str(channel.type) == 'text':
            text_channel_list.append(channel)

To compare to 'text', channel.type must be a string.

For older versions of discord.py, commonly referred to as the async branch, use server instead of guild.

text_channel_list = []
for server in Client.servers:
    for channel in server.channels:
        if str(channel.type) == 'text':
            text_channel_list.append(channel)

这篇关于如何使用 discord.py 获取所有文本频道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆