-
Notifications
You must be signed in to change notification settings - Fork 3
/
getusers.php
100 lines (83 loc) · 3.28 KB
/
getusers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
use JetBrains\PhpStorm\ArrayShape;
function dateCompare() : bool {
$json = file_get_contents('cache.json');
$decoded = json_decode($json);
if ($decoded->day == date("d")) {
echo "<script> console.info('Cache is up to date and being loaded. No further action required.') </script>";
$result = true;
} else {
echo "<script> console.warn('Cache is outdated. Gathering new data and refreshing cache shortly..') </script>";
$result = false;
}
return $result;
}
# If you do not use PhpStorm, please remove the "#[ArrayShape(["name" => "string", "avatar" => "string"])] " part.
#[ArrayShape(["name" => "string", "avatar" => "string"])] function getData(int $id) : array {
$avatar = "https://cdn.discordapp.com/avatars/1021102853915422770/eb255d51dba029547e0fdefeaf6efebe?size=2048";
$username = "API Failure";
if (!file_exists('assets/scripts/keys.php')) {
echo "<script> console.error('Could not fetch any data as no key file was provided on the server with the necessary discord API key..') </script>";
} else {
$key = include 'assets/scripts/keys.php';
$json_options = [
"http" => [
"method" => "GET",
"header" => "Authorization: Bot $key" # This requires an additional PHP file, please make a keys.php file in assets/scripts/ and return your discord bot token
]
];
$url = 'https://discordapp.com/api/users/';
$url .= $id;
$json_context = stream_context_create($json_options);
$json_get = file_get_contents($url, false, $json_context);
$json_decode = json_decode($json_get, true);
if (!$json_decode['id']) {
echo "<script> console.error('API callback failed. Key might be expired or not provided correctly.') </script>";
} else {
$username = $json_decode['username'];
$username .= "#";
$username .= $json_decode['discriminator'];
if ($json_decode['avatar']) {
$avatar = 'https://cdn.discordapp.com/avatars/'.$id.'/'.$json_decode['avatar'].'?size=2048';
}
}
}
return array(
"name" => $username,
"avatar" => $avatar
);
}
function upDate() : string {
if (getUser(682609654504882186, true) == "API failure") {
$date = "00";
} else {
$date = date("d");
}
return $date;
}
if (!dateCompare()) {
$array = array(
"day" => upDate(),
"team" => array(
"zen" => getData(682609654504882186),
"storm" => getData(1052129761285132419),
"sith" => getData(572058099507265556),
"duck" => getData(257111149370146817)
)
);
$json = json_encode($array);
file_put_contents("cache.json", json_encode([]));
file_put_contents('cache.json', $json);
echo "<script> console.info('Cache has been updated successfully.') </script>";
}
function getUser(string $user, bool $name) {
$json = file_get_contents('cache.json');
$decoding = json_decode($json);
if ($name) {
$returned = $decoding->team->$user->name;
echo "<script> console.info('Retrieving user data for $returned') </script>";
} else {
$returned = $decoding->team->$user->avatar;
}
return $returned;
}