Python Discord bot未按名称查找频道
我正在制作一个Discord机器人,当它启动时,我让它在协会的文本频道中搜索,并检查特定的频道名称。如果我打印出频道列表,它会找到所有频道,尽管如果我尝试Python Discord bot未按名称查找频道,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我正在制作一个Discord机器人,当它启动时,我让它在协会的文本频道中搜索,并检查特定的频道名称。如果我打印出频道列表,它会找到所有频道,尽管如果我尝试如果channel.name为“general”它不会将标志更改为true async def on_ready(): print("Bot is ready.") has_channel = False for guild in client.guilds: for channel in guild.t
如果channel.name为“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进行比较吗?我怀疑
频道。name
不是str
类型,因此is
的计算结果为false。尝试:
if str(channel.name) == "general":
正是这样,我甚至尝试了
str(channel.name)的“general”:
但没有成功。谢谢你给我指出了正确的方向,我在一些文档中读到了channel.name
是typestr
,但我想不是真的。再次感谢。