1

I am working on a Discord bot right now, with discord.py.

I have a main script, where all command events trigger functions from other scripts. Everything worked fine, but then I decided to implement databases and downloaded PostgreSQL and asyncpg. Since then, I get this error: AttributeError: module 'mod_commands' has no Attribute 'ban'when I try to call functions from my script named mod_commands.py.

Calling functions from other scripts does work fine. I did not change anything, so I am pretty sure this error has something to do with PostgreSQL or asyncpg. The thing is, I don't have a clue why this is happening and how I could try and fix this.

I am doing this on a Raspberry Pi 4 Model B with Linux 10 (Buster). My Python version is 3.7.3.

These are the scripts I am talking about:

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)

and last but not least, 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

I tried to find answers by myself, but similar questions asked were either related to a special case for which I don't believe the solutions could work for me or were unanswered. Here are the links, in case you need them for any reason:

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'

If you need any more information, please tell me. Thanks for taking the time to think about this.

Harmon758
  • 5,084
  • 3
  • 22
  • 39
meep0
  • 13
  • 1
  • 5
  • Make sure the `mod_commands` you're importing is the one you intend. Put `print(mod_commands.__file__)` near the top of your programm.py. Also, make sure to delete the `__pycache__` directory (if it exists) to make sure you're using the latest version of your code. – Patrick Haugh Jan 15 '20 at 15:17
  • thanks for your advice! sadly, deleting the __pycache__ Directory did not change it and i am importing the intended script – meep0 Jan 15 '20 at 15:44

1 Answers1

2

The cause of this is that you have circular imports:
programm is importing mod_commands which is importing bot_utility which is importing programm, etc.

You should pass the bot's latency to createEmbed rather than import and use a method for it.

Harmon758
  • 5,084
  • 3
  • 22
  • 39