如何使用discord.py获取所有文本频道?

Vee*_*ire 5 channel discord discord.py

我需要让所有频道都发出一个 bunker 命令,这使得所有频道都是只读的。

use*_*876 15

他们改变了Client.serversClient.guilds新版本discord.py(1.0)。
您还可以使用bot代替Client ( info )。
guild.text_channels获取所有文本频道。
对于您可以使用的所有频道bot.get_all_channels()

text_channel_list = []
for guild in bot.guilds:
    for channel in guild.text_channels:
        text_channel_list.append(channel)
Run Code Online (Sandbox Code Playgroud)

  • @Jonathan 只需使用“channel.name”作为其名称,或者您也可以将通道对象转换为字符串“str(channel)”,它将返回其名称 (3认同)

Ben*_*jin 9

假设您正在使用 async 分支,Client该类包含servers,它返回Server机器人连接到的类列表。此处的文档:http : //discordpy.readthedocs.io/en/latest/api.html#discord.Client.servers

遍历此列表,每个Server类都包含channels,它返回Channel服务器拥有的类列表。此处的文档:http : //discordpy.readthedocs.io/en/latest/api.html#discord.Server.channels

最后,遍历此列表,您可以检查每个Channel类的不同属性。例如,如果您想检查频道是否为文本,您可以使用channel.type. 此处的文档:http : //discordpy.readthedocs.io/en/latest/api.html#discord.Channel

一个粗略的例子,说明如何制作Channel“文本”类型的所有对象的列表:

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

要与 'text' 进行比较,channel.type 必须是字符串