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

2024-03-28 12:43:42 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

我正在使用Linux10(Buster)在Raspberry Pi 4模型B上执行此操作。我的Python版本是3.7.3

以下是我正在谈论的脚本:

program.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'

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


Tags: pyimport脚本modbotembedcommandsmember