Im currently creating an economy discord.py bot and im quite new to using Databases. Ive made a simple command to create an account for the user in the database. Heres the code:
import discord, os, requests, json, random, time, datetime, pymongo
from pymongo.mongo_client import MongoClient
import urllib
import asyncio
from discord.ext import commands
from discord import Member,app_commands
from discord.ui import Select, Button, View
intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix="c!", intents=intents, activity=activity)
bot.remove_command("help")
uri = "mongodb+srv://myusername:mypassword@mycluster.vfoyaox.mongodb.net/?retryWrites=true&w=majority"
client = pymongo.MongoClient(uri)
db = client["cluster0"]
collection = db.users
try:
client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
@bot.command()
async def createaccount(ctx):
user_id = ctx.author.id
username = ctx.author.name
collection.insert_one({
"discord_id": user_id,
"discord_username": username,
"discord_balance": 100
})
await ctx.send("Your account has been created with a starting balance of 100 credits.")
It prints out:
bad auth : authentication failed, full error: {'ok': 0, 'errmsg': 'bad auth : authentication failed', 'code': 8000, 'codeName': 'AtlasError'}
from print(e).
It doesnt really show any error, other than that because it was printed, but when I run the command in discord, nothing gets printed.
I alredy troubleshooted it, and found out that it stops after the collection.insert_one, and I assume its probably something wrong with the connection with MongoDB. I also made sure that my uri is correct.
Im very new to databases and I have absolutely no experience with them, so theres probably alot of problems in my code.