Skip to content
This repository was archived by the owner on Feb 7, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions cogs/isocard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

# Imports
import discord
import json
import random
import time
from framework.isobot.db.isocard import IsoCard
from discord import option, ApplicationContext, SlashCommandGroup
from discord.ext import commands

# Variables
with open("database/isocard.json", 'r', encoding="utf-8") as f: isocard_db = json.load(f)

def save():
with open("database/isocard.json", 'w+', encoding="utf-8") as f: json.dump(isocard_db, f, indent=4)
# Variables and Functions
isocard_db = IsoCard()

def generate_card_id() -> int:
# Generate 16 random digits and append to a str variable
Expand All @@ -38,24 +34,12 @@ def __init__(self, bot):
@option(name="ssc", description="The Special Security Code for your new card. (aka. CVV)", type=int)
async def register(self, ctx: ApplicationContext, ssc: int):
new_card_id = generate_card_id()
isocard_db[str(new_card_id)] = {
"cardholder_user_id": ctx.author.id,
"cardholder_name": ctx.author.name,
"ssc": ssc, # Special Security Code
"card_registration_timestamp": round(time.time()),
"type": "standard", # Card type
"config" : {
"spend_limit": 100000, # Daily spending limit for IsoCard
"shared_cardholder_ids": [], # Other users who can use this card
"card_label": None
}
}
save()
new_card_data = isocard_db.generate(new_card_id, ctx.author.id, ctx.author.name, ssc)
localembed = discord.Embed(title=":tada: Congratulations!", description="Your new IsoCard has successfully been registered!", color=discord.Color.green())
localembed.add_field(name="Cardholder name", value=ctx.author.name, inline=False)
localembed.add_field(name="Card number", value=new_card_id, inline=False)
localembed.add_field(name="SSC", value=f"`{ssc}`", inline=True)
localembed.add_field(name="Card registration date", value=f"<t:{isocard_db[str(new_card_id)]['card_registration_timestamp']}:d>", inline=False)
localembed.add_field(name="Card registration date", value=f"<t:{new_card_data['card_registration_timestamp']}:d>", inline=False)
localembed.set_footer(text="Always remember, NEVER share your card info to anyone!")
await ctx.respond(embed=localembed, ephemeral=True)

Expand All @@ -66,7 +50,7 @@ async def register(self, ctx: ApplicationContext, ssc: int):
@option(name="card_number", description="Enter your card number of the card you want to view.", type=int)
async def info(self, ctx: ApplicationContext, card_number: int):
try:
try: card_data = isocard_db[str(card_number)]
try: card_data = isocard_db.fetch_card_data(card_number)
except KeyError: return await ctx.respond("There was a problem with your card number. Please check it and try again.", ephemeral=True)
if card_data["cardholder_user_id"] != ctx.author.id: return await ctx.respond("You do not have permission to access this IsoCard.\n If you think this is an error, please contact the devs.", ephemeral=True)
localembed = discord.Embed(
Expand Down Expand Up @@ -117,11 +101,10 @@ async def my_card(self, ctx: ApplicationContext):
@option(name="card_number", description="Enter your card number that you want to work with.", type=int)
@option(name="new_label", description="What do you want your new card label to be?", type=str, default=None)
async def options_label(self, ctx: ApplicationContext, card_number: int, new_label: str):
try: card_data = isocard_db[str(card_number)]
try: card_data = isocard_db.fetch_card_data(card_number)
except KeyError: return await ctx.respond("There was a problem with your card number. Please check it and try again.", ephemeral=True)
if card_data["cardholder_user_id"] != ctx.author.id: return await ctx.respond("You do not have permission to access this IsoCard.\n If you think this is an error, please contact the devs.", ephemeral=True)
isocard_db[str(card_number)]["config"]["card_label"] = new_label
save()
isocard_db.set_card_label(card_number, new_label)
if new_label == None: return await ctx.respond(embed=discord.Embed(description=":white_check_mark: Your card label has been reset.", color=discord.Color.green()), ephemeral=True)
else: return await ctx.respond(embed=discord.Embed(description=":white_check_mark: Your card label has been edited.", color=discord.Color.green()), ephemeral=True)

Expand Down
51 changes: 51 additions & 0 deletions framework/isobot/db/isocard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""The framework database module for handling IsoCard data."""

# Imports
import json
import time
from colors import Colors as colors

# Functions
class IsoCard:
def __init__(self):
print(f"[framework/db/IsoCard] {colors.green}IsoCard db library initialized.{colors.end}")

def load(self) -> dict:
"""Loads the latest IsoCard database content from local storage."""
with open("database/isocard.json", "r", encoding="utf-8") as f: data = json.load(f)
return data

def save(self, data: dict) -> int:
"""Saves the latest cached database to local storage."""
with open("database/isocard.json", 'w+', encoding="utf-8") as f: json.dump(data, f, indent=4)
return 0

def fetch_card_data(self, card_id: int) -> dict:
"""Fetches the raw `dict` data related to the given IsoCard id.\n\nReturns data as `dict` if successful, returns `KeyError` if card id does not exist."""
isocard_db = self.load()
return isocard_db[str(card_id)]

def generate(self, new_card_id: int, user_id: int, user_name: str, ssc: int) -> dict:
"""Generates a new IsoCard with the given data, and returns that IsoCard data as a `dict` through the function."""
isocard_db = self.load()
isocard_db[str(new_card_id)] = {
"cardholder_user_id": user_id,
"cardholder_name": user_name,
"ssc": ssc, # Special Security Code
"card_registration_timestamp": round(time.time()),
"type": "standard", # Card type
"config" : {
"spend_limit": 100000, # Daily spending limit for IsoCard
"shared_cardholder_ids": [], # Other users who can use this card
"card_label": None
}
}
self.save(isocard_db)
return isocard_db[str(new_card_id)]

def set_card_label(self, card_id: int, label: str) -> int:
"""Changes the display label for a specific IsoCard."""
isocard_db = self.load()
isocard_db[str(card_id)]["config"]["card_label"] = label
self.save()
return 0