Mise en place intégration Discord

This commit is contained in:
Angels-dev
2024-08-22 02:37:00 +02:00
parent 2be55bc65d
commit bc4489098d
34 changed files with 4054 additions and 4 deletions

View File

@@ -0,0 +1,125 @@
<?php
namespace App\Service;
use App\Exception\DiscordApiException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
class DiscordApiService
{
private $client;
private $clientId;
private $clientSecret;
private $redirectUri;
private $baseUrl;
public function __construct(HttpClientInterface $client)
{
$this->client = $client;
$this->clientId = $_ENV['DISCORD_CLIENT_ID'];
$this->clientSecret = $_ENV['DISCORD_CLIENT_SECRET'];
$this->redirectUri = $_ENV['DISCORD_BACKEND_REDIRECT_URI'];
$this->baseUrl = $_ENV['DISCORD_API_BASE_URL'];
}
public function exchangeCodeForToken(string $code): array
{
try {
$response = $this->client->request('POST', $this->baseUrl . '/oauth2/token', [
'body' => [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $this->redirectUri,
],
]);
return $response->toArray();
} catch (ClientExceptionInterface | ServerExceptionInterface | RedirectionExceptionInterface | TransportExceptionInterface $e) {
throw new DiscordApiException($e->getCode(), 'Erreur lors de la requête à l\'API Discord: ' . $e->getMessage(), $e);
}
}
public function refreshToken(string $refreshToken): array
{
try {
$response = $this->client->request('POST', $this->baseUrl . '/oauth2/token', [
'body' => [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
'redirect_uri' => $this->redirectUri,
],
]);
return $response->toArray();
} catch (ClientExceptionInterface | ServerExceptionInterface | RedirectionExceptionInterface | TransportExceptionInterface $e) {
throw new DiscordApiException($e->getCode(), 'Erreur lors de la requête à l\'API Discord: ' . $e->getMessage(), $e);
}
}
public function getBotsData(string $accessToken): array
{
/**
$output = array();
$botsId = [$_ENV['DISCORD_BOT_TAMISEUR_ID'], $_ENV['DISCORD_BOT_GROOVE_ID'], $_ENV['DISCORD_BOT_FUNKY_ID'], $_ENV['DISCORD_BOT_JUJUL_ID'], $_ENV['DISCORD_BOT_CHANTIER_ID']];
foreach($botsId as $botId)
{
try {
$response = $this->client->request('GET', $this->baseUrl . '/users/' . $botId, [
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
],
]);
array_push($output, $response->toArray());
} catch (ClientExceptionInterface | ServerExceptionInterface | RedirectionExceptionInterface | TransportExceptionInterface $e) {
throw new DiscordApiException($e->getCode(), 'Erreur lors de la requête à l\'API Discord: ' . $e->getMessage(), $e);
}
}
return new JsonResponse($output);
*/
try {
$response = $this->client->request('GET', $this->baseUrl . '/users/' . '262299921082875904', [
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
],
]);
return $response->toArray();
} catch (ClientExceptionInterface | ServerExceptionInterface | RedirectionExceptionInterface | TransportExceptionInterface $e) {
throw new DiscordApiException($e->getCode(), 'Erreur lors de la requête à l\'API Discord: ' . $e->getMessage(), $e);
}
}
public function getUserData(string $accessToken): array
{
try {
$response = $this->client->request('GET', $this->baseUrl . '/users/@me', [
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
],
]);
return $response->toArray();
} catch (ClientExceptionInterface | ServerExceptionInterface | RedirectionExceptionInterface | TransportExceptionInterface $e) {
throw new DiscordApiException($e->getCode(), 'Erreur lors de la requête à l\'API Discord: ' . $e->getMessage(), $e);
}
}
public function getUserGuilds(string $accessToken): array
{
try {
$response = $this->client->request('GET', $this->baseUrl . '/users/@me/guilds', [
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
],
]);
return $response->toArray();
} catch (ClientExceptionInterface | ServerExceptionInterface | RedirectionExceptionInterface | TransportExceptionInterface $e) {
throw new DiscordApiException($e->getCode(), 'Erreur lors de la requête à l\'API Discord: ' . $e->getMessage(), $e);
}
}
}