wohali / oauth2-discord-new

New Discord Provider for the OAuth 2.0 Client
MIT License
119 stars 22 forks source link

Accept invite to server #4

Closed Feerko closed 6 years ago

Feerko commented 6 years ago

Hello, how can i make user accept invite to my discord using your code?

Feerko commented 6 years ago

i'm trying with this:

    $user = $provider->getResourceOwner($token);
    $guilds = $user->guilds;
    $connections = $user->connections;
    $invite = $user->acceptInvite('https://discord.gg/blabla');

but this doesn't work :(

wohali commented 6 years ago

This library won't do that. This library is just for requesting an OAuth token from Discord for use with your client app.

Once you have the token, you need to use a library such as Restcord to do the rest.

You wouldn't use an invite link, either. Instead you'd just join them to your guild using a bot. Sample code is something like this:

use RestCord\DiscordClient;
use Wohali\OAuth2\Client\Provider\Discord;

/* Get this number via the desktop client; enable Developer mode, right click on your server and pick Copy ID */
$guildId = 1111111111; 

/* Get this token from the bot registration API at the main Discord website. Be sure to join your bot to your guild! */
$bot_token = 111111111;

$user = $provider->getResourceOwner($token);
$discord_id = $user->getId();
$args = [
  'guild.id' => $guildId,
  'user.id' => intval($discord_id),
  'access_token' => $token
]

$restcord = new DiscordClient([
  token => $bot_token;
]);
$restcord->guild->addGuildMember($args);
Feerko commented 6 years ago
<?php

  require __DIR__ . '/vendor/autoload.php';

  use Wohali\OAuth2\Client\Provider\Discord;
  use RestCord\DiscordClient;

  session_start();

  $provider = new \Wohali\OAuth2\Client\Provider\Discord([
      'clientId' => 'xxx',
      'clientSecret' => 'xxx',
      'redirectUri' => 'xxx'
  ]);

  if (!isset($_GET['code'])) {

      $authUrl = $provider->getAuthorizationUrl();
      $_SESSION['oauth2state'] = $provider->getState();
      header('Location: ' . $authUrl);

  } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

      unset($_SESSION['oauth2state']);
      exit('Invalid state');

  } else {

    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);

    try {

      $user = $provider->getResourceOwner($token);
      $discord_id = $user->getId();
      $args = [
        'guild.id' => xxx,
        'user.id' => intval($discord_id),
        'access_token' => $token
      ];

      $client = new DiscordClient([
        'token' => 'xxx'
      ]);

      var_dump($client->guild->addGuildMember($args));

    } catch (Exception $e) {
      exit("Oh dear... $e");
    }
  }

?>
Feerko commented 6 years ago

That's my code and this doesn't work for me, I don't know what's wrong.

Feerko commented 6 years ago

i get: FORBIDDEN

Feerko commented 6 years ago

Oh dear... GuzzleHttp\Exception\ClientException: Client error: PUT https://discordapp.com/api/v6/guilds/425673791549472780/members/371532684716933120 resulted in a 400 BAD REQUEST response: (truncated...)

HayateLaTech commented 6 years ago

http://www.restcord.com/Guild/Add%20Guild%20Member/

Is the Bot part of the Server and has the CREATE_INSTANT_INVITE permission? @Feerko

Feerko commented 6 years ago

@HayateLaTech

zrzut ekranu 2018-03-22 o 09 29 32 zrzut ekranu 2018-03-22 o 09 29 28
Feerko commented 6 years ago

When i'm select guilds.join i get Invalid State

Feerko commented 6 years ago

I'm get this:

GuzzleHttp\Exception\ClientException: Client error: PUT https://discordapp.com/api/v6/guilds/422016694408577025/members/373878398168072193 resulted in a 400 BAD REQUEST response: (truncated...) in /var/www/discord/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace: #0 /var/www/discord/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response)) #2 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array) #3 /var/www/discord/vendor/guzzlehttp/promises/src/TaskQueue.php(47): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise{closure}() #4 /var/www/discord/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(96): GuzzleHttp\Promise\TaskQueue->run() #5 /var/www/discord/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(123): GuzzleHttp\Handler\CurlMultiHandler->tick() #6 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(246): GuzzleHttp\Handler\CurlMultiHandler->execute(true) #7 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(223): GuzzleHttp\Promise\Promise->invokeWaitFn() #8 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(267): GuzzleHttp\Promise\Promise->waitIfPending() #9 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(225): GuzzleHttp\Promise\Promise->invokeWaitList() #10 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending() #11 /var/www/discord/vendor/guzzlehttp/promises/src/Coroutine.php(65): GuzzleHttp\Promise\Promise->wait() #12 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(246): GuzzleHttp\Promise\Coroutine->GuzzleHttp\Promise{closure}(true) #13 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(223): GuzzleHttp\Promise\Promise->invokeWaitFn() #14 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending() #15 /var/www/discord/vendor/guzzlehttp/promises/src/Coroutine.php(85): GuzzleHttp\Promise\Promise->wait(true) #16 /var/www/discord/vendor/guzzlehttp/command/src/ServiceClient.php(77): GuzzleHttp\Promise\Coroutine->wait() #17 /var/www/discord/vendor/guzzlehttp/command/src/ServiceClient.php(153): GuzzleHttp\Command\ServiceClient->execute(Object(GuzzleHttp\Command\Command)) #18 /var/www/discord/vendor/restcord/restcord/src/OverriddenGuzzleClient.php(87): GuzzleHttp\Command\ServiceClient->call('addGuildMember', Array) #19 /var/www/discord/index.php(49): RestCord\OverriddenGuzzleClient->call('addGuildMember', Array) #20 {main} Next GuzzleHttp\Command\Exception\CommandClientException: There was an error executing the addGuildMember command: Client error: PUT https://discordapp.com/api/v6/guilds/422016694408577025/members/373878398168072193 resulted in a 400 BAD REQUEST response: (truncated...) in /var/www/discord/vendor/guzzlehttp/command/src/Exception/CommandException.php:57 Stack trace: #0 /var/www/discord/vendor/guzzlehttp/command/src/ServiceClient.php(179): GuzzleHttp\Command\Exception\CommandException::fromPrevious(Object(GuzzleHttp\Command\Command), Object(GuzzleHttp\Exception\ClientException)) #1 [internal function]: GuzzleHttp\Command\ServiceClient->GuzzleHttp\Command{closure}() #2 /var/www/discord/vendor/guzzlehttp/promises/src/Coroutine.php(142): Generator->throw(Object(GuzzleHttp\Exception\ClientException)) #3 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Promise\Coroutine->_handleFailure(Object(GuzzleHttp\Exception\ClientException)) #4 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(2, Object(GuzzleHttp\Exception\ClientException), Array) #5 /var/www/discord/vendor/guzzlehttp/promises/src/TaskQueue.php(47): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise{closure}() #6 /var/www/discord/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(96): GuzzleHttp\Promise\TaskQueue->run() #7 /var/www/discord/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(123): GuzzleHttp\Handler\CurlMultiHandler->tick() #8 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(246): GuzzleHttp\Handler\CurlMultiHandler->execute(true) #9 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(223): GuzzleHttp\Promise\Promise->invokeWaitFn() #10 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(267): GuzzleHttp\Promise\Promise->waitIfPending() #11 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(225): GuzzleHttp\Promise\Promise->invokeWaitList() #12 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending() #13 /var/www/discord/vendor/guzzlehttp/promises/src/Coroutine.php(65): GuzzleHttp\Promise\Promise->wait() #14 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(246): GuzzleHttp\Promise\Coroutine->GuzzleHttp\Promise{closure}(true) #15 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(223): GuzzleHttp\Promise\Promise->invokeWaitFn() #16 /var/www/discord/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending() #17 /var/www/discord/vendor/guzzlehttp/promises/src/Coroutine.php(85): GuzzleHttp\Promise\Promise->wait(true) #18 /var/www/discord/vendor/guzzlehttp/command/src/ServiceClient.php(77): GuzzleHttp\Promise\Coroutine->wait() #19 /var/www/discord/vendor/guzzlehttp/command/src/ServiceClient.php(153): GuzzleHttp\Command\ServiceClient->execute(Object(GuzzleHttp\Command\Command)) #20 /var/www/discord/vendor/restcord/restcord/src/OverriddenGuzzleClient.php(87): GuzzleHttp\Command\ServiceClient->call('addGuildMember', Array) #21 /var/www/discord/index.php(49): RestCord\OverriddenGuzzleClient->call('addGuildMember', Array) #22 {main}

HayateLaTech commented 6 years ago

@Feerko you need to add the guild.join scope to the oauth2-request: https://github.com/wohali/oauth2-discord-new#managing-scopes

Feerko commented 6 years ago
      $options = [
          'scope' => ['guilds.join']
      ];
      $authUrl = $provider->getAuthorizationUrl($options);
      $_SESSION['oauth2state'] = $provider->getState();
      header('Location: ' . $authUrl);

like that? still doesn't work

Feerko commented 6 years ago

http://derpmc.pl/discord/index.php it's link to this script