python - discord.py 加载扩展的问题
问题描述
我的 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))```
解决方案
我知道已经晚了,但现在回答总比不回答好。:) 如果你没有解决你的问题,这可能会有所帮助。
问题是找不到 cog,因为你给了他一个无效的路径(这就是你得到 ExtensionNotFound 异常的原因)。
我试过你的代码是这样的:
- 我制作了这个文件层次结构:照片并在“cogs”文件夹中添加了一个名为 commands.py 的文件;
- 在
main.py
我把你的第一个代码是这样的:
from discord.ext import commands
import os
client = commands.Bot(command_prefix = "!")
@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]}')
client.run(TOKEN)
- 在
cogs/commands.py
文件中,我编写了这样的 cog 代码:
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))
- 我运行机器人,当它在线时,我
!unload commands
首先输入了 Discord,因为命令扩展已经加载到main.py
这里的文件中:
# for loop to find cogs folder
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
- 现在该扩展已卸载,我输入
!load commands
并成功加载。
因此,请确保您提供的路径load_extension
和unload_extension
功能是正确的。
推荐阅读
- django - Django admin 中的 ForeignKey 内联下拉菜单
- c# - Automapper not ForMember 不映射(说属性未映射)
- javascript - jQuery:比较 if 语句中的字符串未按预期工作
- java - 在请求参数(spring-boot)中验证日期(ISO-8601)
- php - PHP RegEx + 删除单词后的点
- python - Django Rest 框架、Swagger 和 CORS
- rest - 更新 Entity whitout 表单的最佳方法是什么,FosRest
- python - Tensorflow-GPU Eager Execution:Batch Normalization 极慢并降低 Volatile GPU-Util
- arrays - 无法访问 FormResponse 中的所有 ItemResponse
- python - 从 bing 中提取链接