You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人导入报错:无法从Features.foolbluhh导入指定方法

无法导入Discord Cog方法及数据库无写入的解决方案

错误原因分析

  1. 导入失败:你尝试导入的createFoolbluhh、createServers、createTickets三个方法是定义在FoolBluhh类的__init__方法内部的嵌套函数,既不属于模块级可导出对象,也不是类的成员方法,外部模块根本无法直接访问,因此触发ImportError。
  2. 数据库无写入:这些嵌套函数从未被执行过,加上定义位置错误,自然不会生成数据库文件或写入表结构。

修复步骤

1. 重构foolbluhh.py的代码结构

把三个数据库操作方法从__init__中移出,改为FoolBluhh类的异步成员方法,同时添加目录检查确保数据库文件能正常创建:

import os
from discord.ext import commands
import aiosqlite

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

    async def createFoolbluhh(self):
        print('e')
        # 确保Databases目录存在,避免连接失败
        os.makedirs('Databases', exist_ok=True)
        async with aiosqlite.connect('Databases/foolbluhh.db') as db_connection:
            pass  

    async def createServers(self):
        os.makedirs('Databases', exist_ok=True)
        async with aiosqlite.connect('Databases/foolbluhh.db') as db_connection:
            db_cursor = await db_connection.cursor()
            await db_cursor.execute('''CREATE TABLE IF NOT EXISTS Servers (
                serverID INTEGER PRIMARY KEY,
                serverName TEXT,
                serverPanMsgID INTEGER,
                serverTicketIDs INTEGER
            )''')
            await db_connection.commit()
            print("Servers table created successfully.")

    async def createTickets(self):
        os.makedirs('Databases', exist_ok=True)
        async with aiosqlite.connect('Databases/foolbluhh.db') as db_connection:
            db_cursor = await db_connection.cursor()
            await db_cursor.execute('''CREATE TABLE IF NOT EXISTS Tickets (
                serverTicketID INTEGER PRIMARY KEY,
                ticketOpenerName TEXT,
                ticketOpenerID INTEGER,
                ticketCreationDate TEXT,
                ticketCloseDate TEXT
            )''')
            await db_connection.commit()

async def setup(bot):
    cog = FoolBluhh(bot)
    # 初始化数据库表结构
    await cog.createFoolbluhh()
    await cog.createServers()
    await cog.createTickets()
    await bot.add_cog(cog)

2. 调整ticketcreate.py的调用方式

现在方法是类的成员,不能直接从模块导入,需通过bot的Cog实例调用:

# ticketcreate.py示例代码
from discord.ext import commands

@commands.command()
async def init_db(ctx):
    # 获取FoolBluhh Cog实例
    foolbluhh_cog = ctx.bot.get_cog('FoolBluhh')
    if foolbluhh_cog:
        await foolbluhh_cog.createServers()
        await foolbluhh_cog.createTickets()
        await ctx.send("数据库表初始化完成")

如果不需要依赖Cog的bot对象,也可以把方法改为模块级函数,这样就能保留你原来的导入方式:

# foolbluhh.py模块级函数版本
import os
from discord.ext import commands
import aiosqlite

# 模块级异步函数
async def createFoolbluhh():
    print('e')
    os.makedirs('Databases', exist_ok=True)
    async with aiosqlite.connect('Databases/foolbluhh.db') as db_connection:
        pass  

async def createServers():
    os.makedirs('Databases', exist_ok=True)
    async with aiosqlite.connect('Databases/foolbluhh.db') as db_connection:
        db_cursor = await db_connection.cursor()
        await db_cursor.execute('''CREATE TABLE IF NOT EXISTS Servers (
            serverID INTEGER PRIMARY KEY,
            serverName TEXT,
            serverPanMsgID INTEGER,
            serverTicketIDs INTEGER
        )''')
        await db_connection.commit()
        print("Servers table created successfully.")

async def createTickets():
    os.makedirs('Databases', exist_ok=True)
    async with aiosqlite.connect('Databases/foolbluhh.db') as db_connection:
        db_cursor = await db_connection.cursor()
        await db_cursor.execute('''CREATE TABLE IF NOT EXISTS Tickets (
            serverTicketID INTEGER PRIMARY KEY,
            ticketOpenerName TEXT,
            ticketOpenerID INTEGER,
            ticketCreationDate TEXT,
            ticketCloseDate TEXT
        )''')
        await db_connection.commit()

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

async def setup(bot):
    # 初始化数据库
    await createFoolbluhh()
    await createServers()
    await createTickets()
    await bot.add_cog(FoolBluhh(bot))

此时ticketcreate.py的原有导入代码即可正常工作:

from Features.foolbluhh import createFoolbluhh, createServers, createTickets

3. 关键注意点

  • 异步方法必须用await调用,不能同步执行。
  • 必须确保Databases目录存在,否则数据库连接会失败。
  • 使用get_cog时,传入的名称要和Cog类名完全一致(区分大小写)。

内容的提问来源于stack exchange,提问作者Jack Douglas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 06:13:22