]> git.localhorst.tv Git - alttp.git/blob - app/DiscordAppCommands/AlttpTechCommand.php
switch SNES default port
[alttp.git] / app / DiscordAppCommands / AlttpTechCommand.php
1 <?php
2
3 namespace App\DiscordAppCommands;
4
5 use App\Models\Technique;
6 use Discord\Builders\MessageBuilder;
7 use Discord\Discord;
8 use Discord\Parts\Embed\Embed;
9 use Discord\Parts\Embed\Footer;
10 use Discord\Parts\Interactions\Command\Option;
11 use Discord\Parts\Interactions\Interaction;
12
13 class AlttpTechCommand {
14
15         public static function create(Discord $discord) {
16                 $command = new Option($discord);
17                 $command->setType(Option::SUB_COMMAND);
18                 $command->setName('tech');
19                 $command->setDescription('Lookup tech on alttp.localhorst.tv');
20                 $option = new Option($discord);
21                 $option->setType(Option::STRING);
22                 $option->setName('name');
23                 $option->setDescription('The name of the tech to show');
24                 $option->setAutoComplete(true);
25                 $option->setRequired(true);
26                 $command->addOption($option);
27                 return $command;
28         }
29
30         public static function listen($parent, Discord $discord) {
31                 $discord->listenCommand(
32                         [$parent, 'tech'],
33                         function (Interaction $interaction) use ($discord) {
34                                 $interaction
35                                         ->acknowledgeWithResponse()
36                                         ->done(function() use ($discord, $interaction) {
37                                                 try {
38                                                         $lang = Technique::locale2lang($interaction->locale);
39                                                         $tech = Technique
40                                                                 ::where('type', '=', 'tech')
41                                                                 ->where('name', '=', $interaction->data->options['tech']->options['name']->value)
42                                                                 ->firstOrFail();
43                                                         $properties = [
44                                                                 'color' => 0x389c38,
45                                                                 'description' => $tech->getTranslatedProperty('short', $lang),
46                                                                 'title' => $tech->getTranslatedProperty('title', $lang),
47                                                                 'type' => 'rich',
48                                                                 'url' => url('/tech/'.rawurlencode($tech->name)),
49                                                         ];
50                                                         if ($tech->gif) {
51                                                                 $properties['image'] = [
52                                                                         'url' => url($tech->gif),
53                                                                 ];
54                                                         } else if ($tech->image) {
55                                                                 $properties['image'] = [
56                                                                         'url' => url($tech->image),
57                                                                 ];
58                                                         }
59                                                         $embed = new Embed($discord, $properties);
60                                                         $message = MessageBuilder::new();
61                                                         $message->addEmbed($embed);
62                                                         $interaction->updateOriginalResponse($message);
63                                                 } catch (\Exception $e) {
64                                                         $message = MessageBuilder::new();
65                                                         $message->setContent('Error: '.$e->getMessage());
66                                                         $interaction->updateOriginalResponse($message);
67                                                 }
68                                         });
69                         },
70                         function (Interaction $interaction) {
71                                 $lang = Technique::locale2lang($interaction->locale);
72                                 $phrase = '';
73                                 try {
74                                         $phrase = $interaction->data->options['tech']->options['name']->value;
75                                 } catch (\Exception $e) {
76                                 }
77                                 $tech = Technique::where('type', '=', 'tech')->where('index', '=', 1)->whereNotNull('name');
78                                 if (!empty($phrase)) {
79                                         if ($lang == 'en') {
80                                                 $tech->where('title', 'LIKE', '%'.$phrase.'%');
81                                                 $tech->orWhere('short', 'LIKE', '%'.$phrase.'%');
82                                         } else {
83                                                 $tech = $tech->whereHas('translations', function ($query) use ($lang, $phrase) {
84                                                         $query->where('locale', '=', $lang);
85                                                         $query->where(function ($subquery) use ($phrase) {
86                                                                 $subquery->where('title', 'LIKE', '%'.$phrase.'%');
87                                                                 $subquery->orWhere('short', 'LIKE', '%'.$phrase.'%');
88                                                         });
89                                                 });
90                                         }
91                                 }
92                                 $choices = [];
93                                 foreach ($tech->get() as $t) {
94                                         $choices[] = [
95                                                 'name' => $t->getTranslatedProperty('title', $lang),
96                                                 'value' => $t->name,
97                                         ];
98                                         if (count($choices) == 25) break;
99                                 }
100                                 usort($choices, function ($a, $b) use ($phrase) {
101                                         $a_head = stripos($a['name'], $phrase) === 0;
102                                         $b_head = stripos($a['name'], $phrase) === 0;
103                                         if ($a_head != $b_head) {
104                                                 return $a_head ? -1 : 1;
105                                         }
106                                         return strcasecmp($a['name'], $b['name']);
107                                 });
108                                 return array_slice($choices, 0, 25);
109                         }
110                 );
111         }
112
113 }