Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding guild channels to the simple-demo. #23

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions demos/simple-demo/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
# example: https://mydomain.com/test/includes/login.php
$redirect_url = "";


# GETTING CHANNELS
# - Setting the variable below with server ID will provide you with access to all the channels inside the server.
$guild_id = null;


# IMPORTANT READ THIS:
# - Set the `$bot_token` to your bot token if you want to use guilds.join scope to add a member to your server
# - Check login.php for more detailed info on this.
Expand Down
18 changes: 17 additions & 1 deletion demos/simple-demo/includes/discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
$GLOBALS['base_url'] = "https://discord.com";

# Setting bot token for related requests
$GLOBALS['bot_token'] = null;
require "../config.php";
$GLOBALS['bot_token'] = $bot_token;

# A function to generate a random string to be used as state | (protection against CSRF)
function gen_state()
Expand Down Expand Up @@ -55,6 +56,21 @@ function init($redirect_url, $client_id, $client_secret, $bot_token = null)
$_SESSION['access_token'] = $results['access_token'];
}

# A function to get the channels in a guild | (requires bot token)
function get_channels($guild_id)
{
$url = $GLOBALS['base_url'] . "/api/guilds/$guild_id/channels";
$headers = array('Content-Type: application/x-www-form-urlencoded', 'Authorization: Bot ' . $GLOBALS['bot_token']);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
curl_close($curl);
$results = json_decode($response, true);
return $results;
}

# A function to get user information | (identify scope)
function get_user($email = null)
{
Expand Down
6 changes: 6 additions & 0 deletions demos/simple-demo/includes/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
# Adding user to guild | (guilds.join scope)
# join_guild('SERVER_ID_HERE');

# Fetching guild channels | (guilds scope)

if($guild_id != null) {
$_SESSION['channels'] = get_channels($guild_id);
}

# Fetching user guild details | (guilds scope)
$_SESSION['guilds'] = get_guilds();

Expand Down
20 changes: 20 additions & 0 deletions demos/simple-demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@
?>
</table>
<br>
<h2> Guild Channels:</h2>
<table border="1">
<tr>
<th>NAME</th>
<th>ID</th>
</tr>
<?php
for ($i = 0; $i < sizeof($_SESSION['channels']); $i++) {

echo "<tr><td>";
echo $_SESSION['channels'][$i]['name'];
echo "<td>";
echo $_SESSION['channels'][$i]['id'];
echo "</td>";
echo "</tr></td>";
}
?>
</table>
<br>

<h2> User Guilds Response :</h2>
<div class="response-block">
<p> <?php echo json_encode($_SESSION['guilds']); ?></p>
Expand Down
18 changes: 17 additions & 1 deletion discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
$GLOBALS['base_url'] = "https://discord.com";

# Setting bot token for related requests
$GLOBALS['bot_token'] = null;
require "../config.php";
$GLOBALS['bot_token'] = $bot_token;

# A function to generate a random string to be used as state | (protection against CSRF)
function gen_state()
Expand Down Expand Up @@ -55,6 +56,21 @@ function init($redirect_url, $client_id, $client_secret, $bot_token = null)
$_SESSION['access_token'] = $results['access_token'];
}

# A function to get the channels in a guild | (requires bot token)
function get_channels($guild_id)
{
$url = $GLOBALS['base_url'] . "/api/guilds/$guild_id/channels";
$headers = array('Content-Type: application/x-www-form-urlencoded', 'Authorization: Bot ' . $GLOBALS['bot_token']);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
curl_close($curl);
$results = json_decode($response, true);
return $results;
}

# A function to get user information | (identify scope)
function get_user($email = null)
{
Expand Down