get_cog 没有 return 加载 cog,而是 None
get_cog doesn't return loaded cog but None instead
在我的机器人中,我有一个命令只打印出所有已加载齿轮的名称(以及稍后的描述)。
对于他们中的大多数人来说,它工作得很好,但它似乎有一个名为 RNG
的问题
for c in bot.cogs:
if c is None:
continue
else:
cog_list.append(c)
cog_list = sorted(cog_list)
print(cog_list)
>>> ['General', 'Misc', 'RNG']` # (all loaded cogs, so this is correct)
for cog in cog_list:
cog = bot.get_cog(cog.title())
print(cog)
>>> <general.General object at [...]> # 'General' cog
>>> <misc.Misc object at [...]> # 'Misc' cog
>>> None # 'RNG' cog, but it's not found?
# (expected <rng.RNG object at [...]>)
只是为了表明 RNG cog 已实际加载,这是 print(bot.cogs)
的输出:
{'General': <general.General object at [...]>, 'Misc': <misc.Misc object at [...]>, 'RNG': <rng.RNG object at [...]>}
我从文档中了解到,当找不到 cog 时会返回 None
,但我可以清楚地看到,RNG cog 已加载很多并且包含的命令运行良好。
现在我想知道它是否可能是这个名字,但对机器人执行 help RNG
也能正常工作。
有没有办法让 get_cog
找到我的 RNG 模块?
一些更具描述性的命名可以帮助您了解发生了什么
for cog_name in cog_list:
print(cog_name.title())
打印
General
Misc
Rng
请注意 RNG
已变为 Rng
。这是因为 .title()
字符串方法正在生成字符串 Title Case,其中每个单词都大写。因此,在您的搜索中,您正在查找 Rng
并期望它找到 RNG
,这不起作用,因为搜索区分大小写。
在我的机器人中,我有一个命令只打印出所有已加载齿轮的名称(以及稍后的描述)。
对于他们中的大多数人来说,它工作得很好,但它似乎有一个名为 RNG
for c in bot.cogs:
if c is None:
continue
else:
cog_list.append(c)
cog_list = sorted(cog_list)
print(cog_list)
>>> ['General', 'Misc', 'RNG']` # (all loaded cogs, so this is correct)
for cog in cog_list:
cog = bot.get_cog(cog.title())
print(cog)
>>> <general.General object at [...]> # 'General' cog
>>> <misc.Misc object at [...]> # 'Misc' cog
>>> None # 'RNG' cog, but it's not found?
# (expected <rng.RNG object at [...]>)
只是为了表明 RNG cog 已实际加载,这是 print(bot.cogs)
的输出:
{'General': <general.General object at [...]>, 'Misc': <misc.Misc object at [...]>, 'RNG': <rng.RNG object at [...]>}
我从文档中了解到,当找不到 cog 时会返回 None
,但我可以清楚地看到,RNG cog 已加载很多并且包含的命令运行良好。
现在我想知道它是否可能是这个名字,但对机器人执行 help RNG
也能正常工作。
有没有办法让 get_cog
找到我的 RNG 模块?
一些更具描述性的命名可以帮助您了解发生了什么
for cog_name in cog_list:
print(cog_name.title())
打印
General
Misc
Rng
请注意 RNG
已变为 Rng
。这是因为 .title()
字符串方法正在生成字符串 Title Case,其中每个单词都大写。因此,在您的搜索中,您正在查找 Rng
并期望它找到 RNG
,这不起作用,因为搜索区分大小写。