【发布时间】:2020-08-23 13:38:50
【问题描述】:
我的 discord 机器人已连接到我的 discord 服务器,并且所有命令似乎都按预期运行。当我尝试使用加载或卸载命令时,它给了我一个错误提示“命令引发异常:ExtensionNotFound:无法加载扩展'cogs.commands'。”我不知道为什么它说它们还没有加载扩展中的事件仍在运行的命令。我已经尝试重写加载和卸载命令的内容,我尝试将扩展名重命名为“事件”和“命令”。我只是一个初学者,我想我写错了。这里是加载、卸载、重新加载和设置命令。
@client.command()
async def load(ctx, extension):
client.load_extension(f'cogs.{extension}')
print(f'{extension} successfully loaded')
# cog unloader command
@client.command()
async def unload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
print(f'{extension} successfully unloaded')
# cog reloader command, unload then load extenion
@client.command()
async def reload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
client.load_extension(f'cogs.{extension}')
print(f'{extension} successfully re-loaded')
# for loop to find cogs folder
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
这是另一个文件,其中包含我目前作为扩展编写的事件和命令。
from discord.ext import commands
# --EVENTS--
class Events(commands.Cog):
def __init__(self, client):
self.client = client
# Bot online event
@commands.Cog.listener()
async def on_ready(self):
print('GuhBot v3 is online and ready! C:')
# Member joined Event
@commands.Cog.listener()
async def on_member_join(self, member):
print(f'{member} joined the server. C:')
# Member left Event
@commands.Cog.listener()
async def on_member_remove(self, member):
print(f'{member} left the server. :C')
# --MODERATION--
class Moderation(commands.Cog):
def __init__(self, client):
self.client = client
# clear command. default 5 messages, can be changed by user.
@commands.command()
async def clear(self, ctx, amount=5):
await ctx.channel.purge(limit=amount+1)
# Cog Setup
def setup(client):
client.add_cog(Events(client))
client.add_cog(Moderation(client))```
【问题讨论】: