gpt4 book ai didi

python - Discord bot 未按名称查找 channel

转载 作者:太空宇宙 更新时间:2023-11-04 04:00:21 24 4
gpt4 key购买 nike

我正在制作一个 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":

关于python - Discord bot 未按名称查找 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58470132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com