Answer the question
In order to leave comments, you need to log in
MongoDB problem, 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
In my opinion, instead of
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}.')
update_one
specify in the request options upsert=True
. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question