Skip to content

Instantly share code, notes, and snippets.

@webuti
Last active November 26, 2020 13:35
Show Gist options
  • Save webuti/812a485ad1bcaf13f04b81513fa7446f to your computer and use it in GitHub Desktop.
Save webuti/812a485ad1bcaf13f04b81513fa7446f to your computer and use it in GitHub Desktop.
Discord basic send message and member list
<?php
class Discord
{
protected $token = "";
protected $guidId = "";
protected $endpoint = "https://discord.com/api/v8";
public function post($query, $data = null)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->endpoint . $query,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Bot " . $this->token,
'Content-Type:application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response);
}
public function get($query)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->endpoint . $query,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bot " . $this->token,
),
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response);
}
public function getMemberList()
{
return $this->get("/guilds/" . $this->guidId . "/members?limit=50");
}
public function getUserDms($recipientId)
{
return $this->post("/users/@me/channels", ['recipient_id' => $recipientId]);
}
public function sendMessage($recipientId, $content)
{
$dm = $this->getUserDms($recipientId);
return $this->post("/channels/" . $dm->id . "/messages", ["content" => $content]);
}
public function getMemberArray()
{
$members = $this->getMemberList();
$users = [];
foreach ($members as $mem) {
$users[$mem->user->id] = $mem->user->username . "#" . $mem->user->discriminator;
}
return $users;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment