我的 cogs 文件没有加载到我的主文件中 (discord.py)
Posted
技术标签:
【中文标题】我的 cogs 文件没有加载到我的主文件中 (discord.py)【英文标题】:My cogs file didn't load into my main file (discord.py) 【发布时间】:2021-10-31 13:04:11 【问题描述】:当我尝试运行我的 discord 机器人时,cogs 文件似乎不起作用。我什至尝试在 Discord 中使用负载。我已经观看了许多有关 cogs 的视频并阅读了文档,但我找不到任何解决方案。任何人都可以发现错误吗? 这是我的 cogs 文件
import discord
from discord.ext import commands, tasks
from itertools import cycle
class UpdateCode(commands.Cog) :
def _init_(self, client) :
self.client = client
@ commands.Cogs.listener()
async def on_ready(self):
print("We have logged in as 0.user".format(self.client))
self.status_swap.start()
self.status = cycle([
" Unanswered Question of Life",
" Self - Referential Paradox",
" Near-infinite density?",
" Dark matter ?",
" Measurement of the speed of light in one straight line",
" Schrodinger's cat ???"
"436c69636b2074686973206c696e6b20666f72206672656520766275636b7320212121212121203a200a68747470733a2f2f7777772e796f75747562652e636f6d2f77617463683f763d6451773477395767586351 (try to decrypt this)",
"The light side of Discord is the path of many unnatural abilities"
])
@ tasks.loop(minutes = 5)
async def status_swap(self):
await self.client.change_presence(activity = discord.Game(next(self.status)))
def setup(client) :
client.add_cog(UpdateCode(client))
这是我的主文件
import discord
import os
from discord.ext import commands, tasks
from online import keep_alive
client = commands.Bot(command_prefix = "!")
@ client.command()
async def load(ctx,extension) :
client.load_extension(f"cogs.extension")
@ client.command()
async def unload(ctx,extension) :
client.unload_extension(f"cogs.extension")
for filename in os.listdir('./cogs') :
if filename.endswith('.py') :
client.load_extension(f'cogs.filename[:-3]')
keep_alive()
client.run(os.getenv('MATH_VAR'))
这是错误
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 606, in _load_from_module_spec
spec.loader.exec_module(lib)
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/runner/Math-Bot/cogs/update.py", line 6, in <module>
class UpdateCode(commands.Cog) :
File "/home/runner/Math-Bot/cogs/update.py", line 11, in UpdateCode
@ commands.Cogs.listener()
AttributeError: module 'discord.ext.commands' has no attribute 'Cogs'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "main.py", line 18, in <module>
client.load_extension(f'cogs.filename[:-3]')
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 678, in load_extension
self._load_from_module_spec(spec, name)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 609, in _load_from_module_spec
raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.update' raised an error: AttributeError: module 'discord.ext.commands' has no attribute 'Cogs'
如果您知道如何解决此错误,请评论我的代码。谢谢。
【问题讨论】:
【参考方案1】:分析错误
分解第一部分的错误:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 606, in _load_from_module_spec
spec.loader.exec_module(lib)
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/runner/Math-Bot/cogs/update.py", line 6, in <module>
class UpdateCode(commands.Cog) :
File "/home/runner/Math-Bot/cogs/update.py", line 11, in UpdateCode
# First hint which points to your code.
@ commands.Cogs.listener()
# Second hint which points the problem in your code.
AttributeError: module 'discord.ext.commands' has no attribute 'Cogs'
引用现有代码
其次,您可以参考UpdateCode
Cog 的定义方式,它是基于command.Cog
而不是command.Cogs
。 class UpdateCode(commands.Cog)
注意丢失的's'。
结论
基于我提出的前两点,我们可以得出结论,当您通过在词 'Cog 中包含 's' 来定义 @ commands.Cogs.listener()
装饰器时存在一个小错误'。
参考文献
https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.Cog.listener【讨论】:
以上是关于我的 cogs 文件没有加载到我的主文件中 (discord.py)的主要内容,如果未能解决你的问题,请参考以下文章
我的 React 组件都没有加载到我的 html.erb 文件中