Python discord.ext.commands.errors.ExtensionNotFound:Extension';齿轮。随机';无法加载
我正在尝试使用commands extension运行我的discord bot,这样我就可以将我的所有命令分别存储在Python discord.ext.commands.errors.ExtensionNotFound:Extension';齿轮。随机';无法加载,python,discord,discord.py,python-os,Python,Discord,Discord.py,Python Os,我正在尝试使用commands extension运行我的discord bot,这样我就可以将我的所有命令分别存储在commands文件夹下,但是每次我想运行bot时,我都会在terminal中遇到一系列错误,以下是所有内容: 终端:/bot.py Traceback (most recent call last): File "/home/xinto/.local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line
commands
文件夹下,但是每次我想运行bot时,我都会在terminal中遇到一系列错误,以下是所有内容:
终端:/bot.py
Traceback (most recent call last):
File "/home/xinto/.local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 621, in load_extension
lib = importlib.import_module(name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'cogs.random'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "handlerbot.py", line 36, in <module>
client.load_extension(f'cogs.{filename[:-3]}')
File "/home/xinto/.local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 623, in load_extension
raise errors.ExtensionNotFound(name, e) from e
discord.ext.commands.errors.ExtensionNotFound: Extension 'cogs.random' could not be loaded.
random.py:
#!/usr/bin/python3.6
import discord
from dotenv import load_dotenv
import random
from discord.ext import commands
import os
load_dotenv()
TOKEN = os.getenv('TOKEN')
client = commands.Bot(command_prefix = '!')
#this script types "Connected!" in terminal if nothing has gone wrong
@client.event
async def on_ready():
print('Connected!')
#this script makes sure that bot doesn't reply to itself
@client.event
async def on_message(message):
if message.author == client.user:
return
@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('./commands'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
client.run(TOKEN)
import discord
import os
from discord.ext import commands
class random(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def random(self, ctx):
for filename in os.listdir(./images):
list = '1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '7.jpg', '8.jpg', '9.jpg', '10.jpg', '11.jpg', '12.jpg'
await ctx.send('take this', file=discord.File(random.choice(list)))
def setup(client):
client.add_cog(random(client))
我不能责怪random.py,因为它对我的其他命令也有同样的作用,我已经检查了代码,找不到它不工作的任何原因。非常感谢您的帮助,谢谢 好吧,我知道了。我不得不重新命名
f'cogs{extension}
到
f'commands{extension}
如果有人有同样的问题,你必须用你有命令的文件夹名重命名cogs
,也就是说,我的名字叫commands
,希望我帮助了别人