import discord import pymongo import random import os from discord.ext import commands from discord import app_commands client = pymongo.MongoClient(os.getenv("mongo_url")) db = client.economy coll = db.users class economy(commands.Cog): def __init__(self, bot): self.bot = bot @commands.Cog.listener() async def on_ready(self): print("Economy Online") economy = app_commands.Group(name="economy", description="All the economy commands") @economy.command(name="beg", description="Beg for money") @commands.cooldown(1, 60, commands.BucketType.user) async def beg(self, interaction: discord.Interaction): num = random.randint(1, 100) user = coll.find_one({"_id": interaction.user.id}) if user is not None: coll.update_one({"_id": interaction.user.id}, {"$inc": {"coins": num}}) else: coll.insert_one({"_id": interaction.user.id, "coins": num}) await interaction.response.send_message(f"{interaction.user.mention} you earned {num} coins") @economy.command(name="balance", description="Check your balance") async def balance(self, interaction: discord.Interaction, member :discord.Member = None): if member == None: member = interaction.user user = coll.find_one({"_id": member.id}) if user is None: await interaction.response.send_message(f"{member.mention} You have 0 coins") return await interaction.response.send_message(f"{member.mention} you have {user['coins']} coins") @economy.command(name="pay", description="Pay someone") @commands.cooldown(1, 60, commands.BucketType.user) async def pay(self, interaction: discord.Interaction, member :discord.Member, amount:int): user = coll.find_one({"_id": interaction.user.id}) member_acc = coll.find_one({"_id": member.id}) if user is None or member_acc is None: await interaction.response.send_message(f"Cannot send coins to {member.mention}.") return if user['coins'] < amount: await interaction.response.send_message("You do not have enough coins") return coll.update_one({"_id": interaction.user.id}, {"$inc": {"coins": -amount}}) coll.update_one({"_id": member.id}, {"$inc": {"coins": amount}}) await interaction.response.send_message(f"{member.mention} has been sent {amount} coins!") @economy.command(name="work", description="Work for money") @commands.cooldown(1, 60, commands.BucketType.user) async def work(self, interaction: discord.Interaction): num = random.randint(1, 500) user = coll.find_one({"_id": interaction.user.id}) if user is None: coll.insert_one({"_id": interaction.user.id, "coins": num}) else: coll.update_one({"_id": interaction.user.id}, {"$inc": {"coins": num}}) await interaction.response.send_message(f"You earned {num} coins!") @economy.command(name="rob", description="Rob someone") @commands.cooldown(1, 60, commands.BucketType.user) async def rob(self, interaction: discord.Interaction, member: discord.Member): user = coll.find_one({"_id": interaction.user.id}) member_acc = coll.find_one({"_id": member.id}) if user is None or member_acc is None: await interaction.response.send_message(f"Unable to rob {member.mention}", ephemeral=True) return if member_acc['coins'] < 500: await interaction.response.send_message(f"{member.mention} has too little coins to steal from", ephemeral=True) return amount = random.randint(500, member_acc['coins']) coll.update_one({"_id": interaction.user.id}, {"$inc": {"coins": amount}}) coll.update_one({"_id": member.id}, {"$inc": {"coins": -amount}}) await interaction.response.send_message(f"You have successfully robbed {member.mention} for {amount} coins!") @economy.command(name="slots", description="Play slots") @commands.cooldown(1, 60, commands.BucketType.user) async def slots(self, interaction: discord.Interaction, bet: int): num = bet * 2 chance = random.randint(0, 10) user = coll.find_one({"_id": interaction.user.id}) if user is None: await interaction.response.send_message("You do not have an account yet. Please beg for some money.") return if chance < 5: coll.update_one({"_id": interaction.user.id}, {"$inc": {"coins": -bet}}) await interaction.response.send_message(f"You lost.. Total amount lost is {bet}") return coll.update_one({"_id": interaction.user.id}, {"$inc": {"coins": num}}) await interaction.response.send_message(f"Congrats! You won {num} amount of coins!") async def setup(bot): await bot.add_cog(economy(bot))