💵 Money
This commit is contained in:
@ -1,70 +1,151 @@
|
||||
"""
|
||||
Contains the code that deals with money.
|
||||
|
||||
*Classes*
|
||||
---------
|
||||
Money
|
||||
Deals with money.
|
||||
"""
|
||||
import discord_slash # Used for typehints
|
||||
import discord # Used for typehints
|
||||
|
||||
|
||||
class Money():
|
||||
"""
|
||||
Deals with money.
|
||||
|
||||
*Methods*
|
||||
---------
|
||||
checkBalance(user: str)
|
||||
sendBalance(ctx: discord_slash.context.SlashContext)
|
||||
addMoney(user: str, amount: int)
|
||||
giveMoney(ctx: discord_slash.context.SlashContext, user: discord.User,
|
||||
amount: int)
|
||||
|
||||
*Attributes*
|
||||
------------
|
||||
bot: Gwendolyn
|
||||
The instance of Gwendolyn
|
||||
database: pymongo.Client
|
||||
The mongo database
|
||||
"""
|
||||
|
||||
def __init__(self, bot):
|
||||
"""Initialize the class."""
|
||||
self.bot = bot
|
||||
self.database = bot.database
|
||||
|
||||
# Returns the account balance for a user
|
||||
def checkBalance(self, user):
|
||||
def checkBalance(self, user: str):
|
||||
"""
|
||||
Get the account balance of a user.
|
||||
|
||||
*Parameters*
|
||||
------------
|
||||
user: str
|
||||
The user to get the balance of.
|
||||
|
||||
*Returns*
|
||||
---------
|
||||
balance: int
|
||||
The balance of the user's account.
|
||||
"""
|
||||
self.bot.log("checking "+user+"'s account balance")
|
||||
|
||||
userData = self.database["users"].find_one({"_id":user})
|
||||
userData = self.database["users"].find_one({"_id": user})
|
||||
|
||||
if userData != None:
|
||||
if userData is not None:
|
||||
return userData["money"]
|
||||
else: return 0
|
||||
else:
|
||||
return 0
|
||||
|
||||
async def sendBalance(self, ctx):
|
||||
async def sendBalance(self, ctx: discord_slash.context.SlashContext):
|
||||
"""
|
||||
Get your own account balance.
|
||||
|
||||
*Parameters*
|
||||
------------
|
||||
ctx: discord_slash.context.SlashContext
|
||||
The context of the command.
|
||||
"""
|
||||
await self.bot.defer(ctx)
|
||||
response = self.checkBalance("#"+str(ctx.author.id))
|
||||
userName = ctx.author.display_name
|
||||
if response == 1:
|
||||
new_message = ctx.author.display_name + " has " + str(response) + " GwendoBuck"
|
||||
new_message = f"{userName} has {response} GwendoBuck"
|
||||
else:
|
||||
new_message = ctx.author.display_name + " has " + str(response) + " GwendoBucks"
|
||||
new_message = f"{userName} has {response} GwendoBucks"
|
||||
await ctx.send(new_message)
|
||||
|
||||
# Adds money to the account of a user
|
||||
def addMoney(self,user,amount):
|
||||
def addMoney(self, user: str, amount: int):
|
||||
"""
|
||||
Add money to a user account.
|
||||
|
||||
*Parameters*
|
||||
------------
|
||||
user: str
|
||||
The id of the user to give money.
|
||||
amount: int
|
||||
The amount to add to the user's account.
|
||||
"""
|
||||
self.bot.log("adding "+str(amount)+" to "+user+"'s account")
|
||||
|
||||
userData = self.database["users"].find_one({"_id":user})
|
||||
userData = self.database["users"].find_one({"_id": user})
|
||||
|
||||
if userData != None:
|
||||
self.database["users"].update_one({"_id":user},{"$inc":{"money":amount}})
|
||||
if userData is not None:
|
||||
updater = {"$inc": {"money": amount}}
|
||||
self.database["users"].update_one({"_id": user}, updater)
|
||||
else:
|
||||
self.database["users"].insert_one({"_id":user,"user name":self.bot.databaseFuncs.getName(user),"money":amount})
|
||||
newUser = {
|
||||
"_id": user,
|
||||
"user name": self.bot.databaseFuncs.getName(user),
|
||||
"money": amount
|
||||
}
|
||||
self.database["users"].insert_one(newUser)
|
||||
|
||||
# Transfers money from one user to another
|
||||
async def giveMoney(self, ctx, user, amount):
|
||||
async def giveMoney(self, ctx: discord_slash.context.SlashContext,
|
||||
user: discord.User, amount: int):
|
||||
"""
|
||||
Give someone else money from your account.
|
||||
|
||||
*Parameters*
|
||||
------------
|
||||
ctx: discord_slash.context.SlashContext
|
||||
The context of the command.
|
||||
user: discord.User
|
||||
The user to give money.
|
||||
amount: int
|
||||
The amount to transfer.
|
||||
"""
|
||||
await self.bot.defer(ctx)
|
||||
username = user.display_name
|
||||
if self.bot.databaseFuncs.getID(username) == None:
|
||||
if self.bot.databaseFuncs.getID(username) is 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}
|
||||
userID = f"#{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}"})
|
||||
userid = f"#{ctx.author.id}"
|
||||
userData = self.database["users"].find_one({"_id": userid})
|
||||
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:
|
||||
if amount <= 0:
|
||||
self.bot.log("They tried to steal")
|
||||
await ctx.send("Yeah, no. You can't do that")
|
||||
elif targetUser is None:
|
||||
self.bot.log("They weren't in the system")
|
||||
await ctx.send("The target doesn't exist")
|
||||
elif userData is None or userData["money"] < amount:
|
||||
self.bot.log("They didn't have enough GwendoBucks")
|
||||
await ctx.send("You don't have that many GwendoBuck")
|
||||
else:
|
||||
self.addMoney(f"#{ctx.author.id}", -1 * amount)
|
||||
self.addMoney(targetUser, amount)
|
||||
await ctx.send(f"Transferred {amount} GwendoBucks to {username}")
|
||||
|
Reference in New Issue
Block a user