【问题标题】:My cogs do not work discord.py/nextcord.py我的齿轮不起作用 discord.py/nextcord.py
【发布时间】:2022-01-05 21:17:03
【问题描述】:

我的 cogs 在 nextcord.py 上不起作用

我有这样的错误:

help cannot be loaded. [Extension 'help' could not be loaded.]
Moderation cannot be loaded. [Extension 'Moderation' could not be loaded.]
other cannot be loaded. [Extension 'other' could not be loaded.]
cogs.help cannot be loaded. [Extension 'cogs.help' could not be loaded.]  
cogs.Moderation cannot be loaded. [Extension 'cogs.Moderation' could not be loaded.]
cogs.other cannot be loaded. [Extension 'cogs.other' could not be loaded.]

另外,我的代码如下所示:

@bot.command()
async def load(ctx, extensions):
    if ctx.author.id == 606433100150472704:
        bot.load_extension(f'cogs.{extensions}')
        await ctx.send("Cogs is loaded...")
    else:
        await ctx.send("Вы не являетесь разработчиком...")


@bot.command()
async def unload(ctx, extensions):
    if ctx.author.id == 606433100150472704:
        bot.unload_extension(f'cogs.{extensions}')
        await ctx.send("Cogs is unloaded...")
    else:
        await ctx.send("Вы не являетесь разработчиком...")

@bot.command()
async def reload(ctx, extensions):
    if ctx.author.id == 606433100150472704:
        bot.unload_extension(f'cogs.{extensions}')
        bot.load_extension(f'cogs.{extensions}')
        await ctx.send("Cogs is unloaded...")
    else:
        await ctx.send("Вы не являетесь разработчиком...")

initial_extensions = [
    'help',
    'Moderation',
    'other'
]

for filename in os.listdir('./сogs'):
    if filename.endswith('.py'):
        initial_extensions.append(f"cogs.{filename[:-3]}") #Загрузка всех когов

if __name__ == '__main__':
    for extension in initial_extensions:
        try:
            bot.load_extension(extension)
        except Exception as error:
            print('{} cannot be loaded. [{}]'.format(extension, error))

我认为问题出在代码的最后几行。当我尝试删除 if name == 'ma​​in' 一段代码时,机器人开始工作但命令不起作用。

【问题讨论】:

  • 删除以下代码 except Exception as error: print('{} cannot be loaded. [{}]'.format(extension, error)) 并提供您的错误。
  • ``` 文件“c:\Users\Andriyko\Desktop\Ghost bot\ghost.py”,第 55 行,在 bot.load_extension(extension) 文件“C:\Users\ Andriyko\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\bot.py",第 731 行,在 load_extension 中引发错误。ExtensionNotFound(name) nextcord.ext.commands.errors.ExtensionNotFound : 无法加载扩展“帮助”。 ```

标签: discord discord.py bots nextcord


【解决方案1】:

在 cogs 的文件中,您应该使用来设置它们

import discord
from discord.ext import commands

class Cog(commands.Cog):
  def __init__(self, bot):
    self.bot = bot

  @commands.Cog.listener()
  async def on_ready(self):
    print("Cog 'Cog' loaded")

def setup(bot):
  client.add_cog(Cog(bot))

在 main.py 中,你应该使用

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="prefix")

cogs = ["Cog", "moderation", "other", "etc"]
if __name__ == "__main__": 
  for cog in cogs:
    bot.load_extension(cog)

这样机器人应该能够加载扩展。您不必使用 os 查找文件名

【讨论】:

    【解决方案2】:

    我想这可能是一个更好的方法:-

    main.py 中执行此操作

    import discord
    from discord.ext import commands
    import os
    
    
    class Bot(commands.Bot):
        def __init__(self):
            super().__init__(command_prefix='+', help_command=None, intents=discord.Intents().all())
    
            for filename in os.listdir('./cogs'):
                if filename.endswith('.py'):
                    self.load_extension(f"cogs.{filename[:-3]}")
    
    bot = Bot()
    
    bot.run('token')
    

    我希望这有效!如果还是不行,你可以在 nextcord 的 discord 服务器上提问,或者在 github 页面中创建一个新问题。

    【讨论】:

      【解决方案3】:

      我没有任何评论的声誉。

      最好删除except 语句,以便可以看到完整的回溯。

      @bot.command()
      async def load(ctx, extensions):
          if ctx.author.id == 606433100150472704:
              bot.load_extension(f'cogs.{extensions}')
              await ctx.send("Cogs is loaded...")
          else:
              await ctx.send("Вы не являетесь разработчиком...")
      
      
      @bot.command()
      async def unload(ctx, extensions):
          if ctx.author.id == 606433100150472704:
              bot.unload_extension(f'cogs.{extensions}')
              await ctx.send("Cogs is unloaded...")
          else:
              await ctx.send("Вы не являетесь разработчиком...")
      
      @bot.command()
      async def reload(ctx, extensions):
          if ctx.author.id == 606433100150472704:
              bot.unload_extension(f'cogs.{extensions}')
              bot.load_extension(f'cogs.{extensions}')
              await ctx.send("Cogs is unloaded...")
          else:
              await ctx.send("Вы не являетесь разработчиком...")
      
      initial_extensions = [
          'help',
          'Moderation',
          'other'
      ]
      
      for filename in os.listdir('./сogs'):
          if filename.endswith('.py'):
              initial_extensions.append(f"cogs.{filename[:-3]}") #Загрузка всех когов
      
      if __name__ == '__main__':
          for extension in initial_extensions:
              try:
                  bot.load_extension(extension)
              except Exception as error:
                  print('{} cannot be loaded. [{}]'.format(extension, error))
      

      如果此错误仍然存​​在,请将您的代码和完整的回溯发送到nextcord server 中的帮助频道

      【讨论】:

        猜你喜欢
        • 2020-04-14
        • 1970-01-01
        • 2021-04-12
        • 1970-01-01
        • 2020-10-10
        • 2023-01-26
        • 2023-03-14
        • 1970-01-01
        • 2016-05-10
        相关资源
        最近更新 更多