]> git.localhorst.tv Git - alttp.git/blobdiff - app/Console/Commands/DiscordBotCommand.php
discord bot presence command
[alttp.git] / app / Console / Commands / DiscordBotCommand.php
index 7d9b65e6e358401e2880d4c2a80788a3bec8b6f4..4368bb5b1d4b06d2bc815c74ff35d470cf708d95 100644 (file)
@@ -2,12 +2,16 @@
 
 namespace App\Console\Commands;
 
+use App\Models\DiscordBotCommand as CommandModel;
+use App\Models\DiscordChannel;
 use App\Models\DiscordGuild;
 use App\Models\DiscordRole;
 use Discord\Discord;
+use Discord\Parts\Channel\Channel;
 use Discord\Parts\Channel\Message;
 use Discord\Parts\Guild\Guild;
 use Discord\Parts\Guild\Role;
+use Discord\Parts\User\Activity;
 use Discord\WebSockets\Event;
 use Illuminate\Console\Command;
 use Monolog\Handler\StreamHandler;
@@ -43,6 +47,23 @@ class DiscordBotCommand extends Command
                        'logger' => $logger,
                        'token' => config('discord.token'),
                ]);
+               $discord->on('ready', function (Discord $discord) {
+                       $activity = new Activity($discord);
+                       $activity->type = Activity::TYPE_LISTENING;
+                       $activity->name = 'HolySmoke';
+                       $activity->url = 'https://alttp.localhorst.tv/';
+                       $discord->updatePresence($activity);
+
+                       $discord->getLoop()->addPeriodicTimer(1, function () use ($discord) {
+                               $command = CommandModel::where('status', '=', 'pending')->oldest()->first();
+                               if ($command) {
+                                       try {
+                                               $command->execute($discord);
+                                       } catch (\Exception $e) {
+                                       }
+                               }
+                       });
+               });
                $discord->on(Event::GUILD_CREATE, function (Guild $guild, Discord $discord) {
                        try {
                                DiscordGuild::onUpstreamCreate($guild);
@@ -57,6 +78,27 @@ class DiscordBotCommand extends Command
                                $this->error('guild update: '.$e->getMessage());
                        }
                });
+               $discord->on(Event::CHANNEL_CREATE, function (Channel $channel, Discord $discord) {
+                       try {
+                               DiscordGuild::onUpstreamCreate($channel);
+                       } catch (\Exception $e) {
+                               $this->error('channel create: '.$e->getMessage());
+                       }
+               });
+               $discord->on(Event::CHANNEL_UPDATE, function (Channel $channel, Discord $discord, ?Channel $old) {
+                       try {
+                               DiscordGuild::onUpstreamUpdate($channel);
+                       } catch (\Exception $e) {
+                               $this->error('channel update: '.$e->getMessage());
+                       }
+               });
+               $discord->on(Event::CHANNEL_DELETE, function ($channel, Discord $discord) {
+                       try {
+                               DiscordGuild::onUpstreamDelete($channel);
+                       } catch (\Exception $e) {
+                               $this->error('channel delete: '.$e->getMessage());
+                       }
+               });
                $discord->on(Event::GUILD_ROLE_CREATE, function (Role $role, Discord $discord) {
                        try {
                                DiscordRole::onUpstreamCreate($role);
@@ -64,7 +106,7 @@ class DiscordBotCommand extends Command
                                $this->error('guild role create: '.$e->getMessage());
                        }
                });
-               $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discordi, ?Role $old) {
+               $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discord, ?Role $old) {
                        try {
                                DiscordRole::onUpstreamUpdate($role);
                        } catch (\Exception $e) {
@@ -78,25 +120,6 @@ class DiscordBotCommand extends Command
                                $this->error('guild role delete: '.$e->getMessage());
                        }
                });
-               $discord->on('ready', function (Discord $discord) {
-                       $discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) {
-                               if (!empty($message->guild_id)) return;
-                               if (!empty($message->webhook_id)) return;
-                               if (!empty($message->application_id)) return;
-                               if (is_null($message->author)) return;
-                               if ($message->author->bot) return;
-                               $discord->getLoop()->addTimer(0.6, function() use ($message) {
-                                       $message->react('😄');
-                               });
-                               if (!is_null($message->channel)) {
-                                       $discord->getLoop()->addTimer(2.0, function() use ($message) {
-                                               $message->channel->sendMessage('bugger off');
-                                       });
-                               } else {
-                                       $message->delayedReply('bugger off', 2000);
-                               }
-                       });
-               });
                $discord->getLoop()->addSignal(SIGINT, function() use ($discord) {
                        $discord->close();
                });