Skip to content

Instantly share code, notes, and snippets.

@ariankordi
Last active June 7, 2020 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariankordi/36b8f07005937f711e77af38294d2e14 to your computer and use it in GitHub Desktop.
Save ariankordi/36b8f07005937f711e77af38294d2e14 to your computer and use it in GitHub Desktop.
simple php script for a discord application that adds a user to an already existing bot group via oauth2, resolves a code (((made this two days ago))
<?php
//ini_set('display_errors', true);
define('CLIENT_ID', '');
define('CLIENT_SECRET', '');
// token for a bot, needs to be prefixed with Bot
define('TOKEN', '');
// group channel that you have already created, lol,
define('CHANNEL_ID', '');
define('API_ENDPOINT', 'https://discord.com/api/v6');
// sobbing emoji
define('SOBBING', '&#128557;');
function reqFailed() {
// if there's an error then it will have already been printed at this point
?><style>
div {
font-size: 72px;
}
</style>
<div>
<?php
for($i = 0; $i < 70; $i++) {
// print sob emoji that many times
?><?=SOBBING?><?php
}
?>
</div>
<?php
}
// represents the full url of the page
$thisURL = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
if(empty($_GET['code'])) {
header('Location: /');
exit();
}
// so we have code now i guess
// get an oauth2 access token
$postData = array(
'grant_type' => 'authorization_code',
'scope' => 'gdm.join identify',
// if the request doesn't work, this might likely be the cause
'redirect_uri' => $thisURL,
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'code' => $_GET['code']
);
$requestOpts = array(
'http' => array(
// makes it so that it doesn't exception on bad requests and whatnot
'ignore_errors' => true,
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => http_build_query($postData)
)
);
$requestContext = stream_context_create($requestOpts);
ini_set('display_errors', true);
$responseContent = file_get_contents(API_ENDPOINT . '/oauth2/token', false, $requestContext);
ini_set('display_errors', false);
if(!$responseContent) {
reqFailed();
exit();
}
$oauthResponse = json_decode($responseContent);
if(isset($oauthResponse->error)) {
// oh no an error
header('Content-Type: text/plain');
if($oauthResponse->error === 'invalid_request') {
http_response_code(400);
if($oauthResponse->error_description === 'Invalid "code" in request.') {
echo 'this url is expired. ';
}
echo 'bad request!!!!';
exit();
}
http_response_code(500);
echo 'error. here, you figure it out:' . PHP_EOL;
print_r($oauthResponse);
}
// assuming that our response is valid now
if(!isset($oauthResponse->access_token)) {
http_response_code(500);
echo 'i didnt find an access token i dont know how to handle this im just gonna die';
exit();
}
// ughhh time to get the user
$requestOpts = array(
'http' => array(
'ignore_errors' => true,
'method' => 'GET',
'header' => 'Authorization: ' . $oauthResponse->token_type . ' ' . $oauthResponse->access_token
)
);
$requestContext = stream_context_create($requestOpts);
ini_set('display_errors', true);
$responseContent = file_get_contents(API_ENDPOINT . '/users/@me', false, $requestContext);
ini_set('display_errors', false);
if(!$responseContent) {
reqFailed();
exit();
}
$userResponse = json_decode($responseContent);
//$response->id
$postData = array(
'access_token' => $oauthResponse->access_token,
'nick' => 'seth',
);
$requestOpts = array(
'http' => array(
'ignore_errors' => true,
'method' => 'PUT',
'header' => 'Authorization: ' . TOKEN . "\r\n"
. 'Content-Type: application/json',
'content' => json_encode($postData)
)
);
$requestContext = stream_context_create($requestOpts);
ini_set('display_errors', true);
$responseContent = file_get_contents(ENDPOINT . '/channels/' . CHANNEL_ID
. '/recipients/' . $userResponse->id, false, $requestContext);
ini_set('display_errors', false);
if($responseContent === false) {
reqFailed();
exit();
}
// uhh error handling lol not
header('Content-Type: text/plain');
// accepted
http_response_code(202);
?>thank you, <?=$userResponse->username?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment