From 1257077ce974d5297b9c864de93cd0e32e00b79f Mon Sep 17 00:00:00 2001 From: bariscodefx Date: Sun, 21 Apr 2024 09:07:35 +0300 Subject: [PATCH] Database: fixed users weren't saved to cache on checking ban of users --- src/database/Database.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/database/Database.php b/src/database/Database.php index 88096e7..9edf330 100644 --- a/src/database/Database.php +++ b/src/database/Database.php @@ -38,11 +38,11 @@ class Database extends PDO public $isConnected = false; /** - * Hashmap of banned users + * Hashmap of cached users for checking bot ban * * @var HashMap */ - private HashMap $bannedUsers; + private HashMap $cachedUsers; /** * __construct @@ -60,7 +60,7 @@ public function __construct() } $this->isConnected = true; $this->createTables(); - $this->bannedUsers = new HashMap(); + $this->cachedUsers = new HashMap(); } /** @@ -746,7 +746,7 @@ public function setRPGItemType(int $id, int $type): bool */ public function banUserFromBot(int $discord_id): ?\PDOStatement { - $this->bannedUsers->set((string)$discord_id, true); + $this->cachedUsers->set((string)$discord_id, true); return $this->query( sprintf("INSERT INTO bans SET discord_id = %d", $discord_id) ); @@ -760,8 +760,8 @@ public function banUserFromBot(int $discord_id): ?\PDOStatement */ public function unbanUserFromBot(int $discord_id): ?\PDOStatement { - $this->bannedUsers->set((string)$discord_id, false); - return $this->query( + $this->cachedUsers->set((string)$discord_id, false); + return $this->query( sprintf("DELETE FROM bans WHERE discord_id = %d", $discord_id) ); } @@ -774,15 +774,17 @@ public function unbanUserFromBot(int $discord_id): ?\PDOStatement */ public function isUserBannedFromBot(int $discord_id): bool { - if ($this->bannedUsers->get((string)$discord_id)) + if ($this->cachedUsers->get((string)$discord_id)) { return true; } - return $this->query( + $banned = $this->query( sprintf( "SELECT * FROM bans WHERE discord_id = %d", $discord_id ) )->fetch() ? true : false; + $this->cachedUsers->set((string)$discord_id, $banned); + return $banned; } /**