Jango DarkWind
Jango DarkWind

Reputation: 47

TypeError: 'Collection' object is not callableon mongodb

I have been trying to create a warn system using mongodb, and I am having a hard time setting up the schema/ section to upload each time a user gets warned. the error I get is what is below. I have looked around all documentation I can, I just do not understand.

Traceback (most recent call last):
  File "C:\Users\dargo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\dargo\PycharmProjects\MaxCodezBot\cogs\warns.py", line 53, in warn
    await self.client.warns.insert(warn)
  File "C:\Users\dargo\PycharmProjects\MaxCodezBot\utils\mongo.py", line 85, in insert
    await self.db.insert_one(dict)
  File "C:\Users\dargo\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\collection.py", line 3440, in __call__
    raise TypeError("'Collection' object is not callable. If you "
TypeError: 'Collection' object is not callable. If you meant to call the 'insert_one' method on a 'Database' object it is failing because no such method exists.

with the code

@commands.command()
    @commands.guild_only()
    # @commands.has_guild_permissions(kick_members=True)
    async def warn(self, ctx, user: discord.User, *, reason=None):
        if user is None:
            await ctx.send("You have not provided a user!")
            return

        if user is user.bot:
            await ctx.send(f"<@{ctx.author.id}, you cannot warn a bot!")
            return

        if user.id == ctx.author.id:
            embed = discord.Embed(description=f"<@{ctx.author.id}>, you are not allowed to warn yourself!")

            embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar_url)
            embed.set_footer(text=f"{ctx.message.guild.name} Custom Designed Bot")
            embed.timestamp = ctx.message.created_at

            await ctx.send(embed=embed)
            return

        author_id = ctx.author.id
        guild = ctx.message.guild.id
        first_warning = False
        warn_id = 0
        reason = reason or None

        warn = {
            "_id": ctx.message.guild.id,
            "user": user.id,
            "warn": [{
                "PunishType": 'warn',
                "Moderator": ctx.message.author.id,
                "Reason": reason,
            }],
        }
        await self.client.warns.insert(warn)

Upvotes: 0

Views: 3086

Answers (1)

Gibbs
Gibbs

Reputation: 22974

There is a mismatch between the code and error trace I believe.

Error is at self.db.insert_one(dict)

It means that you are trying to invoke insert one on db object.

It should be db.coll.insert_one(doc)

Upvotes: 1

Related Questions