]> git.localhorst.tv Git - alttp.git/blob - app/DiscordAppCommands/AlttpTechCommand.php
d97cfee544a78ca93254d5970230bb06ec43588a
[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->image) {
51                                                                 $properties['image'] = [
52                                                                         'url' => url($tech->image),
53                                                                 ];
54                                                         }
55                                                         $embed = new Embed($discord, $properties);
56                                                         $message = MessageBuilder::new();
57                                                         $message->addEmbed($embed);
58                                                         $interaction->updateOriginalResponse($message);
59                                                 } catch (\Exception $e) {
60                                                         $message = MessageBuilder::new();
61                                                         $message->setContent('Error: '.$e->getMessage());
62                                                         $interaction->updateOriginalResponse($message);
63                                                 }
64                                         });
65                         },
66                         function (Interaction $interaction) {
67                                 $lang = Technique::locale2lang($interaction->locale);
68                                 $phrase = '';
69                                 try {
70                                         $phrase = $interaction->data->options['tech']->options['name']->value;
71                                 } catch (\Exception $e) {
72                                 }
73                                 $tech = Technique::where('type', '=', 'tech')->where('index', '=', 1)->whereNotNull('name');
74                                 if (!empty($phrase)) {
75                                         if ($lang == 'en') {
76                                                 $tech->where('title', 'LIKE', '%'.$phrase.'%');
77                                                 $tech->orWhere('short', 'LIKE', '%'.$phrase.'%');
78                                         } else {
79                                                 $tech = $tech->whereHas('translations', function ($query) use ($lang, $phrase) {
80                                                         $query->where('locale', '=', $lang);
81                                                         $query->where(function ($subquery) use ($phrase) {
82                                                                 $subquery->where('title', 'LIKE', '%'.$phrase.'%');
83                                                                 $subquery->orWhere('short', 'LIKE', '%'.$phrase.'%');
84                                                         });
85                                                 });
86                                         }
87                                 }
88                                 $choices = [];
89                                 foreach ($tech->get() as $t) {
90                                         $choices[] = [
91                                                 'name' => $t->getTranslatedProperty('title', $lang),
92                                                 'value' => $t->name,
93                                         ];
94                                         if (count($choices) == 25) break;
95                                 }
96                                 usort($choices, function ($a, $b) use ($phrase) {
97                                         $a_head = stripos($a['name'], $phrase) === 0;
98                                         $b_head = stripos($a['name'], $phrase) === 0;
99                                         if ($a_head != $b_head) {
100                                                 return $a_head ? -1 : 1;
101                                         }
102                                         return strcasecmp($a['name'], $b['name']);
103                                 });
104                                 return array_slice($choices, 0, 25);
105                         }
106                 );
107         }
108
109 }