Как правильно работать с MongoDB?
Уже как день пытаюсь сделать дискорд бота, который может выдавать 5 минутные коды аунтификации, но каждый раз выдает MongoDB и не только ошибки, например :raise ConfigurationError(str(exc)) pymongo.errors.ConfigurationError: All nameservers failed to answer the query _mongodb._tcp.vapdb.lnobjko.mongodb.net. IN SRV. Просьба помочь мне в решении проблемы, чтобы код работал, спасибо.
from pymongo import MongoClient
import random
import asyncio
cluster = MongoClient("mongodb+srv://Kyrylo:<password>[email protected]/VAPDB?retryWrites=true&w=majority")
db = cluster["mydatabase"]
codes_collection = db["codes"]
client = discord.Client(intents=discord.Intents.all())
TOKEN = "токен"
async def generate_code():
while True:
try:
code = ''.join(random.choices('0123456789', k=4))
if codes_collection.count_documents({"code": code}) == 0:
codes_collection.insert_one({"code": code})
await asyncio.sleep(300)
codes_collection.delete_one({"code": code})
except Exception as e:
print(f"Error in generate_code: {e}")
continue
async def send_code(user):
code = codes_collection.find_one()
if code is None:
await user.send("Коды временно недоступны. Попробуйте позже.")
else:
try:
codes_collection.delete_one({"_id": code["_id"]})
await user.send(f"Ваш код: {code['code']}")
except Exception as e:
print(f"Error sending code to user: {e}")
@client.event
async def on_ready():
print("Бот запущен")
asyncio.create_task(generate_code())
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == "!код":
user_codes = codes_collection.find({"user_id": message.author.id})
user_codes_list = list(user_codes)
if len(user_codes_list) > 0:
await message.channel.send("У вас уже есть один код.")
else:
await send_code(message.author)
codes_collection.insert_one({"user_id": message.author.id})
client.run(TOKEN)