This commit is contained in:
2025-10-28 17:34:32 +01:00
parent f79a27aaca
commit ed1ea1f7af
4 changed files with 35 additions and 31 deletions

View File

@@ -6,8 +6,12 @@ Contains the code that deals with money.
Money
Deals with money.
"""
import typing
import interactions # Used for typehints
if typing.TYPE_CHECKING:
from gwendolyn_client import Gwendolyn
class Money():
"""
@@ -29,7 +33,7 @@ class Money():
The mongo database
"""
def __init__(self, bot):
def __init__(self, bot: "Gwendolyn"):
"""Initialize the class."""
self.bot = bot
self.database = bot.database
@@ -70,34 +74,35 @@ class Money():
response = self.checkBalance("#"+str(ctx.author.id))
user_name = ctx.author.display_name
if response == 1:
new_message = f"{user_name} has {response} GwendoBuck"
new_message = f"{user_name} has 1 GwendoBuck"
else:
new_message = f"{user_name} has {response} GwendoBucks"
await ctx.send(new_message)
# Adds money to the account of a user
def addMoney(self, user: str, amount: int):
def addMoney(self, user: interactions.User, amount: int):
"""
Add money to a user account.
*Parameters*
------------
user: str
The id of the user to give money.
user: User
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")
self.bot.log(f"adding {amount} to {user}'s account")
user_id = f"#{user.id}"
user_data = self.database["users"].find_one({"_id": user})
user_data = self.database["users"].find_one({"_id": user_id})
if user_data is not None:
updater = {"$inc": {"money": amount}}
self.database["users"].update_one({"_id": user}, updater)
self.database["users"].update_one({"_id": user_id}, updater)
else:
new_user = {
"_id": user,
"user name": self.bot.database_funcs.get_name(user),
"_id": user_id,
"user name": user.display_name,
"money": amount
}
self.database["users"].insert_one(new_user)
@@ -119,32 +124,17 @@ class Money():
"""
await ctx.defer()
username = user.display_name
if self.bot.database_funcs.get_id(username) is None:
async for member in ctx.guild.fetch_members(limit=None):
if member.display_name.lower() == username.lower():
username = member.display_name
user_id = f"#{member.id}"
new_user = {
"_id": user_id,
"user name": username,
"money": 0
}
self.bot.database["users"].insert_one(new_user)
userid = f"#{ctx.author.id}"
user_data = self.database["users"].find_one({"_id": userid})
targetUser = self.bot.database_funcs.get_id(username)
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 user_data is None or user_data["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)
self.addMoney(ctx.author, -1 * amount)
self.addMoney(user, amount)
await ctx.send(f"Transferred {amount} GwendoBucks to {username}")