update
Tá an tiomantas seo le fáil i:
76
src/Bot.php
Comhad gnáth
76
src/Bot.php
Comhad gnáth
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace VoiceState;
|
||||
|
||||
use \Monolog\Logger;
|
||||
use Discord\Parts\WebSockets\VoiceStateUpdate;
|
||||
|
||||
class Bot
|
||||
{
|
||||
public const NAME = "VoiceState";
|
||||
public const MAX_LOG_FILE_COUNT = 7;
|
||||
private const STATE_FILE_TEMPLATE = "%s.json";
|
||||
|
||||
private const STATE_DEFAULT = [
|
||||
'state' => null,
|
||||
];
|
||||
private const STATE_ACTIVE = 'active';
|
||||
private const STATE_MUTE = 'muted';
|
||||
private const STATE_SELF_MUTE = 'selfMuted';
|
||||
|
||||
private string $stateFileDir;
|
||||
private Logger $logger;
|
||||
|
||||
public function __construct(string $stateFileDir, Logger $logger)
|
||||
{
|
||||
if (!is_dir($stateFileDir)) {
|
||||
throw new \RuntimeException("stateFileDir is no directory");
|
||||
}
|
||||
|
||||
$this->stateFileDir = rtrim($stateFileDir, '/');
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function updateVoiceState(VoiceStateUpdate $voiceState): void
|
||||
{
|
||||
try {
|
||||
$stateFile = $this->stateFileDir . '/' . sprintf(self::STATE_FILE_TEMPLATE, $voiceState->user_id);
|
||||
|
||||
if (!file_exists($stateFile)) {
|
||||
$this->createStatusFile($stateFile);
|
||||
}
|
||||
|
||||
$state = $this->getState($voiceState);
|
||||
file_put_contents($stateFile, json_encode($state));
|
||||
} catch (\Throwable $t) {
|
||||
$this->logger->error($t);
|
||||
}
|
||||
}
|
||||
|
||||
private function getState(VoiceStateUpdate $voiceState)
|
||||
{
|
||||
$state = self::STATE_DEFAULT;
|
||||
$state['state'] = self::STATE_ACTIVE;
|
||||
if ($voiceState->mute) {
|
||||
$state['state'] = self::STATE_MUTE;
|
||||
}
|
||||
if ($voiceState->self_mute) {
|
||||
$state['state'] = self::STATE_SELF_MUTE;
|
||||
}
|
||||
|
||||
return $state;
|
||||
}
|
||||
|
||||
private function createStatusFile(string $stateFile): void
|
||||
{
|
||||
if (file_exists($stateFile)) {
|
||||
return;
|
||||
}
|
||||
|
||||
file_put_contents($stateFile, json_encode(self::STATE_DEFAULT));
|
||||
|
||||
if (!file_exists($stateFile)) {
|
||||
throw new \RuntimeException("could not create statefile: " . $stateFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
Tagairt in Eagrán Nua
Cuir bac ar úsáideoir