feat: refactored balance command

This commit is contained in:
Seaswimmer 2023-08-01 17:52:14 -04:00
parent 2173998ced
commit d51f42289b
No known key found for this signature in database
GPG key ID: 5019678FD9CF50D8
2 changed files with 25 additions and 21 deletions

View file

@ -5,5 +5,6 @@
"short" : "Simple points system.", "short" : "Simple points system.",
"description" : "Implements a way for moderators to give out social-credit like points, dubbed 'sugoncredits' by the community.", "description" : "Implements a way for moderators to give out social-credit like points, dubbed 'sugoncredits' by the community.",
"tags" : ["bank"], "tags" : ["bank"],
"end_user_data_statement": "This cog stores no end user data." "end_user_data_statement": "This cog stores no end user data.",
"requirements": "inflect"
} }

View file

@ -1,7 +1,10 @@
import discord
from redbot.core import commands, checks, data_manager, Config
import sqlite3 import sqlite3
from sqlite3 import Error from sqlite3 import Error
import discord
import inflect
from redbot.core import Config, checks, commands, data_manager
from tabulate import tabulate
class SugonCredit(commands.Cog): class SugonCredit(commands.Cog):
"""Implements a way for moderators to give out social-credit like points, dubbed 'sugoncredits' by the community.""" """Implements a way for moderators to give out social-credit like points, dubbed 'sugoncredits' by the community."""
@ -20,6 +23,10 @@ class SugonCredit(commands.Cog):
con.commit() con.commit()
con.close() con.close()
def pluralize(word, count):
p = inflect.engine()
return p.plural(word, count)
def new_guild_generation(self, guild_id): def new_guild_generation(self, guild_id):
"""Adds a new table for a guild to the SQLite databse.""" """Adds a new table for a guild to the SQLite databse."""
con = sqlite3.connect(f'{self.data_path}') con = sqlite3.connect(f'{self.data_path}')
@ -71,27 +78,23 @@ class SugonCredit(commands.Cog):
@commands.guild_only() @commands.guild_only()
async def balance(self, ctx, user: discord.Member = None): async def balance(self, ctx, user: discord.Member = None):
"""Checks an account's balance.""" """Checks an account's balance."""
con = sqlite3.connect(f'{self.data_path}') target = user if user else ctx.author
con = sqlite3.connect(self.data_path)
cur = con.cursor() cur = con.cursor()
await self.new_guild_generation({ctx.guild.id}) await self.new_guild_generation({ctx.guild.id})
bank_name = await self.config.guild(ctx.guild).bank_name() bank_name = await self.config.guild(ctx.guild).get_raw('bank_name', default="Bank")
currency_name = await self.config.guild(ctx.guild).currency_name() currency_name = await self.config.guild(ctx.guild).get_raw('currency_name', default="Credit")
if user == None: cur.execute(f"SELECT user_id FROM {ctx.guild.id} WHERE user_id = ?;", (target.id,))
target = ctx.author if not cur.fetchone():
else:
target = user
if cur.execute(f'''SELECT user_id FROM {ctx.guild.id}
WHERE EXISTS (SELECT user_id FROM {ctx.guild.id} WHERE {target.id});''')=="FALSE":
await self.new_user_generation({ctx.guild.id}, target) await self.new_user_generation({ctx.guild.id}, target)
stored_username = cur.execute(f'''SELECT username FROM {ctx.guild.id} cur.execute(f"SELECT balance FROM {ctx.guild.id} WHERE user_id = ?;", (target.id,))
WHERE user_id = {target.id};''') bal = cur.fetchone()[0]
bal = cur.execute(f'''SELECT balance FROM {ctx.guild.id} output_bal = f'{bal:,}'
WHERE user_id = {target.id};''') pluralized_currency_name = await self.pluralize(currency_name, bal)
output_bal = (f'{bal:,}') embed_title = f"{bank_name} - Balance"
if bal == 1 or bal == -1: embed_color = await self.bot.get_embed_color(None)
embed=discord.Embed(title=f"{bank_name} - Balance", color=await self.bot.get_embed_color(None), description=f"{target.mention} has {output_bal} {currency_name}.") embed_description = f"{target.mention} has {output_bal} {pluralized_currency_name}."
else: embed = discord.Embed(title=embed_title, color=embed_color, description=embed_description)
embed=discord.Embed(title=f"{bank_name} - Balance", color=await self.bot.get_embed_color(None), description=f"{target.mention} has {output_bal} {currency_name}s.")
await ctx.send(embed=embed) await ctx.send(embed=embed)
con.close() con.close()