-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_donate.php
122 lines (94 loc) · 2.69 KB
/
update_donate.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/*
* Update Discord channel with donations
* (c) 2019 HarpyWar <[email protected]>
*/
require_once __DIR__.'/vendor/autoload.php';
require_once 'config.php';
use RestCord\DiscordClient;
// if file modification time less update_interval seconds
if (file_exists(Config::donate_file) && filemtime(Config::donate_file) + Config::donate_update_interval > time())
{
return;
}
// add donations
$embed = array();
$sum = 0;
try
{
// get players count from the planet
$data = file_get_contents(Config::last_donations_url);
$json = json_decode($data);
$donations = $json->result;
// get total sum
foreach ($donations as $d)
{
$sum += $d->order_sum;
}
// subtract donations from prev years
for ($i = 2019; $i < date("Y"); $i++)
$sum -= Config::donate_max;
if ($sum < 0)
$sum = 0;
$embed['title'] = "DONATE FOR THE NFK SERVER <<";
$embed['url'] = "https://nfk.harpywar.com/donate.php";
$embed['description'] = '';
$percents = round($sum / (Config::donate_max / 100));
$embed['description'] .= "**" . $sum . (($sum > Config::donate_max)
? " ₽**"
: " / " . Config::donate_max . " ₽** (" . $percents . "% for this year" . ")\n");
$embed['description'] .= "\n**Respect these guys**\n";
$embed['description'] .= "---------------------->\n\n";
for ($i = count($donations) - 1; $i >= 0; $i--)
{
$d = $donations[$i];
$embed['description'] .= ":skull_crossbones: **" . $d->order_sum . " ₽** from " . $d->username . "\n";
if ($d->message) {
$embed['description'] .= "```\n" . $d->message . "```\n";
} else {
$embed['description'] .= "\n";
}
}
}
catch (Exception $e) {
echo $e->getMessage();
}
// update sym
file_put_contents(Config::donate_file, $sum);
// get random citate
$idx = rand(0, count(Config::quotes) - 1);
$body = '> ' . Config::quotes[$idx] . "\n\n";
// send update request to discord
$client = new DiscordClient([
'token' => Config::discord_token
]);
try
{
$params = array(
"channel.id" => Config::discord_donate_channel_id
);
// modify channel name
$params['name'] = Config::donate_channel_title;
if ($sum < Config::donate_max)
$params['name'] .= sprintf(Config::donate_channel_title_sum, $sum, Config::donate_max);
$client->channel->modifyChannel($params);
// get channel messages
$messages = $client->channel->getChannelMessages($params);
$params['content'] = $body;
$params['embed'] = $embed;
if (count($messages) > 0)
{
$params['message.id'] = (int)$messages[count($messages) - 1]['id'];
// edit last message
$result = $client->channel->editMessage($params);
}
else
{
// create new message if there is no one
$result = $client->channel->createMessage($params);
}
}
catch (Exception $e)
{
echo "ERROR\n" . $e->getMessage();
}