How to get all text channels using discord.py?

2020-05-09 05:36发布

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

2条回答
放我归山
2楼-- · 2020-05-09 06:23

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

Iterating over this list, each Server class contains channels, which returns a list of Channel classes that the server has. Documentation here: http://discordpy.readthedocs.io/en/latest/api.html#discord.Server.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: http://discordpy.readthedocs.io/en/latest/api.html#discord.Channel

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

text_channel_list = []
for server in Client.servers:
    for channel in server.channels:
        if channel.type == 'Text':
            text_channel_list.append(channel)
查看更多
祖国的老花朵
3楼-- · 2020-05-09 06:33

They changed Client.servers to Client.guilds in new version of discord.py.
You can also use bot instead of Client. And guild.text_channels to get all text channels.
For all channels you can use bot.get_all_channels()

text_channel_list = []
for guild in bot.guilds:
    for channel in guild.text_channels:
        text_channel_list.append(channel)
查看更多
登录 后发表回答