3 namespace App\DiscordAppCommands;
5 use App\Models\Technique;
6 use Discord\Builders\MessageBuilder;
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;
13 class AlttpTechCommand {
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);
30 public static function listen($parent, Discord $discord) {
31 $discord->listenCommand(
33 function (Interaction $interaction) use ($discord) {
35 ->acknowledgeWithResponse()
36 ->done(function() use ($discord, $interaction) {
38 $lang = Technique::locale2lang($interaction->locale);
40 ::where('type', '=', 'tech')
41 ->where('name', '=', $interaction->data->options['tech']->options['name']->value)
45 'description' => $tech->getTranslatedProperty('short', $lang),
46 'title' => $tech->getTranslatedProperty('title', $lang),
48 'url' => url('/tech/'.rawurlencode($tech->name)),
51 $properties['image'] = [
52 'url' => url($tech->gif),
54 } else if ($tech->image) {
55 $properties['image'] = [
56 'url' => url($tech->image),
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);
70 function (Interaction $interaction) {
71 $lang = Technique::locale2lang($interaction->locale);
74 $phrase = $interaction->data->options['tech']->options['name']->value;
75 } catch (\Exception $e) {
77 $tech = Technique::where('type', '=', 'tech')->where('index', '=', 1)->whereNotNull('name');
78 if (!empty($phrase)) {
80 $tech->where('title', 'LIKE', '%'.$phrase.'%');
81 $tech->orWhere('short', 'LIKE', '%'.$phrase.'%');
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.'%');
93 foreach ($tech->get() as $t) {
95 'name' => $t->getTranslatedProperty('title', $lang),
98 if (count($choices) == 25) break;
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;
106 return strcasecmp($a['name'], $b['name']);
108 return array_slice($choices, 0, 25);