-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.php
executable file
·186 lines (156 loc) · 7.97 KB
/
bot.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
use Discord\Discord;
use Discord\WebSockets\Event;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Setup Logger
$log = new Logger('voice_tracking_bot');
$log->pushHandler(new StreamHandler(__DIR__ . '/bot.log', Logger::DEBUG));
try {
$discord = new Discord([
'token' => $_ENV['discord_token'],
]);
$mysqli = new mysqli($_ENV['host_db'], $_ENV['username_db'], $_ENV['password_db'], $_ENV['database_db'], $_ENV['port_db']);
if ($mysqli->connect_error) {
throw new Exception("Database connection failed: " . $mysqli->connect_error);
}
$voiceStates = [];
$deafenTimers = [];
$idleTimers = [];
$discord->on('ready', function ($discord) use ($mysqli, &$voiceStates, &$deafenTimers, &$idleTimers, $log) {
$log->info("Bot is ready.");
// Cache roles in the database
try {
$guild = $discord->guilds->first();
$query = "DELETE FROM discord_roles";
$mysqli->query($query);
foreach ($guild->roles as $role) {
$roleName = $mysqli->real_escape_string($role->name);
$query = "INSERT INTO discord_roles (name) VALUES ('$roleName')";
$notAdd = ['@everyone', 'Member', "Development Hub"];
if (!in_array($roleName, $notAdd)) {
$mysqli->query($query);
}
}
} catch (Exception $e) {
$log->error("Failed to cache roles: " . $e->getMessage());
}
$discord->on(Event::VOICE_STATE_UPDATE, function ($voiceState) use ($discord, $mysqli, &$voiceStates, &$deafenTimers, &$idleTimers, $log) {
try {
$userId = $voiceState->user_id;
$newChannelId = $voiceState->channel_id;
$username = $voiceState->user->username;
$currentDate = date('Y-m-d');
$newChannelName = null;
if ($newChannelId) {
$channel = $discord->getChannel($newChannelId);
$newChannelName = $channel ? $channel->name : null;
}
$currentTime = time();
// User left a voice channel
if (!$newChannelId) {
if (isset($voiceStates[$userId])) {
$joinTime = $voiceStates[$userId]['join_time'];
$previousChannelId = $voiceStates[$userId]['channel_id'];
$totalTime = $currentTime - $joinTime;
$query = "UPDATE voice_presence SET total_time = total_time + '$totalTime', closed = 1
WHERE user_id = '$userId' AND date = '$currentDate' AND channel_id = '$previousChannelId' AND closed = 0";
$mysqli->query($query);
unset($voiceStates[$userId]);
unset($idleTimers[$userId]);
unset($deafenTimers[$userId]);
}
return;
}
// User joined a new channel
if (!isset($voiceStates[$userId])) {
$query = "INSERT INTO voice_presence (user_id, date, total_time, channel_id, channel_name, username, closed)
VALUES ('$userId', '$currentDate', 0, '$newChannelId', '$newChannelName', '$username', 0)";
$mysqli->query($query);
$voiceStates[$userId] = [
'channel_id' => $newChannelId,
'join_time' => $currentTime,
'idle_notified' => false,
];
$idleTimers[$userId] = $currentTime;
} elseif ($voiceStates[$userId]['channel_id'] !== $newChannelId) {
// User switched channels
$previousChannelId = $voiceStates[$userId]['channel_id'];
$joinTime = $voiceStates[$userId]['join_time'];
$totalTime = $currentTime - $joinTime;
$query = "UPDATE voice_presence SET total_time = total_time + '$totalTime', closed = 1
WHERE user_id = '$userId' AND date = '$currentDate' AND channel_id = '$previousChannelId' AND closed = 0";
$mysqli->query($query);
$query = "INSERT INTO voice_presence (user_id, date, total_time, channel_id, channel_name, username, closed)
VALUES ('$userId', '$currentDate', 0, '$newChannelId', '$newChannelName', '$username', 0)";
$mysqli->query($query);
$voiceStates[$userId] = [
'channel_id' => $newChannelId,
'join_time' => $currentTime,
'idle_notified' => false,
];
$idleTimers[$userId] = $currentTime;
}
} catch (Exception $e) {
$log->error("Error in VOICE_STATE_UPDATE: " . $e->getMessage());
}
});
$discord->loop->addPeriodicTimer(1, function () use ($discord, $mysqli, &$voiceStates, &$deafenTimers, &$idleTimers, $log) {
try {
$currentTime = time();
$currentDate = date('Y-m-d');
foreach ($deafenTimers as $userId => $startTime) {
if ($currentTime - $startTime >= 2700) {
if (isset($voiceStates[$userId])) {
$guild = $discord->guilds->first();
$member = $guild->members->get('id', $userId);
if ($member) {
$member->moveMember(null);
unset($deafenTimers[$userId]);
}
}
}
}
foreach ($idleTimers as $userId => $startTime) {
if ($currentTime - $startTime >= 600 && !$voiceStates[$userId]['idle_notified']) {
$user = $discord->users->get('id', $userId);
if ($user) {
$user->sendMessage("You are now Idle. You will be disconnected after 10 minutes of being idle!");
}
$voiceStates[$userId]['idle_notified'] = true;
} elseif ($currentTime - $startTime >= 1200) {
if (isset($voiceStates[$userId])) {
$guild = $discord->guilds->first();
$member = $guild->members->get('id', $userId);
if ($member) {
$member->moveMember(null);
$user = $discord->users->get('id', $userId);
if ($user) {
$user->sendMessage("You have been disconnected from the voice channel because you were AFK!");
}
unset($idleTimers[$userId]);
unset($voiceStates[$userId]);
}
}
}
}
foreach ($voiceStates as $userId => &$state) {
$joinTime = $state['join_time'];
$elapsedTime = $currentTime - $joinTime;
$query = "UPDATE voice_presence SET total_time = total_time + '$elapsedTime'
WHERE user_id = '$userId' AND date = '$currentDate' AND closed = 0";
$mysqli->query($query);
$state['join_time'] = $currentTime;
}
} catch (Exception $e) {
$log->error("Error in periodic timer: " . $e->getMessage());
}
});
});
$discord->run();
} catch (Exception $e) {
$log->error("Bot encountered a fatal error: " . $e->getMessage());
}