-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathss13_functions.php
328 lines (311 loc) · 22.6 KB
/
ss13_functions.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
$discord2ckey = function ($tutelar, $id)
{
if (!$browser_post = $tutelar->functions['misc']['browser_post']) return;
if (!$result = $browser_post($tutelar, 'http://civ13.valzargaming.com/discord2ckey/', ['Content-Type' => 'application/x-www-form-urlencoded'], ['discord' => $id], true)) return "<@$id> is either not registered to any ckey or the server did not return a response";
if (is_array($result)) $result = json_decode(json_encode($result), true); //curl returns string
elseif (is_string($result)) $result = json_decode($result); //$browser->post returns React\Promise\Promise
$response = null;
if (is_object($result) && !str_contains(get_class($result), 'React\Promise')) { //json_decoded object
if ($result = $result->ckey) $response = "<@$id> is registered to $result";
else $response = "<@$id> is not registered to any ckey";
}
if (is_array($result)) { //json_decoded array
if ($result = $result['ckey']) $response = "<@$id> is registered to ckey $result";
else $response = "<@$id> is not registered to any ckey";
}
if (is_string($result)) {
if ($result) $response = "<@$id> is registered to $result";
else $response = "<@$id> is not registered to any ckey";
}
//React\Promise\Promise from $browser->post
return $response ?? $result->then(function ($response) use ($tutelar, $id) {
$result = json_decode((string)$response->getBody(), true);
if ($ckey = $result['ckey']) return "<@$id> is registered to ckey $ckey";
return "<@$id> is not registered to any ckey";
}, function (Exception $e) use ($tutelar) {
$tutelar->logger->warning('BROWSER POST error: ' . $e->getMessage());
});
};
$ss13_slash_init = function (\Tutelar\Tutelar $tutelar, $commands) use ($discord2ckey)
{
if ($command = $commands->get('name', 'ckey')) $commands->delete($command->id);
$tutelar->discord->guilds->get('id', '468979034571931648')->commands->freshen()->then( function ($commands) use ($tutelar) {
//if ($command = $commands->get('name', 'ckey')) $commands->delete($command->id);
if (! $commands->get('name', 'ckey')) {
$command = new \Discord\Parts\Interactions\Command\Command($tutelar->discord, [
'type' => \Discord\Parts\Interactions\Command\Command::USER,
'name' => 'ckey',
'dm_permission' => false,
'default_member_permissions' => (string) new \Discord\Parts\Permissions\RolePermission($tutelar->discord, ['moderate_members' => true]),
]);
$commands->save($command);
}
});
/* Blue Colony
$tutelar->discord->guilds->get('id', '807759102624792576')->commands->freshen()->then( function ($commands) use ($tutelar) {
//if ($command = $commands->get('name', 'ckey')) $commands->delete($command->id);
if (! $commands->get('name', 'ckey')) {
$command = new \Discord\Parts\Interactions\Command\Command($tutelar->discord, [
'type' => \Discord\Parts\Interactions\Command\Command::USER,
'name' => 'ckey',
'dm_permission' => false,
'default_member_permissions' => (string) new \Discord\Parts\Permissions\RolePermission($tutelar->discord, ['moderate_members' => true]),
]);
$commands->save($command);
}
});*/
$tutelar->discord->listenCommand('ckey', function ($interaction) use ($tutelar, $discord2ckey) {
if (!$response = $discord2ckey($tutelar, $interaction->data->target_id)) return $interaction->respondWithMessage(\Discord\Builders\MessageBuilder::new()->setContent('There was an error retrieving data'));
if ($response instanceof \React\Promise\Promise) return $response->then( function ($response) use ($interaction) { $interaction->respondWithMessage(\Discord\Builders\MessageBuilder::new()->setContent($response), true); });
$interaction->respondWithMessage(\Discord\Builders\MessageBuilder::new()->setContent($response), true);
});
};
$ss13_debug_guild_message = function (\Tutelar\Tutelar $tutelar, $message, string $message_content, string $message_content_lower)
{
//
};
$ss13_owner_message = function (\Tutelar\Tutelar $tutelar, $message, string $message_content, string $message_content_lower)
{
//
};
$ss13_manager_message = function (\Tutelar\Tutelar $tutelar, $message, string $message_content, string $message_content_lower)
{
if (!str_starts_with($message_content_lower, 'ss13')) return;
$message_content = trim(substr($message_content, strlen('ss13')));
$message_content_lower = strtolower($message_content);
if (str_starts_with($message_content_lower, 'setup')) {
$message_content = trim(substr($message_content, strlen('setup')));
$message_content_lower = strtolower($message_content);
if (str_starts_with($message_content_lower, 'channel')) {
$message_content = trim(substr($message_content, strlen('channel')));
$message_content_lower = strtolower($message_content);
preg_match('/<#([0-9]*)>/', $message_content_lower, $matches);
if (!$matches[1] || !$message->guild->channels->get('id', $matches[1])) return $message->reply('Channel not found! Please mention the channel in the format of <#channel_id>');
$message_content_lower = trim(substr($message_content_lower, strlen($matches[0])));
if (!in_array($message_content_lower, ['suggestion_pending', 'suggestion_approved', 'tip_pending', 'tip_approved'])) return $message->reply('Invalid channel configuration `' . $message_content_lower . '`! Valid options are ' . implode(', ', ['suggestion_pending', 'suggestion_approved', 'tip_pending', 'tip_approved']));
$tutelar->discord_config[$message->guild_id]['channels'][$message_content_lower] = $matches[1];
if (!isset($tutelar->suggestions[$message->guild_id]['pending'])) $tutelar->suggestions[$message->guild_id]['pending'] = [];
if (!isset($tutelar->suggestions[$message->guild_id]['approved'])) $tutelar->suggestions[$message->guild_id]['approved'] = [];
if (!isset($tutelar->suggestions[$message->guild_id]['denied'])) $tutelar->suggestions[$message->guild_id]['denied'] = [];
if (!isset($tutelar->tips[$message->guild_id]['pending'])) $tutelar->tips[$message->guild_id]['pending'] = [];
if (!isset($tutelar->tips[$message->guild_id]['approved'])) $tutelar->tips[$message->guild_id]['approved'] = [];
if (!isset($tutelar->tips[$message->guild_id]['denied'])) $tutelar->tips[$message->guild_id]['denied'] = [];
$tutelar->logger->info('Updated ' . $message_content_lower . ' channel id ' . $matches[1]);
$tutelar->saveConfig();
$tutelar->VarSave('suggestions.json', $tutelar->suggestions);
$tutelar->VarSave('tips.json', $tutelar->tips);
return $message->react("👍");;
}
}
};
$ss13_admin_message = function (\Tutelar\Tutelar $tutelar, $message, string $message_content, string $message_content_lower)
{
//
};
$ss13_moderator_message = function (\Tutelar\Tutelar $tutelar, $message, string $message_content, string $message_content_lower)
{
//
};
$ss13_any_message = function (\Tutelar\Tutelar $tutelar, $message, string $message_content, string $message_content_lower)
{
//
};
$ss13_direct_message = function (\Tutelar\Tutelar $tutelar, $message, string $message_content, string $message_content_lower)
{
//
};
$ss13_guild_message = function (\Tutelar\Tutelar $tutelar, $message, string $message_content, string $message_content_lower)
{
//
};
$ss13_any_called_message = function (\Tutelar\Tutelar $tutelar, $message, string $message_content, string $message_content_lower)
{
//
};
$ss13_guild_called_message = function (\Tutelar\Tutelar $tutelar, $message, string $message_content, string $message_content_lower) use ($perm_check, $ss13_debug_guild_message, $ss13_owner_message, $ss13_manager_message, $ss13_admin_message, $ss13_moderator_message)
{
if (str_starts_with($message_content_lower, 'tip')) {
$message_content = trim(substr($message_content, strlen('tip')));
$message_content_lower = strtolower($message_content);
if (!$message_content_lower) return;
if ($tutelar->discord_config[$message->guild_id]['channels']['tip_approved']) {
if (str_starts_with($message_content_lower, 'approve')) {
if (is_int($id = trim(substr($message_content, strlen('approve'))))) {
if (! $perm_check(['administrator', 'manage_server', 'ban_members', 'moderate_members'], $message->member)) return $message->reply('You do not have permission to use this command');
if (! $tip = $tutelar->tips[$id]) return $message->reply('Tip does not exist');
if (! $channel = $tutelar->discord->getChannel($tutelar->discord_config[$message->guild_id]['channels']['tip_approved'])) return $message->reply('Unable to locate tip approved channel');
//return $this->VarSave('tips.json', $this->tips);
}
}
if (str_starts_with($message_content_lower, 'deny')) {
if (is_int($id = trim(substr($message_content, strlen('deny'))))) {
if (! $perm_check(['administrator', 'manage_server', 'ban_members', 'moderate_members'], $message->member)) return $message->reply('You do not have permission to use this command');
if (! $tip = $tutelar->tips[$id]) return $message->reply('Tip does not exist');
//
}
}
}
if ($tutelar->discord_config[$message->guild_id]['channels']['tip_pending']) {
if (! $channel = $tutelar->discord->getChannel($tutelar->discord_config[$message->guild_id]['channels']['tip_pending'])) return $message->reply('Unable to locate tip pending channel!');
//
}
}
if (str_starts_with($message_content_lower, 'suggest')) {
if(str_starts_with($message_content_lower, 'suggestion')) $message_content = trim(substr($message_content, strlen('suggestion')));
else $message_content = trim(substr($message_content, strlen('suggest')));
$message_content_lower = strtolower($message_content);
if (!$message_content_lower) return;
if ($message_content_lower == 'pending') {
if (! $perm_check(['administrator', 'manage_server', 'ban_members', 'moderate_members'], $message->member)) return $message->reply('You do not have permission to use this command');
$builder = new \Discord\Builders\MessageBuilder();
$builder->addFileFromContent('pending.txt', var_export($tutelar->suggestions[$message->guild_id]['pending'], true));
return $message->reply($builder);
}
if ($message_content_lower == 'approved') {
if (! $perm_check(['administrator', 'manage_server', 'ban_members', 'moderate_members'], $message->member)) return $message->reply('You do not have permission to use this command');
$builder = new \Discord\Builders\MessageBuilder();
$builder->addFileFromContent('approved.txt', var_export($tutelar->suggestions[$message->guild_id]['approved'], true));
return $message->reply($builder);
}
if ($message_content_lower == 'denied') {
if (! $perm_check(['administrator', 'manage_server', 'ban_members', 'moderate_members'], $message->member)) return $message->reply('You do not have permission to use this command');
$builder = new \Discord\Builders\MessageBuilder();
$builder->addFileFromContent('denied.txt', var_export($tutelar->suggestions[$message->guild_id]['denied'], true));
return $message->reply($builder);
}
if (str_starts_with($message_content_lower, 'approve')) {
if (! $suggestion = $tutelar->suggestions[$message->guild_id]['pending'][$id = trim(substr($message_content, strlen('approve')))]) return $message->reply('Suggestion does not exist');
if (! $perm_check(['administrator', 'manage_server', 'ban_members', 'moderate_members'], $message->member)) return $message->reply('You do not have permission to use this command');
if (! $channel = $tutelar->discord->getChannel($tutelar->discord_config[$message->guild_id]['channels']['suggestion_approved'])) return $message->reply('Unable to locate suggestion approved channel');
$tutelar->suggestions[$message->guild_id]['approved'][$id] = $suggestion;
unset($tutelar->suggestions[$message->guild_id]['pending'][$id]);
$builder = new \Discord\Builders\MessageBuilder();
$builder->setAllowedMentions(['parse'=>[]]);
$embed = new \Discord\Parts\Embed\Embed($tutelar->discord);
$embed->setTitle('Suggestion ID: ' . $id);
$embed->setDescription('Suggestion submitted by: <@' . $suggestion['user_id'] . '>' . PHP_EOL . 'Suggestion approved by: ' . $message->author);
if ($user = $tutelar->discord->users->get('id', $suggestion['user_id'])) {
$embed->setAuthor($user->displayname, $user->avatar);
$embed->setThumbnail($user->avatar);
}
if (strlen($suggestion['content']) <= 1024) $embed->addFieldValues('Suggestion', $suggestion['content']);
else $builder->setContent($suggestion['content']);
if (isset($tutelar->owner_id) && $owner = $tutelar->discord->users->get('id', $tutelar->owner_id)) $embed->setFooter(($tutelar->github ? "{$tutelar->github}" . PHP_EOL : '') . "{$tutelar->discord->username} by {$owner->displayname}");
$embed->setTimestamp();
$builder->addEmbed($embed);
$channel->sendMessage($builder)->then(function($message){$message->react("👍")->then(function($result)use($message){$message->react("👎");});});
if ($m = $message->guild->channels->get('id' , $tutelar->discord_config[$message->guild_id]['channels']['suggestion_pending'])->messages->get('id', $suggestion['message_id'])) $m->delete();
else $message->guild->channels->get('id', $tutelar->discord_config[$message->guild_id]['channels']['suggestion_pending'])->messages->fetch('id', $suggestion['message_id'])->then(function ($message) {$message->delete();});
$message->react("👍");
return $tutelar->VarSave('suggestions.json', $tutelar->suggestions);
}
if (str_starts_with($message_content_lower, 'deny')) {
if (! $suggestion = $tutelar->suggestions[$message->guild_id]['pending'][$id = trim(substr($message_content, strlen('deny')))]) return $message->reply('Suggestion does not exist');
if (! $perm_check(['administrator', 'manage_server', 'ban_members', 'moderate_members'], $message->member)) return $message->reply('You do not have permission to use this command');
$tutelar->suggestions[$message->guild_id]['denied'][$id] = $suggestion;
unset($tutelar->suggestions[$message->guild_id]['pending'][$id]);
if ($m = $message->guild->channels->get('id' , $tutelar->discord_config[$message->guild_id]['channels']['suggestion_pending'])->messages->get('id', $suggestion['message_id'])) $m->delete();
$message->react("👍");
return $tutelar->VarSave('suggestions.json', $tutelar->suggestions);
}
if ($tutelar->discord_config[$message->guild_id]['channels']['suggestion_pending']) {
if (! $channel = $tutelar->discord->getChannel($tutelar->discord_config[$message->guild_id]['channels']['suggestion_pending'])) return $message->reply('Unable to locate suggestion pending channel!');
$tutelar->suggestions[$message->guild_id]['pending'][$message->id] = [
'user_id' => $message->author->id,
'content' => $message_content,
'message_id' => ''
];
$builder = new \Discord\Builders\MessageBuilder();
$embed = new \Discord\Parts\Embed\Embed($tutelar->discord);
$embed->setAuthor($message->author->displayname, $message->author->avatar);
$embed->setThumbnail($message->author->avatar);
$embed->setTitle('Suggestion ID: ' . $message->id);
$embed->setDescription('Suggestion submitted by: ' . $message->author);
if (isset($tutelar->owner_id) && $owner = $tutelar->discord->users->get('id', $tutelar->owner_id)) $embed->setFooter(($tutelar->github ? "{$tutelar->github}" . PHP_EOL : '') . "{$tutelar->discord->username} by {$owner->displayname}");
$embed->setTimestamp();
if (strlen($message_content) <= 1024) $embed->addFieldValues('Suggestion', $message_content);
else $builder->setContent($message_content);
$builder->addEmbed($embed);
$channel->sendMessage($builder)->then(function($new_message) use ($tutelar, $message) {
$tutelar->suggestions[$message->guild_id]['pending'][$message->id]['message_id'] = $new_message->id;
$tutelar->VarSave('suggestions.json', $tutelar->suggestions);
$new_message->react("👍")->then(function($result)use($new_message){$new_message->react("👎");});
});
return $message->reply("Your suggestion has been submitted for review with ID {$message->id}");
}
}
if (str_starts_with($message_content_lower, 'tests')) {
$message_content = trim(substr($message_content, strlen('tests')));
$message_content_lower = strtolower($message_content);
$tokens = explode(' ', $message_content);
if (!$tokens[0]) {
if (empty($tutelar->tests)) return $message->reply("No tests have been created yet! Try creating one with `tests test_key add {Your Test's Question}`");
return $message->reply('Available tests: `' . implode('`, `', array_keys($tutelar->tests)) . '`');
}
if (! isset($tokens[1]) || (! array_key_exists($test_key = $tokens[0], $tutelar->tests) && $tokens[1] != 'add')) return $message->reply("Test `$test_key` hasn't been created yet! Please add a question first.");
if ($tokens[1] == 'list') return $message->reply(\Discord\Builders\MessageBuilder::new()->addFileFromContent("$test_key.txt", var_export($tutelar->tests[$test_key], true)));
if ($tokens[1] == 'add') {
unset ($tokens[1], $tokens[0]);
$tutelar->tests[$test_key][] = $question = implode(' ', $tokens);
$message->reply("Added question to test $test_key: $question");
return $tutelar->VarSave('tests.json', $tutelar->tests);
}
if ($tokens[1] == 'remove') {
if (! is_numeric($tokens[2])) return $message->replay("Invalid format! Please use the format `tests test_key remove #`");
if (! isset($tutelar->tests[$test_key][$tokens[2]])) return $message->reply("Question not found in test $test_key! Please use the format `tests test_key remove #`");
$message->reply('Removed question ' . $tokens[2] . ': ' . $tutelar->tests[$test_key][$tokens[2]]);
unset($tutelar->tests[$test_key][$tokens[2]]);
return $tutelar->VarSave('tests.json', $tutelar->tests);
}
if ($tokens[1] == 'post') {
if (! is_numeric($tokens[2])) return $message->replay("Invalid format! Please use the format `tests test_key post #`");
if (count($tutelar->tests[$test_key])<$tokens[2]) return $message->replay("Can't return more questions than exist in a test!");
$questions = [];
while (count($questions)<$tokens[2]) if (! in_array($tutelar->tests[$test_key][($rand = array_rand($tutelar->tests[$test_key]))], $questions)) $questions[] = $tutelar->tests[$test_key][$rand];
return $message->reply("$test_key test:" . PHP_EOL . implode(PHP_EOL, $questions));
}
if ($tokens[1] == 'delete') {
$message->reply("Deleted test `$test_key`");
unset($tutelar->tests[$test_key]);
return $tutelar->VarSave('tests.json', $tutelar->tests);
}
}
if ($message->user_id == $tutelar->owner_id) {
$ss13_debug_guild_message($tutelar, $message, $message_content, $message_content_lower);
$ss13_owner_message($tutelar, $message, $message_content, $message_content_lower);
$ss13_manager_message($tutelar, $message, $message_content, $message_content_lower);
$ss13_admin_message($tutelar, $message, $message_content, $message_content_lower);
$ss13_moderator_message($tutelar, $message, $message_content, $message_content_lower);
return;
}
if ($message->guild->owner_id == $message->user_id) $ss13_owner_message($tutelar, $message, $message_content, $message_content_lower);
if ($perm_check(['administrator', 'manage_server'], $message->member)) $ss13_manager_message($tutelar, $message, $message_content, $message_content_lower);
if ($perm_check(['administrator', 'ban_members'], $message->member)) $ss13_admin_message($tutelar, $message, $message_content, $message_content_lower);
if ($perm_check(['administrator', 'moderate_members'], $message->member)) $ss13_moderator_message($tutelar, $message, $message_content, $message_content_lower);
//if (str_starts_with($message_content_lower, 'tip '))
};
$ss13_on_message = function (\Tutelar\Tutelar $tutelar, $message) use ($ss13_any_message, $ss13_direct_message, $ss13_guild_message, $ss13_any_called_message, $ss13_guild_called_message)
{
if (!$message->content) return; //Don't process message without text
$message_content = '';
$message_content_lower = '';
$called = false;
//$tutelar->logger->debug('[MESSAGE] {' . $message->guild_id . '/' . $message->channel_id . '} ' . $message->author->displayname . ': ' . $message->content);
foreach ($tutelar->command_symbol as $symbol) {
if (str_starts_with($message->content, $symbol)) {
$message_content = trim(substr($message->content, strlen($symbol)));
$message_content_lower = strtolower($message_content);
$called = true;
$tutelar->logger->debug($message->guild_id . ' - `' . $message_content . '`');
break;
}
}
//if ($message->guild->owner_id != $tutelar->owner_id) return; //Only process commands from a guild that Valithor owns
$ss13_any_message($tutelar, $message, $message_content, $message_content_lower);
$ss13_direct_message($tutelar, $message, $message_content, $message_content_lower);
$ss13_guild_message($tutelar, $message, $message_content, $message_content_lower);
if (!$called) return;
$ss13_any_called_message($tutelar, $message, $message_content, $message_content_lower);
$ss13_guild_called_message($tutelar, $message, $message_content, $message_content_lower);
};