2023-03-05 22:53:54 -05:00
import discord
2023-03-14 19:42:44 -04:00
from redbot . core import commands , checks , data_manager , Config
2023-03-13 21:39:51 -04:00
import sqlite3
2023-03-05 22:53:54 -05:00
2023-03-05 22:55:04 -05:00
class SugonCredit ( commands . Cog ) :
2023-03-05 22:53:54 -05:00
""" Implements a way for moderators to give out social-credit like points, dubbed ' sugoncredits ' by the community. """
def __init__ ( self , bot ) :
self . bot = bot
2023-03-13 21:39:51 -04:00
self . config = Config . get_conf ( self , identifier = 47252584 )
self . config . register_global (
bank_name = " Social Credit Enforcement Agency " ,
currency_name = " Social Credit " ,
max_bal = 1000000000
)
con = sqlite3 . connect ( ' credit_db ' )
cur = con . cursor ( )
if cur . execute ( ''' SELECT name
FROM sqlite_master
WHERE type = ' table '
AND name = ' {credit} ' ; ''' ) == 0:
2023-03-14 19:42:44 -04:00
cur . execute ( ''' CREATE TABLE credit (username text, user_id text, balance real); ''' )
2023-03-13 21:39:51 -04:00
con . commit ( )
con . close ( )
else :
con . close ( )
2023-03-05 22:53:54 -05:00
2023-03-13 21:39:51 -04:00
def new_user_generation ( self , target ) :
""" Adds a new user to the SQLite database. """
2023-03-14 19:42:44 -04:00
username = str ( { target } )
2023-03-13 21:39:51 -04:00
con = sqlite3 . connect ( ' credit_db ' )
cur = con . cursor ( )
2023-03-14 19:42:44 -04:00
cur . execute ( f ''' INSERT INTO credit
VALUES ( ' {username} ' , { target . id } , 250 ) ; ''' )
con . commit ( )
con . close ( )
def username_updater ( self , target ) :
""" Updates a users ' username in the SQLite database. """
new_username = str ( target )
con = sqlite3 . connect ( ' credit_db ' )
cur = con . cursor ( )
cur . execute ( f ''' UPDATE credit
SET username = ' {new_username} '
WHERE user_id = { target . id } ; ''' )
2023-03-13 21:39:51 -04:00
con . commit ( )
con . close ( )
2023-03-05 22:53:54 -05:00
@commands.group ( autohelp = True , aliases = [ " sugoncredit " ] )
@commands.guild_only ( )
2023-03-05 22:56:00 -05:00
async def credit ( self , ctx ) :
2023-03-05 22:53:54 -05:00
""" Simple points system. """
@credit.command ( )
@commands.guild_only ( )
async def balance ( self , ctx , user : discord . Member = None ) :
""" Checks an account ' s balance. """
2023-03-13 21:39:51 -04:00
con = sqlite3 . connect ( ' credit_db ' )
cur = con . cursor ( )
bank_name = await self . config . guild ( ctx . guild ) . bank_name ( )
currency_name = await self . config . guild ( ctx . guild ) . currency_name ( )
2023-03-05 22:53:54 -05:00
if user == None :
target = ctx . author
else :
target = user
2023-03-14 19:42:44 -04:00
if cur . execute ( f ''' SELECT user_id FROM credit
WHERE EXISTS ( SELECT user_id FROM credit WHERE { target . id } ) ; ''' )== " FALSE " :
await self . new_user_generation ( self , target )
stored_username = cur . execute ( f ''' SELECT username FROM credit
WHERE user_id = { target . id } ; ''' )
if str ( target ) != stored_username :
await self . username_updater ( self , target )
bal = cur . execute ( f ''' SELECT balance FROM credit
WHERE user_id = { target . id } ; ''' )
2023-03-06 22:09:59 -05:00
output_bal = ( f ' { bal : , } ' )
2023-03-05 22:53:54 -05:00
if bal == 1 or bal == - 1 :
2023-03-06 22:09:59 -05:00
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 } . " )
2023-03-05 22:53:54 -05:00
else :
2023-03-06 22:09:59 -05:00
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. " )
2023-03-05 22:53:54 -05:00
await ctx . send ( embed = embed )
@credit.command ( )
@commands.guild_only ( )
@commands.mod ( )
async def add ( self , ctx , target : discord . Member , amount : int ) :
""" Adds credits to an account. """
2023-03-05 23:24:13 -05:00
try :
val = int ( amount )
except ValueError :
await ctx . send ( content = " ``amount`` must be a number! Please try again. " )
return
2023-03-14 19:42:44 -04:00
con = sqlite3 . connect ( ' credit_db ' )
cur = con . cursor ( )
2023-03-06 23:03:37 -05:00
image = discord . File ( fp = data_manager . bundled_data_path ( self ) / " add.png " , filename = " Add.png " )
2023-03-13 21:39:51 -04:00
bank_name = await self . config . bank_name ( )
currency_name = await self . config . currency_name ( )
max_bal = await self . config . max_bal ( )
2023-03-14 19:42:44 -04:00
if cur . execute ( f ''' SELECT user_id FROM credit
WHERE EXISTS ( SELECT user_id FROM credit WHERE { target . id } ) ; ''' )== " FALSE " :
await self . new_user_generation ( self , target )
stored_username = cur . execute ( f ''' SELECT username FROM credit
WHERE user_id = { target . id } ; ''' )
if str ( target ) != stored_username :
await self . username_updater ( self , target )
bal = cur . execute ( f ''' SELECT balance FROM credit
WHERE user_id = { target . id } ; ''' )
current_bal = cur . execute ( f ''' SELECT balance FROM credit
WHERE user_id = { target . id } ; ''' )
2023-03-05 22:53:54 -05:00
new_bal = current_bal + amount
2023-03-06 22:09:59 -05:00
output_amount = ( f ' { val : , } ' )
output_new_bal = ( f ' { new_bal : , } ' )
output_max_bal = ( f ' { max_bal : , } ' )
2023-03-05 22:53:54 -05:00
if new_bal > max_bal :
2023-03-06 22:09:59 -05:00
await ctx . send ( content = f " You are attempting to set { target . mention } ' s balance to above { output_max_bal } . Please try again! " )
2023-03-05 22:53:54 -05:00
return
2023-03-06 21:21:14 -05:00
elif ctx . guild . id == 204965774618656769 :
2023-03-06 21:56:38 -05:00
logging_channel = self . bot . get_channel ( 1082495815878189076 )
2023-03-14 19:42:44 -04:00
cur . execute ( f ''' UPDATE credit
SET balance = { new_bal }
WHERE user_id = { target . id } ; ''' )
2023-03-06 22:09:59 -05:00
await ctx . send ( content = f " { target . mention } now has { output_amount } more SugonCredit, with a total of { output_new_bal } ! " )
2023-03-06 21:21:14 -05:00
if amount == 1 or amount == - 1 :
2023-03-06 23:02:51 -05:00
await target . send ( content = f " You gained { output_amount } SugonCredit! Good work community member! You now have { output_new_bal } SugonCredits. " , file = image )
2023-03-06 21:21:14 -05:00
else :
2023-03-06 23:02:51 -05:00
await target . send ( content = f " You gained { output_amount } SugonCredits! Good work community member! You now have { output_new_bal } SugonCredits. " , file = image )
2023-03-06 21:56:38 -05:00
logging_embed = discord . Embed ( title = " SugonCredit Added " , color = await self . bot . get_embed_color ( None ) , description = f " { ctx . author . name } # { ctx . author . discriminator } ( { ctx . author . id } ) added { amount } SugonCredit to { target . name } # { target . discriminator } ( { target . id } )! They now have { new_bal } SugonCredit. " )
await logging_channel . send ( embed = logging_embed )
2023-03-06 21:27:42 -05:00
elif ctx . guild . id != 204965774618656769 :
2023-03-06 22:09:59 -05:00
embed = discord . Embed ( title = f " { bank_name } - Add " , color = await self . bot . get_embed_color ( None ) , description = f " { target . mention } ' s { currency_name } balance has been increased by { output_amount } . \n Current balance is { output_new_bal } . " )
2023-03-14 19:42:44 -04:00
cur . execute ( f ''' UPDATE credit
SET balance = { new_bal }
WHERE user_id = { target . id } ; ''' )
2023-03-05 22:53:54 -05:00
await ctx . send ( embed = embed )
2023-03-14 19:42:44 -04:00
con . commit ( )
con . close ( )
2023-03-05 22:53:54 -05:00
@credit.command ( )
@commands.guild_only ( )
@commands.mod ( )
async def remove ( self , ctx , target : discord . Member , amount : int ) :
""" Removes credits from an account. """
2023-03-05 23:24:13 -05:00
try :
val = int ( amount )
except ValueError :
await ctx . send ( content = " ``amount`` must be a number. Please try again! " )
return
2023-03-06 23:03:37 -05:00
image = discord . File ( fp = data_manager . bundled_data_path ( self ) / " remove.mp4 " , filename = " MEGA_BASE.mp4 " )
2023-03-14 19:42:44 -04:00
con = sqlite3 . connect ( ' credit_db ' )
cur = con . cursor ( )
2023-03-13 21:39:51 -04:00
bank_name = await self . config . bank_name ( )
currency_name = await self . config . currency_name ( )
max_bal = await self . config . max_bal ( )
2023-03-14 19:42:44 -04:00
if cur . execute ( f ''' SELECT user_id FROM credit
WHERE EXISTS ( SELECT user_id FROM credit WHERE { target . id } ) ; ''' )== " FALSE " :
await self . new_user_generation ( self , target )
stored_username = cur . execute ( f ''' SELECT username FROM credit
WHERE user_id = { target . id } ; ''' )
if str ( target ) != stored_username :
await self . username_updater ( self , target )
current_bal = cur . execute ( f ''' SELECT balance FROM credit
WHERE user_id = { target . id } ; ''' )
2023-03-05 22:53:54 -05:00
new_bal = current_bal - amount
2023-03-06 22:09:59 -05:00
output_amount = ( f ' { val : , } ' )
output_new_bal = ( f ' { new_bal : , } ' )
2023-03-13 21:39:51 -04:00
output_max_bal = ( f ' { max_bal : , } ' )
2023-03-14 19:45:50 -04:00
if new_bal > max_bal :
2023-03-13 21:39:51 -04:00
await ctx . send ( content = f " You are attempting to set { target . mention } ' s balance to above { output_max_bal } . Please try again! " )
2023-03-06 21:21:14 -05:00
elif ctx . guild . id == 204965774618656769 :
2023-03-14 19:42:44 -04:00
cur . execute ( f ''' UPDATE credit
SET balance = { new_bal }
WHERE user_id = { target . id } ; ''' )
2023-03-06 21:56:38 -05:00
logging_channel = self . bot . get_channel ( 1082495815878189076 )
2023-03-06 22:09:59 -05:00
await ctx . send ( content = f " { target . mention } now has { output_amount } less SugonCredit, with a total of { output_new_bal } ! \n If this is a punishment, do better Galaxy Player! Re-education mods will be sent to your DM ' s if your SugonCredit drops to a substantially low amount! " )
2023-03-06 21:21:14 -05:00
if amount == 1 or amount == - 1 :
2023-03-06 23:02:01 -05:00
await target . send ( content = f " __MESSAGE FROM THE MINISTRY OF THE MEGA BASE__ \n \n (我们的) { output_amount } SugonCredit has been taken from your account. Citizen, do not continue to preform bad actions! Glory to the Galaxy Communist Party! " , file = image )
2023-03-06 21:21:14 -05:00
else :
2023-03-06 23:02:01 -05:00
await target . send ( content = f " __MESSAGE FROM THE MINISTRY OF THE MEGA BASE__ \n \n (我们的) { output_amount } SugonCredits have been taken from your account. Citizen, do not continue to preform bad actions! Glory to the Galaxy Communist Party! " , file = image )
2023-03-06 22:09:59 -05:00
logging_embed = discord . Embed ( title = " SugonCredit Removed " , color = await self . bot . get_embed_color ( None ) , description = f " { ctx . author . name } # { ctx . author . discriminator } ( { ctx . author . id } ) removed { output_amount } SugonCredit from { target . name } # { target . discriminator } ( { target . id } )! They now have { output_new_bal } SugonCredit. " )
2023-03-06 21:58:50 -05:00
await logging_channel . send ( embed = logging_embed )
2023-03-06 21:25:52 -05:00
elif ctx . guild . id != 204965774618656769 :
2023-03-06 22:09:59 -05:00
embed = discord . Embed ( title = f " { bank_name } - Remove " , color = await self . bot . get_embed_color ( None ) , description = f " { target . mention } ' s { currency_name } balance has been decreased by { output_amount } . \n Current balance is { output_new_bal } . " )
2023-03-06 21:25:52 -05:00
await ctx . send ( embed = embed )
2023-03-14 19:42:44 -04:00
cur . execute ( f ''' UPDATE credit
SET balance = { new_bal }
WHERE user_id = { target . id } ; ''' )
con . commit ( )
con . close ( )