获取属性错误:模块 'mod_commands' 没有属性 'ban' - 即使我添加了它

Posted

技术标签:

【中文标题】获取属性错误:模块 \'mod_commands\' 没有属性 \'ban\' - 即使我添加了它【英文标题】:Getting Attribute Error: module 'mod_commands' has no attribute 'ban' - even though I added it获取属性错误:模块 'mod_commands' 没有属性 'ban' - 即使我添加了它 【发布时间】:2020-05-02 09:12:47 【问题描述】:

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

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

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

我在带有 Linux 10 (Buster) 的 Raspberry Pi 4 Model B 上执行此操作。我的 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'

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

【问题讨论】:

确保您要导入的 mod_commands 是您想要的。将print(mod_commands.__file__) 放在 programm.py 顶部附近。此外,请确保删除 __pycache__ 目录(如果存在)以确保您使用的是最新版本的代码。 感谢您的建议!可悲的是,删除 pycache 目录并没有改变它,我正在导入预期的脚本 【参考方案1】:

造成这种情况的原因是您有循环导入:programm 正在导入 mod_commands 正在导入 bot_utility 正在导入 programm 等。

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

【讨论】:

以上是关于获取属性错误:模块 'mod_commands' 没有属性 'ban' - 即使我添加了它的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 SWIG 在 Python 中实例化 C++ 类(获取属性错误)

AttributeError:“模块”对象没有属性

Osmnx 错误:模块“osmnx.elevation”没有属性“add_node_elevations_raster”

freeswitch基本命令

无法为 BuildType_Decorated 获取“applicationVariants”的未知属性

属性错误模块 'cv2.cv2' 没有属性 'videocapture'