Skip to content

Commit

Permalink
Database: fixed users weren't saved to cache on checking ban of users
Browse files Browse the repository at this point in the history
  • Loading branch information
bariscodefxy committed Apr 21, 2024
1 parent bc4e0c2 commit 1257077
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -60,7 +60,7 @@ public function __construct()
}
$this->isConnected = true;
$this->createTables();
$this->bannedUsers = new HashMap();
$this->cachedUsers = new HashMap();
}

/**
Expand Down Expand Up @@ -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)
);
Expand All @@ -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)
);
}
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 1257077

Please sign in to comment.