【问题标题】:discord.py problems with loading extensionsdiscord.py 加载扩展的问题
【发布时间】: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))```    

【问题讨论】:

    标签: python bots discord


    【解决方案1】:

    我知道已经晚了,但现在回答总比不回答好。 :) 如果你没有解决你的问题,这里可能会有所帮助。

    问题是找不到 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 文件中,我编写了这样的 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))
    
    1. 我运行机器人,当它在线时,我首先输入了 Discord !unload commands,因为 命令扩展已加载到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 函数的路径是正确的。

    【讨论】:

      猜你喜欢
      • 2020-05-29
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多