import asyncio from cmath import inf import aiomysql from steam import SteamID from colors import * class Database: database = None permitions = {"VIP":("20:oa",2592000)} def __init__(self, host, user, password, db): self.host = host self.user = user self.password = password self.db = db async def connect(self): info("Create database pool") self.database = await aiomysql.create_pool( host = self.host, user = self.user, password = self.password, db=self.db ) async def disconnect(self): ok("Disconnect from database") await self.database.wait_closed() async def processing_vip(self, steamid: SteamID, amount, status = "VIP", who = "Donate.User"): check_status = await self.check_update_VIP(steamid) if check_status: if check_status > 1: info(f"Append more time to {steamid.community_url}") return await self.update_user(steamid, amount) else: info(f"Create new vip user {steamid.community_url}") return await self.add_new_user(steamid, amount, status, who) else: warning(f"{steamid.community_url} currently have ADMIN/MOD permitions") return False async def check_update_VIP(self, steamid: SteamID): # 0 = mod/admin | -1 = not found | 1 = need update query = f"SELECT * FROM `sm_admins` WHERE `identity` LIKE '{steamid.id2_zero}' ORDER BY `reg_date` DESC" async with self.database.acquire() as connection: async with connection.cursor(aiomysql.DictCursor) as cursor: await cursor.execute(query) for row in await cursor.fetchall(): if row["status"] == "VIP" and row["amount"] > 0: return 1 else: return 0 return -1 async def update_user(self, steamid: SteamID, amount): query = f"UPDATE `sm_admins` SET `amount`=`amount`+{amount}, `reg_date`=`reg_date` WHERE `identity` LIKE '{steamid.id2_zero}'" async with self.database.acquire() as connection: async with connection.cursor(aiomysql.DictCursor) as cursor: await cursor.execute(query) await connection.commit() return 101 async def add_new_user(self, steamid: SteamID, amount, status, who): settings = self.permitions.get(status) level, flags = settings[0].split(":") query = f"INSERT INTO `sm_admins` (`id`, `authtype`, `identity`, `password`, `flags`, `name`, `immunity`, `comment`, `status`, `reg_date`, `amount`) VALUES (NULL, 'steam', '{steamid.id2_zero}', NULL, '{flags}', '', '{level}', '{who}', '{status}', CURRENT_TIMESTAMP,'{amount}')" async with self.database.acquire() as connection: async with connection.cursor(aiomysql.DictCursor) as cursor: await cursor.execute(query) await connection.commit() return 100