routmoute/routmoute-discord-bundle

This bundle is simple access to discord OAuth & API for Symfony 5

1.1.0 2021-02-13 19:11 UTC

This package is auto-updated.

Last update: 2021-09-02 18:41:09 UTC


README

Manual Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Step 1: Create configuration file

Create configuration file config/packages/routmoute_discord.yaml and modify scopes if you want

// config/packages/routmoute_discord.yaml

routmoute_discord:
    oauth:
        client_id: '%env(ROUTMOUTE_DISCORD_CLIENT_ID)%'
        client_secret: '%env(ROUTMOUTE_DISCORD_CLIENT_SECRET)%'
        scope:
            - identify
            - email
    api:
        bot_token: '%env(ROUTMOUTE_DISCORD_BOT_TOKEN)%'

Step 2: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

composer require routmoute/routmoute-discord-bundle

Configuration

Step 1: Create your Discord Application

  • Go to https://discord.com/developers/applications
  • Create a New Application
  • Copy CLIENT ID and CLIENT SECRET for next step
  • Go to OAuth2 Tab
  • Add Redirect https://yourDomain.domain/receiveDiscord
  • Go to Bot Tab and copy TOKEN for next step

Step 2: Create your env variables

Add this environments vars in your .env file.

ROUTMOUTE_DISCORD_CLIENT_ID=YourClientId
ROUTMOUTE_DISCORD_CLIENT_SECRET=YourClientSecret
ROUTMOUTE_DISCORD_BOT_TOKEN=YourBotToken

Usage (for Symfony 5)

Discord OAuth

Step 1: Create Controller in your App

Create Controller, for exemple src/Controller/DiscordOAuthController.php

// src/Controller/DiscordOAuthController.php

<?php

namespace App\Controller;

use Routmoute\Bundle\RoutmouteDiscordBundle\Service\RoutmouteDiscordOAuthService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class DiscordOAuthController extends AbstractController
{
    /**
    * @Route("/connectToDiscord", name="routmoute_discord_redirect", methods="GET")
    */
    public function redirectToDiscord(RoutmouteDiscordOAuthService $oAuthService, UrlGeneratorInterface $urlGenerator): RedirectResponse
    {
        $redirectUrl = $urlGenerator->generate('routmoute_discord_receiver', [], UrlGeneratorInterface::ABSOLUTE_URL);
        return new RedirectResponse($oAuthService->getRedirectDiscordUrl($redirectUrl));
    }

    /**
    * @Route("/receiveDiscord", name="routmoute_discord_receiver", methods="GET")
    */
    public function receiveFromDiscordAuthorize(Request $request, RoutmouteDiscordOAuthService $oAuthService, UrlGeneratorInterface $urlGenerator): RedirectResponse
    {
        $redirectUrl = $urlGenerator->generate('routmoute_discord_receiver', [], UrlGeneratorInterface::ABSOLUTE_URL);
        $userData = $oAuthService->getUserData($request, $redirectUrl);

        // TODO: Process userData and change path_to_redirect

        return $this->redirectToRoute('path_to_redirect');
    }
}

Step 2: Create your redirect button

Create a button in your frontend that redirect to routmoute_discord_redirect path.

for example, in twig template:

<a href="{{ path('routmoute_discord_redirect') }}">
    <button type="button">Link my account with discord</button>
</a>

Discord API (Bot)

Example usage in Controller:

<?php
namespace App\Controller;

use Routmoute\Bundle\RoutmouteDiscordBundle\Service\RoutmouteDiscordApiService;

class MyController extends AbstractController
{
    public function index(RoutmouteDiscordApiService $discordAPI)
    {
        $discordId = 'theUserDiscordId';

        $userInfos = $discordAPI->getUserFromDiscordId($discordId);

        $userInfos["id"];
        $userInfos["username"];
        $userInfos["discriminator"];
        $userInfos["avatar"];
    }
}

https://discord.com/developers/docs/resources/user

Parameters

client_id

Required The CLIENT ID provided by discord

client_secret

Required The CLIENT SECRET provided by discord

scope

Required The Discord API scopes - https://discord.com/developers/docs/topics/oauth2#shared-resources

  • identify - discordId, avatar, username, discriminator
  • email - email
  • ...

bot_token

Required The Bot TOKEN provided by discord