Untitled

 avatar
unknown
plain_text
5 months ago
1.9 kB
13
Indexable
import discord
import random
from discord.ext import commands
from discord import app_commands
from pymongo import MongoClient
from discord.ext import commands

# MongoDB Connection Setup
client = MongoClient("mongodb+srv://williamjmaurice:ygDY0QI2Om4Y7YDZ@economy.iaynb.mongodb.net/?retryWrites=true&w=majority&appName=Economy")  # Replace with your MongoDB connection URI
db = client['Economy']  # The database name
collection = db['Metal']  # The collection name

# Bot Setup
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user}')

@bot.command()
async def mine(ctx):
    num = random.randint(1,4)
    await ctx.send(f"You have mined {num} ore")

    """
    Saves an integer to MongoDB with the user's ID.
    """
    # Create a dictionary with user information and the integer value
    user_data = {
        'user_id': ctx.author.id,
        'username': ctx.author.name,
        'ore': num,
    }

    # Insert the data into MongoDB
    collection.update_one(
        {'user_id': ctx.author.id},  # Find document by user ID
        {'$set': user_data},          # Update or insert the new data
        upsert=True                   # If no document exists, create one
    )    

@bot.command()
async def mine_bal(ctx):
    """
    Retrieves the integer saved for a specific user from MongoDB.
    """
    # Find the user's data in the collection
    user_data = collection.find_one({'user_id': ctx.author.id})

    if user_data:
        saved_integer = user_data.get('saved_integer')
        await ctx.send(f"Your metal balance is: {saved_integer}")
    else:
        await ctx.send("No integer has been saved for you.")

# Run the bot with your token
bot.run("TOKEN")
Editor is loading...
Leave a Comment