【问题标题】:How to get rid of Text Channels from channel list in Discord.py如何从 Discord.py 的频道列表中删除文本频道
【发布时间】:2018-09-10 04:44:05
【问题描述】:

这里有一些示例代码:

for server in client.servers:
        for channel in server.channels:
            print(channel)

输出例如:

Text Channels
general
commands
Text Channels
main-channel
admin-channel
Text Channels
first-channel

我想发出命令 !br 将消息发送到每台服务器的第一个频道。但我不能,因为 Text Channels 行也算作频道

Here's little image what I mean by Text Channels

我总是收到这样的错误:

Cannot send messages in a non-text channel

所以我想在这些频道出现时跳过它们,或者将它们从频道列表中完全删除。

这里是命令的完整代码:

elif message.content.startswith('!br'):
    for server in client.servers:
        for channel in server.channels:
            if channel.permissions_for(server.me).send_messages:
                await client.send_message(channel, str(message.content[4:] )
                break

感谢您的回答。

【问题讨论】:

    标签: python discord.py channels


    【解决方案1】:

    如果您想检查频道是否为文本频道,您可以使用channel.type。文档here。 您只需要检查该频道的类型应为text

    elif message.content.startswith('!br'):
        for server in client.servers:
            for channel in server.channels:
                if (channel.permissions_for(server.me).send_messages) and (channel.type == "text"):
                    await client.send_message(channel, str(message.content[4:] )
                    break
    

    channel.type == discord.ChannelType.text 将检查频道是否为文本频道,以便您发送消息。

    【讨论】:

    • 出现此错误消息:AttributeError: 'Channel' object has no attribute 'replace'
    • 您现在可以检查一下吗?我已经更新了答案
    • 还是同样的错误信息:无法在非文本频道中发送消息
    • 您只能在文本频道上发送消息,因此您需要检查 channel.type==discord.ChannelType.text 。这是我认为的解决方法
    猜你喜欢
    • 2021-12-21
    • 2021-01-25
    • 2021-11-08
    • 2021-10-04
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    相关资源
    最近更新 更多