1

I have a little problem which you can skip and does not give you a big error that doesn't let you run the bot, by it is very frustrating tho.

Code:

    import asyncio
    import discord
    from discord.ext import commands
    from main import db
         ^^^^ Unable to import 'main' [pylint(import-error)]
    import datetime
    import random

I am using cogs so that's why I am importing from main.

Pearoo
  • 45
  • 1
  • 7
  • This is just a pylint error, does the code work? – Łukasz Kwieciński Mar 30 '21 at 08:16
  • He said that the code works – TudorTeo Mar 30 '21 at 08:23
  • PyLint can often be tricky when it comes to imports, if you made the main.py file yourself, adding an empty `__init__.py` to the same directory as main can fix the issue. If not, I would double check that PyLint is configured correctly. See this thread: https://stackoverflow.com/questions/1899436/pylint-unable-to-import-error-how-to-set-pythonpath – Rudy Mar 30 '21 at 08:25

1 Answers1

0

If you are using cogs you don't have to import main. Just give a look to the discord.py docs and you will see that cogs must be like this:

import discord

from discord.ext import commands
class MembersCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    @commands.guild_only()
    async def joined(self, ctx, *, member: discord.Member):
        """Says when a member joined."""
        await ctx.send(f'{member.display_name} joined on {member.joined_at}')

    @commands.command(name='coolbot')
    async def cool_bot(self, ctx):
        """Is the bot cool?"""
        await ctx.send('This bot is cool. :)')

    @commands.command(name='top_role', aliases=['toprole'])
    @commands.guild_only()
    async def show_toprole(self, ctx, *, member: discord.Member=None):
        """Simple command which shows the members Top Role."""

        if member is None:
            member = ctx.author

        await ctx.send(f'The top role for {member.display_name} is {member.top_role.name}')    
    def setup(bot):
        bot.add_cog(MembersCog(bot))

And in main.py: bot.load_extension('folder_name.file_name') This is just an example.

filipporomani
  • 275
  • 3
  • 9
  • my cog loader is `for filename in os.listdir('./cogs'): if filename.endswith('.py'): client.load_extension(f"cogs.{filename[:-3]}") print(f"----------------------------") print(f"{len(client.extensions)}. | Loaded!")`. will that work? – Pearoo Apr 12 '21 at 08:06
  • The cog loader is not the problem. You don't need to import main. You need to read the docs and read how a cog work! As i showed in the upper code, you don't need to import main. – filipporomani Apr 12 '21 at 17:48