Skip to main content
 首页 » 编程设计

python之Discord bot 未按名称查找 channel

2024年11月01日8zdz8207

我正在制作一个 Discord 机器人,当它启动时,我让它搜索公会文本 channel 并检查特定的 channel 名称。如果我打印出 channel 列表,它会找到所有 channel ,尽管如果我尝试 if channel.name is "general" 它不会将标志更改为 true。

async def on_ready(): 
    print("Bot is ready.") 
    has_channel = False 
    for guild in client.guilds: 
        for channel in guild.text_channels: 
            print(channel.name) 
            if channel.name is "general": 
                has_channel = True 
        print(has_channel) 

这是我的代码,这是输出:

Bot is ready. 
general 
streaming-updates 
welcome 
goodbye 
streaming-schedules 
False 

您知道为什么它不与 if 语句中的 true 进行比较吗?

请您参考如下方法:

我怀疑 channel.name 不是 str 类型,因此 is 的计算结果为 false。尝试:

if str(channel.name) == "general":