From: Daniel Karbach Date: Thu, 24 Aug 2023 19:59:33 +0000 (+0200) Subject: new discord command X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=1557083acedbffeae2cec7deaa485fd8dcdbaa49;p=alttp.git new discord command --- diff --git a/app/Console/Commands/DiscordBotCommand.php b/app/Console/Commands/DiscordBotCommand.php index 7c0da50..18faf8d 100644 --- a/app/Console/Commands/DiscordBotCommand.php +++ b/app/Console/Commands/DiscordBotCommand.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\DiscordAppCommands\AlttpCommand; use App\Models\DiscordBotCommand as CommandModel; use App\Models\DiscordChannel; use App\Models\DiscordGuild; @@ -67,10 +68,10 @@ class DiscordBotCommand extends Command }); if (config('discord.enable_commands')) { - //AosrPresetCommand::listen($discord); + AlttpCommand::listen($discord); } if (config('discord.create_commands')) { - //AosrPresetCommand::delete($discord); + AlttpCommand::create($discord); } }); $discord->on(Event::GUILD_CREATE, function (Guild $guild, Discord $discord) { diff --git a/app/DiscordAppCommands/AlttpCommand.php b/app/DiscordAppCommands/AlttpCommand.php new file mode 100644 index 0000000..e27767b --- /dev/null +++ b/app/DiscordAppCommands/AlttpCommand.php @@ -0,0 +1,24 @@ +setName('alttp'); + $command->setDescription('A Link to the Past'); + $command->addOption(AlttpTechCommand::create($discord)); + $discord->application->commands->save( + $discord->application->commands->create($command->toArray()) + ); + } + + public static function listen(Discord $discord) { + AlttpTechCommand::listen('alttp', $discord); + } + +} diff --git a/app/DiscordAppCommands/AlttpTechCommand.php b/app/DiscordAppCommands/AlttpTechCommand.php new file mode 100644 index 0000000..d97cfee --- /dev/null +++ b/app/DiscordAppCommands/AlttpTechCommand.php @@ -0,0 +1,109 @@ +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->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); + } + ); + } + +} diff --git a/app/Models/Technique.php b/app/Models/Technique.php index 0b25e47..cc94327 100644 --- a/app/Models/Technique.php +++ b/app/Models/Technique.php @@ -10,6 +10,11 @@ class Technique extends Model { use HasFactory; + public static function locale2lang($locale) { + if (strtolower(substr($locale, 0, 2)) == 'de') return 'de'; + return 'en'; + } + public function chapters() { return $this ->belongsToMany(Technique::class, 'technique_chapter', 'parent_id', 'child_id') diff --git a/database/migrations/2023_08_24_194804_add_tech_image.php b/database/migrations/2023_08_24_194804_add_tech_image.php new file mode 100644 index 0000000..a2490ff --- /dev/null +++ b/database/migrations/2023_08_24_194804_add_tech_image.php @@ -0,0 +1,32 @@ +string('image')->nullable()->default(null); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('techniques', function(Blueprint $table) { + $table->dropColumn('image'); + }); + } +};