Skip to content

Instantly share code, notes, and snippets.

@ollieread
Created July 22, 2018 12:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ollieread/50f3bf03f3ea4e8c885b4fc74fecf82f to your computer and use it in GitHub Desktop.
Save ollieread/50f3bf03f3ea4e8c885b4fc74fecf82f to your computer and use it in GitHub Desktop.
Parser for discord permission int returned by the API, and an implementation of their Snowflake IDs.
<?php
namespace Ollieread\Discord\Support;
/**
* Class PermissionCalculator
*
* @package Ollieread\Discord\Support
*/
class PermissionParser
{
/**
* @var array
*/
protected static $permissions = [
'CREATE_INSTANT_INVITE' => 0x00000001,
'KICK_MEMBERS' => 0x00000002,
'BAN_MEMBERS' => 0x00000004,
'ADMINISTRATOR' => 0x00000008,
'MANAGE_CHANNELS' => 0x00000010,
'MANAGE_GUILD' => 0x00000020,
'ADD_REACTIONS' => 0x00000040,
'VIEW_AUDIT_LOG' => 0x00000080,
'VIEW_CHANNEL' => 0x00000400,
'SEND_MESSAGES' => 0x00000800,
'SEND_TTS_MESSAGES' => 0x00001000,
'MANAGE_MESSAGES' => 0x00002000,
'EMBED_LINKS' => 0x00004000,
'ATTACH_FILES' => 0x00008000,
'READ_MESSAGE_HISTORY' => 0x00010000,
'MENTION_EVERYONE' => 0x00020000,
'USE_EXTERNAL_EMOJIS' => 0x00040000,
'CONNECT' => 0x00100000,
'SPEAK' => 0x00200000,
'MUTE_MEMBERS' => 0x00400000,
'DEAFEN_MEMBERS' => 0x00800000,
'MOVE_MEMBERS' => 0x01000000,
'USE_VAD' => 0x02000000,
'CHANGE_NICKNAME' => 0x04000000,
'MANAGE_NICKNAMES' => 0x08000000,
'MANAGE_ROLES' => 0x10000000,
'MANAGE_WEBHOOKS' => 0x20000000,
'MANAGE_EMOJIS' => 0x40000000,
];
/**
* Parse a permission int to an array of useable permissions.
*
* @param int $permissions
*
* @return array
*/
public static function toArray(int $permissions): array
{
$parsedPermissions = [];
foreach (self::$permissions as $permission => $value) {
$parsedPermissions[$permission] = (bool)($permissions & $value);
}
return $parsedPermissions;
}
/**
* Parse a permission array to an int for sending over the API.
*
* @param array $permissions
*
* @return int
*/
public static function toInt(array $permissions): int
{
$parsedPermissions = 0;
foreach ($permissions as $permission => $presence) {
$permission = strtoupper($permission);
if ($presence && array_key_exists($permission, self::$permissions)) {
$parsedPermissions |= self::$permissions[$permission];
}
}
return $parsedPermissions;
}
/**
* Check whether a given permission exists.
*
* @param string $permission
*
* @return bool
*/
public static function exists(string $permission): bool
{
return array_key_exists(strtoupper($permission), self::$permissions);
}
}
<?php
namespace Ollieread\Discord\Support;
class Snowflake
{
/**
* @var string
*/
protected $id;
/**
* @var int
*/
protected $timestamp;
/**
* @var int
*/
protected $workerId;
/**
* @var int
*/
protected $processId;
/**
* @var int
*/
protected $increment;
public function __construct(string $id)
{
$this->id = $id;
$this->timestamp = ($this->id >> 22) + 1420070400000;
$this->workerId = ($this->id & 0x3E0000) >> 17;
$this->processId = ($this->id & 0x1F000) >> 12;
$this->increment = $this->id & 0xFFF;
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return int
*/
public function getTimestamp(): int
{
return $this->timestamp;
}
/**
* @return int
*/
public function getWorkerId(): int
{
return $this->workerId;
}
/**
* @return int
*/
public function getProcessId(): int
{
return $this->processId;
}
/**
* @return int
*/
public function getIncrement(): int
{
return $this->increment;
}
public function __toString()
{
return $this->id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment