Discord.py, MongoDB check if user is already in

Viewed 107

Hey I'm making command that register the user by their discord ID(mongodb). So now I want to add if User already registered the bot return that you are already in.

I tried this:

    @commands.command()
    async def register(self, ctx):
      mongo_url = ""
      cluster = MongoClient(
        mongo_url
      )
      db = cluster["DNFT-registered-users(DISCORDBOT)"]
      collection = db["registered"]
      register = {
        "User": ctx.author.id,
      }
      if ctx.author.id in collection:
        return await ctx.send("You are in already")
      else:
        collection.insert_one(register)
        return await ctx.send("worked")
1 Answers

In this pattern you can use if collection.find_one({‘User’: ctx.author.id}) is None:. If I were you I would use motor instead of pymongo.

Related