How to get all text channels using discord.py?

Veestire picture Veestire · Mar 23, 2018 · Viewed 18.3k times · Source

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

Answer

Benjin picture Benjin · Mar 23, 2018

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)