M
M
MrPipka2022-04-13 18:50:04
Python
MrPipka, 2022-04-13 18:50:04

MongoDB error in discord.py?

I'm building a database bot for discord. During development, I encountered this problem: every time I try to display the balance of a person, this error is displayed in the console

raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: cluster0-shard-00-00.bn6du.mongodb.net:27017: connection closed,cluster0-shard-00-02.bn6du.mongodb.net:27017: connection closed,cluster0-shard-00-01.bn6du.mongodb.net:27017: connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 6256e1b6ca5089d0ea20fe00, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('cluster0-shard-00-00.bn6du.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-00.bn6du.mongodb.net:27017: connection closed')>, <ServerDescription ('cluster0-shard-00-01.bn6du.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-01.bn6du.mongodb.net:27017: connection closed')>, <ServerDescription ('cluster0-shard-00-02.bn6du.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-02.bn6du.mongodb.net:27017: connection closed')>]>


Bot code:

import discord
from discord.ext import commands
from pymongo import MongoClient
import sys

cluster = MongoClient("")

bot = commands.Bot(command_prefix='b.')
@bot.remove_command('help')
    
@bot.event
async def on_ready():
    print("Bot connected to the server")
    await bot.change_presence(status = discord.Status.online, activity = discord.Game('b.help'))

@bot.event
async def on_message(message):
    if collection.count_documents({f'money{message.guild.id}':message.author.id}) == 0:
        collection.insert_one({f'money{message.guild.id}':message.author.id, 'count': 1})
        await bot.process_commands(message)
    else:
        collection.update_one({f'money{message.guild.id}': message.author.id}, {'$inc': {'count': 1}})
        await bot.process_commands(message)


@bot.command()
async def check_balance(ctx):
    if collection.count_documents({f'money{ctx.guild.id}': ctx.author.id}) == 1:
        await ctx.send('money: ' + str(collection.find_one({f'money{ctx.guild.id}': ctx.author.id})['count']))
    else:
        print('false')
      
bot.run('')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question