Files
Gwendolyn/funcs/games/money.py

71 lines
2.9 KiB
Python

class Money():
def __init__(self, bot):
self.bot = bot
self.database = bot.database
# Returns the account balance for a user
def checkBalance(self, user):
self.bot.log("checking "+user+"'s account balance")
userData = self.database["users"].find_one({"_id":user})
if userData != None:
return userData["money"]
else: return 0
async def sendBalance(self, ctx):
await self.bot.defer(ctx)
response = self.checkBalance("#"+str(ctx.author.id))
if response == 1:
new_message = ctx.author.display_name + " has " + str(response) + " GwendoBuck"
else:
new_message = ctx.author.display_name + " has " + str(response) + " GwendoBucks"
await ctx.send(new_message)
# Adds money to the account of a user
def addMoney(self,user,amount):
self.bot.log("adding "+str(amount)+" to "+user+"'s account")
userData = self.database["users"].find_one({"_id":user})
if userData != None:
self.database["users"].update_one({"_id":user},{"$inc":{"money":amount}})
else:
self.database["users"].insert_one({"_id":user,"user name":self.bot.databaseFuncs.getName(user),"money":amount})
# Transfers money from one user to another
async def giveMoney(self, ctx, user, amount):
await self.bot.defer(ctx)
username = user.display_name
if self.bot.databaseFuncs.getID(username) == None:
async for member in ctx.guild.fetch_members(limit=None):
if member.display_name.lower() == username.lower():
username = member.display_name
userID = "#" + str(member.id)
newUser = {"_id":userID,"user name":username,"money":0}
self.bot.database["users"].insert_one(newUser)
userData = self.database["users"].find_one({"_id":f"#{ctx.author.id}"})
targetUser = self.bot.databaseFuncs.getID(username)
if amount > 0:
if targetUser != None:
if userData != None:
if userData["money"] >= amount:
self.addMoney(f"#{ctx.author.id}",-1 * amount)
self.addMoney(targetUser,amount)
await ctx.send(f"Transferred {amount} GwendoBucks to {username}")
else:
self.bot.log("They didn't have enough GwendoBucks")
await ctx.send("You don't have that many GwendoBucks")
else:
self.bot.log("They didn't have enough GwendoBucks")
await ctx.send("You don't have that many GwendoBuck")
else:
self.bot.log("They weren't in the system")
await ctx.send("The target doesn't exist")
else:
self.bot.log("They tried to steal")
await ctx.send("Yeah, no. You can't do that")