Getting Attribute Error: module 'mod_commands' has no attribute 'ban' - even though I added it

Getting Attribute Error: module 'mod_commands' has no attribute 'ban' - even though I added it

我现在正在开发一个 Discord 机器人,discord.py。

我有一个主脚本,其中所有命令事件都会触发来自其他脚本的函数。一切正常,但后来我决定实施数据库并下载了 PostgreSQL 和 asyncpg。从那时起,我收到此错误:AttributeError: module 'mod_commands' has no Attribute 'ban' 当我尝试从名为 mod_commands.py 的脚本调用函数时。

从其他脚本调用函数确实可以正常工作。我没有做任何更改,所以我很确定这个错误与 PostgreSQL 或 asyncpg 有关。问题是,我不知道为什么会发生这种情况,如何我可以尝试解决这个问题。

我正在 Raspberry Pi 4 Model B 和 Linux 10 (Buster) 上执行此操作。我的 Python 版本是 3.7.3.

这些是我正在谈论的脚本:

programm.py:

import mod_commands
from discord.ext import commands
import discord

bot = commands.Bot(command_prefix='~',description=description)

@bot.command(name='ban')
@commands.has_permissions(ban_members=True)
@commands.bot_has_permissions(ban_members=True)
async def ban(ctx, members : commands.Greedy[discord.Member], *, reason = 'Idiotisches Verhalten'):
    await mod_commands.ban(ctx, members, reason)

def getLatency():
    return bot.latency

bot.run(TOKEN)

mod_commands.py:

import bot_utility
from discord.ext import commands

async def ban(ctx, members, reason):
    print('command => mod_commands.ban')
    bannableMembers = []
    for member in members:
        if(member.guild_permissions.administrator):
            await ctx.send('{} kann nicht gebannt werden.'.format(member.display_name))
        else:
            bannableMembers.append(member)
            embed = bot_utility.createEmbed(ctx.guild, 'Du wurdest gebannt', 'Grund: ' + reason, 0xFF0000)
            await member.send(embed=embed)
            await ctx.guild.ban(member, reason=reason)
    if(bannableMembers != None):
        embed = bot_utility.createEmbed(ctx.guild, 'Banns: (' + str(len(bannableMembers)) + ')', 'Grund: ' + reason, 0xff0000)
        for member in bannableMembers:
            embed.add_field(name=member.display_name, value=member.id)
        await ctx.send(embed=embed)

最后但同样重要的是,bot_utility.py:

import discord
import programm

def createEmbed(guild, title, description, colour : discord.colour):
    embed = discord.Embed(title = title, description=description, colour=colour)
    embed.set_footer(text=guild.name + '||%.2fs' % programm.getLatency(), icon_url=guild.icon_url)
    embed.type = 'rich'
    return embed

我试图自己寻找答案,但提出的类似问题要么与我认为解决方案对我不起作用的特殊情况有关,要么没有得到解答。以下是链接,以备不时之需:

Attribute Error 'module' object has no attribute 'DateField'

Getting attribute error : module 'matplotlib.pyplot' has no attribute 'canvas'

AttributeError: 'module' object has no attribute 'tests'

如果您需要更多信息,请告诉我。感谢您花时间考虑这个问题。

这是因为您有循环导入:
programm 正在导入 mod_commands 正在导入 bot_utility 正在导入 programm,等等

您应该将机器人的延迟传递给 createEmbed 而不是导入并使用它的方法。