T
T
tatsuki12021-11-08 22:02:26
Python
tatsuki1, 2021-11-08 22:02:26

Instead of replacing, MongoDB is written again, can you help?

I am writing one system with a MongoDB database, and I want that when the database by server ID is empty, data is entered, and when the server ID matches the server ID that is recorded in the database, the data is replaced, not written again.

@commands.command()
    async def welcome(self, ctx, channel: discord.TextChannel):
        if ctx.message.author.guild_permissions.manage_messages:
            result = channel1.find_one({"guild_id": ctx.guild.id, "channel_id": channel.id})
            if result is None:
                channel1.insert_one({f"guild_id": ctx.guild.id, "channel_id": channel.id})
                await ctx.send(f'{ctx.author.mention}, канал успешно установлен на {channel.mention}.')
            elif result is not None:
                channel1.update_one({"guild_id": ctx.guild.id}, {"$set": {"channel_id": channel.id}})
                await ctx.send(f'{ctx.author.mention}, канал изменён на {channel.mention}.')
        else:
            await ctx.send(f'{ctx.author.mention}, вам нужны права на управлени

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Telmor, 2021-11-09
@Telmor

Try to enter "channel_id": 0 into the database. Then in the check it will be possible to do: if result != or == 0. This way it will be easier. (I personally do this)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question