加载扩展的 discord.py 问题

豆豆

我的不和谐机器人连接到我的不和谐服务器,所有命令似乎都按预期运行。当我尝试使用加载或卸载命令时,它给我一条错误消息:“命令引发异常: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 异常的原因)。

我试过你的代码是这样的:

  1. 我创建了这个文件层次结构:photo并在“cogs”文件夹中添加了一个名为 commands.py 的文件;
  1. 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)
  1. cogs/commands.py文件中,我写了这样的齿轮代码:
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))
  1. 我运行机器人,当它在线时,我!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]}')
  1. 现在该扩展已卸载,我输入!load commands并成功加载。

因此,请确保您提供的路径load_extensionunload_extension功能是正确的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章