-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.php
More file actions
393 lines (351 loc) · 17.5 KB
/
bot.php
File metadata and controls
393 lines (351 loc) · 17.5 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
declare(strict_types=1);
/*
* This file is a part of the DiscordPHP-MTG project.
*
* Copyright (c) 2025-present Valithor Obsidion <valithor@discordphp.org>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE.md file.
*/
namespace MTG;
//use Clue\React\Redis\Factory as Redis;
use Discord\Builders\CommandBuilder;
use Discord\Builders\Components\ActionRow;
use Discord\Builders\Components\Button;
use Discord\Builders\Components\Separator;
//use Discord\Helpers\CacheConfig;
use Discord\Helpers\ExCollectionInterface;
use Discord\Parts\Channel\Channel;
use Discord\Parts\Interactions\Command\Command;
use Discord\Parts\Interactions\Command\Option;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\OAuth\Application;
use Discord\Parts\User\User;
use Discord\Repository\EmojiRepository;
use Discord\Repository\Interaction\GlobalCommandRepository;
use Discord\WebSockets\Event;
use Discord\WebSockets\Intents;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;
use MTG\Parts\Card;
use React\EventLoop\Loop;
use function React\Async\async;
use function React\Promise\set_rejection_handler;
$technician_id = getenv('technician_id') ?: '116927250145869826'; // Default to Valithor Obsidion's ID
ini_set('zend.assertions', '1'); // Enable assertions for development
define('MTGCARDINFOBOT_START', microtime(true));
ini_set('display_errors', 1);
error_reporting(E_ALL);
set_time_limit(0);
ignore_user_abort(true);
ini_set('max_execution_time', 0);
ini_set('memory_limit', '-1'); // Unlimited memory usage
define('MAIN_INCLUDED', 1); // Token and SQL credential files may be protected locally and require this to be defined to access
$autoload_path = file_exists($autoload_path = __DIR__.'/vendor/autoload.php') ? $autoload_path
: (file_exists($autoload_path = dirname(__DIR__).'/vendor/autoload.php') ? $autoload_path
: (file_exists($autoload_path = realpath(__DIR__.'/../vendor/autoload.php')) ? $autoload_path
: (file_exists($autoload_path = realpath(__DIR__.'/../../vendor/autoload.php')) ? $autoload_path
: (file_exists($autoload_path = realpath(dirname(__DIR__).'/../vendor/autoload.php')) ? $autoload_path
: (
file_exists($autoload_path = realpath(dirname(__DIR__).'/../../vendor/autoload.php')) ? $autoload_path
: null
)))));
$autoload_path ? require ($autoload_path) : throw new \Exception('Composer autoloader not found. Run `composer update` and try again.');
function loadEnv(string $filePath): void
{
if (! file_exists($filePath)) {
throw new \Exception('The .env file does not exist.');
}
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$trimmedLines = array_map('trim', $lines);
$filteredLines = array_filter($trimmedLines, fn ($line) => $line && ! str_starts_with($line, '#'));
array_walk($filteredLines, function ($line) {
[$name, $value] = array_map('trim', explode('=', $line, 2));
if (! array_key_exists($name, $_ENV)) {
putenv(sprintf('%s=%s', $name, $value));
}
});
}
$env_path = file_exists($env_path = getcwd().'/.env') ? $env_path
: (file_exists($env_path = dirname(getcwd()).'/.env') ? $env_path
: (file_exists($env_path = realpath(getcwd().'/../.env') ?: '') ? $env_path
: (file_exists($env_path = realpath(getcwd().'/../../.env') ?: '') ? $env_path
: (file_exists($env_path = realpath(dirname(getcwd()).'/../.env') ?: '') ? $env_path
: (
file_exists($env_path = realpath(dirname(getcwd()).'/../../.env') ?: '') ? $env_path
: null
)))));
$env_path ? loadEnv($env_path) : throw new \Exception('The .env file does not exist. Please create one in the root directory.');
$streamHandler = new StreamHandler('php://stdout', Level::Debug);
$streamHandler->setFormatter(new LineFormatter(null, null, true, true, true));
$logger = new Logger('MTGCARDINFOBOT', [$streamHandler]);
//file_put_contents('output.log', ''); // Clear the contents of 'output.log'
//$logger->pushHandler(new StreamHandler('output.log', Level::Debug));
$logger->debug('Loading configurations for the bot...');
set_rejection_handler(function (\Throwable $e) use ($logger): void {
//if ($e->getMessage() === 'Cannot resume a fiber that is not suspended') return;
$logger->warning("Unhandled Promise Rejection: {$e->getMessage()} [{$e->getFile()}:{$e->getLine()}] ".str_replace('#', '\n#', $e->getTraceAsString()));
});
$mtg = new MTG([
'logger' => $logger,
'socket_options' => [
'dns' => '8.8.8.8',
],
'token' => getenv('TOKEN'),
'storeMessages' => true, // Only needed if messages need to be stored in the cache
'intents' => Intents::getDefaultIntents() /*| Intents::GUILD_MEMBERS | Intents::GUILD_PRESENCES | Intents::MESSAGE_CONTENT*/,
'useTransportCompression' => false, // Disable zlib-stream
'usePayloadCompression' => true, // RFC1950 2.2
//'disabledEvents' => [Event::GUILD_CREATE],
//'loadAllMembers' => true,
/*
'cache' => new CacheConfig(
$interface = new RedisCache(
(new Redis(Loop::get()))->createLazyClient('127.0.0.1:6379'),
'dphp:cache:
'),
$compress = true, // Enable compression if desired
$sweep = false // Disable automatic cache sweeping if desired
),
*/
//'collection' => \MTG\Helpers\Collection::class,
]);
$logger->debug('Bot instance created successfully.');
$webapi = null;
$socket = null;
$global_error_handler = async(function (int $errno, string $errstr, ?string $errfile, ?int $errline) use (&$mtg, &$logger, &$technician_id) {
if (! $mtg instanceof MTG) {
return;
}
$logger->error($msg = sprintf("[%d] Fatal error on `%s:%d`: %s\nBacktrace:\n```\n%s\n```", $errno, $errfile, $errline, $errstr, implode("\n", array_map(fn ($trace) => ($trace['file'] ?? '').':'.($trace['line'] ?? '').($trace['function'] ?? ''), debug_backtrace()))));
if (getenv('TESTING')) {
return;
}
$promise = $mtg->users->fetch($technician_id);
$promise = $promise->then(fn (User $user) => $user->getPrivateChannel());
$promise = $promise->then(fn (Channel $channel) => $channel->sendMessage(MTG::createBuilder()->setContent($msg)));
});
set_error_handler($global_error_handler);
use React\Socket\SocketServer;
use React\Http\HttpServer;
use React\Http\Message\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Promise\PromiseInterface;
//use React\Sh\Shell;
$socket = new SocketServer(
sprintf('%s:%s', '0.0.0.0', getenv('http_port') ?: 55555),
[
'tcp' => [
'so_reuseport' => true,
],
],
Loop::get()
);
/**
* Handles the HTTP request using the HttpServiceManager.
*
* @param ServerRequestInterface $request The HTTP request object.
* @return Response The HTTP response object.
*/
$webapi = new HttpServer(Loop::get(), async(
fn (ServerRequestInterface $request): Response =>
/** @var ?MTG $mtg */
($mtg instanceof MTG)
? new Response(Response::STATUS_IM_A_TEAPOT, ['Content-Type' => 'text/plain'], 'Service Not Yet Implemented')
: new Response(Response::STATUS_SERVICE_UNAVAILABLE, ['Content-Type' => 'text/plain'], 'Service Unavailable')
));
/**
* This code snippet handles the error event of the web API.
* It logs the error message, file, line, and trace, and handles specific error cases.
* If the error message starts with 'Received request with invalid protocol version', it is ignored.
* If the error message starts with 'The response callback', it triggers a restart process.
* The restart process includes sending a message to a specific Discord channel and closing the socket connection.
* After a delay of 5 seconds, the script is restarted by calling the 'restart' function and closing the Discord connection.
*
* @param \Exception $e The \exception object representing the error.
* @param RequestInterface|null $request The HTTP request object associated with the error, if available.
* @param object $mtg The main object of the application.
* @param object $socket The socket object.
* @param bool $testing Flag indicating if the script is running in testing mode.
*/
$webapi->on('error', async(function (\Exception $e, ?RequestInterface $request = null) use (&$mtg, &$logger, &$socket, $technician_id) {
if (str_starts_with($e->getMessage(), 'Received request with invalid protocol version')) {
return;
}
$logger->warning("[WEBAPI] {$e->getMessage()} [{$e->getFile()}:{$e->getLine()}] ".str_replace('\n', PHP_EOL, $e->getTraceAsString()));
if ($request) {
$logger->error('[WEBAPI] Request: '.preg_replace('/(?<=key=)[^&]+/', '********', $request->getRequestTarget()));
}
if (! str_starts_with($e->getMessage(), 'The response callback')) {
return;
}
$logger->error('[WEBAPI] ERROR - RESTART');
if (! $mtg instanceof MTG) {
return;
}
$socket->close();
if (getenv('TESTING')) {
return;
}
$promise = $mtg->users->fetch($technician_id);
$promise = $promise->then(fn (User $user) => $user->getPrivateChannel());
$promise = $promise->then(fn (Channel $channel) => $channel->sendMessage(MTG::createBuilder()->setContent('Restarting due to error in HttpServer API...')));
}));
$func = function (MTG $mtg) {
$mtg->emojis->freshen()
->then(fn (EmojiRepository $emojis) => $mtg->application->commands->freshen())
->then(function (GlobalCommandRepository $commands) use ($mtg): void {
if ($names = array_map(fn ($command) => $command->name, iterator_to_array($commands))) {
$mtg->logger->debug('[GLOBAL APPLICATION COMMAND LIST] `'.implode('`, `', $names).'`');
}
$name = 'card_search';
$mtg->listenCommand(
$name,
fn (Interaction $interaction) => $interaction->acknowledgeWithResponse(true)
->then(fn () => $mtg->cards->getCards(array_map(fn ($option) => $option->value, $interaction->data->options->jsonSerialize())))
->then(function (ExCollectionInterface $cards) use ($mtg, $interaction): PromiseInterface {
$builder = MTG::createBuilder();
if (! $card = $cards->first()) {
return $interaction->updateOriginalResponse($builder->setContent('No card found matching the search criteria.'));
}
/** @var Card $card */
if (! $container = $card->toContainer($interaction)) {
return $interaction->updateOriginalResponse($builder->setContent('A card was found, but it is not supported for display.')->addFileFromContent('card.json', json_encode($card, JSON_PRETTY_PRINT)));
}
if ($ci = (! is_array($card_ci = $card->colorIdentity)
? $mtg->colorIdentityToInteger(null)
: ((count($card_ci) === 1)
? $mtg->colorIdentityToInteger($card_ci[0])
: null))
) {
$container->setAccentColor($ci);
}
$buttons = [$card->getJsonButton($interaction)];
if ($view_image_button = $card->getViewImageButton($interaction)) {
$buttons[] = $view_image_button;
}
if ($legalities_button = $card->getLegalitiesButton($interaction)) {
$buttons[] = $legalities_button;
}
if ($rulings_button = $card->getRulingsButton($interaction)) {
$buttons[] = $rulings_button;
}
if ($foreign_button = $card->getForeignNamesButton($interaction)) {
$buttons[] = $foreign_button;
}
return $interaction->updateOriginalResponse(
$builder->addComponent(
$container->addComponents([
Separator::new(),
ActionRow::new()->addComponents($buttons),
Separator::new(),
Button::link(MTG::GITHUB)->setLabel('GitHub'),
])
)
);
})
);
if (! $command = $commands->get('name', $name)) {
$mtg->logger->debug("[GLOBAL APPLICATION COMMAND] Creating `$name` command...");
$option_name = $mtg->getFactory()->part(Option::class);
/** @var Option $option_name */
$option_name
->setName('name')
->setDescription('nissa, worldwaker|jace|ajani, caller.')
->setType(Option::STRING);
$option_cmc = $mtg->getFactory()->part(Option::class);
/** @var Option $option_cmc */
$option_cmc
->setName('cmc')
->setDescription('Converted mana cost.')
->setType(Option::INTEGER);
$option_colorIdentity = $mtg->getFactory()->part(Option::class);
/** @var Option $option_colorIdentity */
$option_colorIdentity
->setName('color_identity')
->setDescription('W, U, B, R, G.')
->setType(Option::STRING);
$option_types = $mtg->getFactory()->part(Option::class);
/** @var Option $option_types */
$option_types
->setName('types')
->setDescription('Creature, Instant, Enchantment.')
->setType(Option::STRING);
$options_subtypes = $mtg->getFactory()->part(Option::class);
/** @var Option $options_subtypes */
$options_subtypes
->setName('subtypes')
->setDescription('Elf, Goblin, Dragon.')
->setType(Option::STRING);
$options_gameFormat = $mtg->getFactory()->part(Option::class);
/** @var Option $options_gameFormat */
$options_gameFormat
->setName('game_format')
->setDescription('Standard, Modern, Legacy, Vintage, Commander.')
->setType(Option::STRING);
$options_contains = $mtg->getFactory()->part(Option::class);
/** @var Option $options_contains */
$options_contains
->setName('contains')
->setDescription('Filter cards based on whether or not they have a specific field available (like imageUrl).')
->setType(Option::STRING);
$options_multiverseid = $mtg->getFactory()->part(Option::class);
/** @var Option $options_multiverseid */
$options_multiverseid
->setName('multiverseid')
->setDescription('The multiverse ID of the card.')
->setType(Option::INTEGER);
$options_legality = $mtg->getFactory()->part(Option::class);
/** @var Option $options_legality */
$options_legality
->setName('legality')
->setDescription('Legal, Banned or Restricted.')
->setType(Option::STRING);
$builder = CommandBuilder::new()
->setName($name)
->setType(Command::CHAT_INPUT)
->setDescription('Search for a card. See docs.magicthegathering.io/#api_v1cards_list for details.')
->setContext([Interaction::CONTEXT_TYPE_GUILD, Interaction::CONTEXT_TYPE_BOT_DM, Interaction::CONTEXT_TYPE_PRIVATE_CHANNEL])
->addIntegrationType(Application::INTEGRATION_TYPE_GUILD_INSTALL)
->addIntegrationType(Application::INTEGRATION_TYPE_USER_INSTALL)
->addOption($option_name)
->addOption($option_cmc)
->addOption($option_colorIdentity)
->addOption($option_types)
->addOption($options_subtypes)
->addOption($options_gameFormat)
->addOption($options_contains)
->addOption($options_multiverseid)
->addOption($options_legality);
//$mtg->logger->debug($name, ['command builder ' . $builder::class => json_encode($builder, JSON_PRETTY_PRINT)]);
$builder->create($commands)->save('card_search initial creation');
}
//$mtg->logger->debug($name, ['command ' . $command::class => json_encode($command, JSON_PRETTY_PRINT)]);
//$commands->delete($command);
});
};
$init_called = false;
$application_init_called = false;
$mtg->once('init', function (MTG $mtg) use (&$init_called, &$application_init_called, &$func) {
$init_called = true;
if (! $application_init_called) {
return;
}
$func($mtg);
unset($func, $init_called, $application_init_called);
});
$mtg->once('application-init', function (MTG $mtg) use (&$init_called, &$application_init_called, &$func) {
$application_init_called = true;
if (! $init_called) {
return;
}
$func($mtg);
unset($func, $init_called, $application_init_called);
});
//composer$shell = new Shell();
//$shell->setScope(['mtg' => $mtg]);
$mtg->run();