]> git.localhorst.tv Git - alttp.git/blobdiff - app/Console/Commands/DiscordBotCommand.php
sync discord channels
[alttp.git] / app / Console / Commands / DiscordBotCommand.php
index 7d51cb9df4868ec0bc76fcfc22c13d28a66606a0..4ffbe119f440dbf5d308d05283025cb9557bec57 100644 (file)
@@ -2,7 +2,15 @@
 
 namespace App\Console\Commands;
 
+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\WebSockets\Event;
 use Illuminate\Console\Command;
 use Monolog\Handler\StreamHandler;
 use Monolog\Logger;
@@ -37,7 +45,80 @@ class DiscordBotCommand extends Command
                        'logger' => $logger,
                        'token' => config('discord.token'),
                ]);
+               $discord->on(Event::GUILD_CREATE, function (Guild $guild, Discord $discord) {
+                       try {
+                               DiscordGuild::onUpstreamCreate($guild);
+                       } catch (\Exception $e) {
+                               $this->error('guild create: '.$e->getMessage());
+                       }
+               });
+               $discord->on(Event::GUILD_UPDATE, function (Guild $guild, Discord $discord, ?Guild $old) {
+                       try {
+                               DiscordGuild::onUpstreamUpdate($guild);
+                       } catch (\Exception $e) {
+                               $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);
+                       } catch (\Exception $e) {
+                               $this->error('guild role create: '.$e->getMessage());
+                       }
+               });
+               $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discord, ?Role $old) {
+                       try {
+                               DiscordRole::onUpstreamUpdate($role);
+                       } catch (\Exception $e) {
+                               $this->error('guild role update: '.$e->getMessage());
+                       }
+               });
+               $discord->on(Event::GUILD_ROLE_DELETE, function ($role, Discord $discord) {
+                       try {
+                               DiscordRole::onUpstreamDelete($role);
+                       } catch (\Exception $e) {
+                               $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();