]> git.localhorst.tv Git - alttp.git/commitdiff
new discord command
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 24 Aug 2023 19:59:33 +0000 (21:59 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 24 Aug 2023 19:59:59 +0000 (21:59 +0200)
app/Console/Commands/DiscordBotCommand.php
app/DiscordAppCommands/AlttpCommand.php [new file with mode: 0644]
app/DiscordAppCommands/AlttpTechCommand.php [new file with mode: 0644]
app/Models/Technique.php
database/migrations/2023_08_24_194804_add_tech_image.php [new file with mode: 0644]

index 7c0da505a7b1db36748c209a59d81d4fc907927f..18faf8d49bb72fb9a3fd77a68e93d29f1a8eb29d 100644 (file)
@@ -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 (file)
index 0000000..e27767b
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+namespace App\DiscordAppCommands;
+
+use Discord\Builders\CommandBuilder;
+use Discord\Discord;
+
+class AlttpCommand {
+
+       public static function create(Discord $discord) {
+               $command = CommandBuilder::new();
+               $command->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 (file)
index 0000000..d97cfee
--- /dev/null
@@ -0,0 +1,109 @@
+<?php
+
+namespace App\DiscordAppCommands;
+
+use App\Models\Technique;
+use Discord\Builders\MessageBuilder;
+use Discord\Discord;
+use Discord\Parts\Embed\Embed;
+use Discord\Parts\Embed\Footer;
+use Discord\Parts\Interactions\Command\Option;
+use Discord\Parts\Interactions\Interaction;
+
+class AlttpTechCommand {
+
+       public static function create(Discord $discord) {
+               $command = new Option($discord);
+               $command->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);
+                       }
+               );
+       }
+
+}
index 0b25e47b19ded74f56c91a8c65a662c1021e2eae..cc94327389a5d3476939c9a830398ee6fb5c3166 100644 (file)
@@ -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 (file)
index 0000000..a2490ff
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        *
+        * @return void
+        */
+       public function up()
+       {
+               Schema::table('techniques', function(Blueprint $table) {
+                       $table->string('image')->nullable()->default(null);
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        *
+        * @return void
+        */
+       public function down()
+       {
+               Schema::table('techniques', function(Blueprint $table) {
+                       $table->dropColumn('image');
+               });
+       }
+};