Cogs不和谐.py

问题描述 投票:0回答:3

我正在尝试将我的discord.py 代码移至cogs 中。我不断收到同样的错误

Exception has occurred: ModuleNotFoundError No module named 'cogs'

bot.py

from discord.ext import commands
bot = commands.Bot(command_prefix='!')

bot.load_extension('cogs.maincog')

bot.run('token')

maincog.py

from discord.ext import commands


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

    @commands.command()
    async def test(self, ctx):
        await ctx.send("test")


def setup(bot):
    bot.add_cog(MainCog(bot))

文件夹:

谢谢!

python discord.py
3个回答
1
投票

因为maincog和bot、cogs在同一个目录下。不需要包含在内。 这是因为 python 将查找名为 cogs 的子目录,您的情况不需要该子目录。 新的 Bot.py 代码:

from discord.ext import commands
bot = commands.Bot(command_prefix='!')

bot.load_extension('maincog')

bot.run('token')

1
投票

bot.py 不需要包含在文件夹中,因此将其放在外面,这应该可以工作,但有一些改进:

bot.py
(文件夹外)

import os
from discord.ext import commands

client = commands.Bot(command_prefix='!')
# If you don't want the default help command and want to make by yourself, you can use this:
client.remove_command('help')

# With this event, always when your bot is online, it will be printed on the terminal:
@client.event
async def on_ready():
    print('Bot Online!')

# This causes all files inside the folder with the ending .py to be loaded, without having to load one by one:
for filename in os.listdir('cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}')

client.run('token')
  • OBS:我写了“客户端”,但如果你愿意,你可以将其替换为“机器人”。这正是我喜欢的方式。
  • OBS2:如果保留“client”,请将 maincog.py 上的“bot”替换为“client”,就像这样:

maincog.py
(文件夹内)

from discord.ext import commands


class MainCog(commands.Cog):
    def __init__(self, client):
        self.author = None
        self.client = client

    @commands.command()
    async def test(self, ctx):
        await ctx.send('test')


def setup(client):
    client.add_cog(MainCog(client))

1
投票

虽然我最近回答了一点,但我相信不和谐已经改变,你的代码将不再工作,因为现在一切都必须是,

awaited
。所以这是你的正确代码:
bot.py

import os #this will be used later
import discord
from discord.ext import commands

intents = discord.Intents.default() #make sure to define the intents
intents.message_content = True #enable the message content intent

bot = commands.Bot(command_prefix='!', intents = intents) #define the bot and add the intents

token = "your-token"

cogs = ["maincog"]
async def load_cogs():
    for cogs in admincogs:
        try:
            await bot.load_extension(f'cogs.admin.{cogs.lower()}')
            print(f'{cogs} cog loaded.')
        except Exception as e:
            print(f'Failed to load {cogs} cog: {e}')

@bot.event
async def on_ready():
    await load_cogs()
    print('We have logged in as {0.user}'.format(bot))

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    await bot.process_commands(message)

bot.run(token)

在你的 maincogs.py 中:

from discord.ext import commands


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

    @commands.command()
    async def test(self, ctx):
        await ctx.send("test")


async def setup(bot):
    await bot.add_cog(MainCog(bot))

我希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.