setType(Option::SUB_COMMAND); $command->setName('tech'); $command->setDescription('Lookup tech on alttp.localhorst.tv'); $option = new Option($discord); $option->setType(Option::STRING); $option->setName('name'); $option->setDescription('The name of the tech to show'); $option->setAutoComplete(true); $option->setRequired(true); $command->addOption($option); return $command; } public static function listen($parent, Discord $discord) { $discord->listenCommand( [$parent, 'tech'], function (Interaction $interaction) use ($discord) { $interaction ->acknowledgeWithResponse() ->done(function() use ($discord, $interaction) { try { $lang = Technique::locale2lang($interaction->locale); $tech = Technique ::where('type', '=', 'tech') ->where('name', '=', $interaction->data->options['tech']->options['name']->value) ->firstOrFail(); $properties = [ 'color' => 0x389c38, 'description' => $tech->getTranslatedProperty('short', $lang), 'title' => $tech->getTranslatedProperty('title', $lang), 'type' => 'rich', 'url' => url('/tech/'.rawurlencode($tech->name)), ]; if ($tech->gif) { $properties['image'] = [ 'url' => url($tech->gif), ]; } else if ($tech->image) { $properties['image'] = [ 'url' => url($tech->image), ]; } $embed = new Embed($discord, $properties); $message = MessageBuilder::new(); $message->addEmbed($embed); $interaction->updateOriginalResponse($message); } catch (\Exception $e) { $message = MessageBuilder::new(); $message->setContent('Error: '.$e->getMessage()); $interaction->updateOriginalResponse($message); } }); }, function (Interaction $interaction) { $lang = Technique::locale2lang($interaction->locale); $phrase = ''; try { $phrase = $interaction->data->options['tech']->options['name']->value; } catch (\Exception $e) { } $tech = Technique::where('type', '=', 'tech')->where('index', '=', 1)->whereNotNull('name'); if (!empty($phrase)) { if ($lang == 'en') { $tech->where('title', 'LIKE', '%'.$phrase.'%'); $tech->orWhere('short', 'LIKE', '%'.$phrase.'%'); } else { $tech = $tech->whereHas('translations', function ($query) use ($lang, $phrase) { $query->where('locale', '=', $lang); $query->where(function ($subquery) use ($phrase) { $subquery->where('title', 'LIKE', '%'.$phrase.'%'); $subquery->orWhere('short', 'LIKE', '%'.$phrase.'%'); }); }); } } $choices = []; foreach ($tech->get() as $t) { $choices[] = [ 'name' => $t->getTranslatedProperty('title', $lang), 'value' => $t->name, ]; if (count($choices) == 25) break; } usort($choices, function ($a, $b) use ($phrase) { $a_head = stripos($a['name'], $phrase) === 0; $b_head = stripos($a['name'], $phrase) === 0; if ($a_head != $b_head) { return $a_head ? -1 : 1; } return strcasecmp($a['name'], $b['name']); }); return array_slice($choices, 0, 25); } ); } }