]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/DiscordBotCommand.php
sync ladder schedule
[alttp.git] / app / Console / Commands / DiscordBotCommand.php
1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\DiscordAppCommands\AlttpCommand;
6 use App\Models\DiscordBotCommand as CommandModel;
7 use App\Models\DiscordChannel;
8 use App\Models\DiscordGuild;
9 use App\Models\DiscordRole;
10 use Discord\Discord;
11 use Discord\Parts\Channel\Channel;
12 use Discord\Parts\Channel\Message;
13 use Discord\Parts\Guild\Guild;
14 use Discord\Parts\Guild\Role;
15 use Discord\Parts\User\Activity;
16 use Discord\WebSockets\Event;
17 use Discord\WebSockets\Intents;
18 use Illuminate\Console\Command;
19 use Monolog\Handler\StreamHandler;
20 use Monolog\Logger;
21 use React\EventLoop\Loop;
22
23 class DiscordBotCommand extends Command
24 {
25         /**
26          * The name and signature of the console command.
27          *
28          * @var string
29          */
30         protected $signature = 'discord:bot';
31
32         /**
33          * The console command description.
34          *
35          * @var string
36          */
37         protected $description = 'Runs the discord bot';
38
39         /**
40          * Execute the console command.
41          *
42          * @return int
43          */
44         public function handle()
45         {
46                 $logger = new Logger('DiscordBot');
47                 $logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO));
48                 $discord = new Discord([
49                         'intents' => Intents::getDefaultIntents() | Intents::GUILD_MEMBERS,
50                         'logger' => $logger,
51                         'token' => config('discord.token'),
52                 ]);
53                 $discord->on('ready', function (Discord $discord) {
54                         $activity = new Activity($discord);
55                         $activity->type = Activity::TYPE_LISTENING;
56                         $activity->name = 'HolySmoke';
57                         $activity->url = 'https://alttp.localhorst.tv/';
58                         $discord->updatePresence($activity);
59
60                         $discord->getLoop()->addPeriodicTimer(1, function () use ($discord) {
61                                 $command = CommandModel::where('status', '=', 'pending')->oldest()->first();
62                                 if ($command) {
63                                         try {
64                                                 $command->execute($discord);
65                                         } catch (\Exception $e) {
66                                         }
67                                 }
68                         });
69
70                         if (config('discord.enable_commands')) {
71                                 AlttpCommand::listen($discord);
72                         }
73                         if (config('discord.create_commands')) {
74                                 AlttpCommand::create($discord);
75                         }
76                 });
77                 $discord->on(Event::GUILD_CREATE, function (Guild $guild, Discord $discord) {
78                         try {
79                                 DiscordGuild::onUpstreamCreate($guild);
80                         } catch (\Exception $e) {
81                                 $this->error('guild create: '.$e->getMessage());
82                         }
83                 });
84                 $discord->on(Event::GUILD_UPDATE, function (Guild $guild, Discord $discord, ?Guild $old) {
85                         try {
86                                 DiscordGuild::onUpstreamUpdate($guild);
87                         } catch (\Exception $e) {
88                                 $this->error('guild update: '.$e->getMessage());
89                         }
90                 });
91                 $discord->on(Event::CHANNEL_CREATE, function (Channel $channel, Discord $discord) {
92                         try {
93                                 DiscordChannel::onUpstreamCreate($channel);
94                         } catch (\Exception $e) {
95                                 $this->error('channel create: '.$e->getMessage());
96                         }
97                 });
98                 $discord->on(Event::CHANNEL_UPDATE, function (Channel $channel, Discord $discord, ?Channel $old) {
99                         try {
100                                 DiscordChannel::onUpstreamUpdate($channel);
101                         } catch (\Exception $e) {
102                                 $this->error('channel update: '.$e->getMessage());
103                         }
104                 });
105                 $discord->on(Event::CHANNEL_DELETE, function ($channel, Discord $discord) {
106                         try {
107                                 DiscordChannel::onUpstreamDelete($channel);
108                         } catch (\Exception $e) {
109                                 $this->error('channel delete: '.$e->getMessage());
110                         }
111                 });
112                 $discord->on(Event::GUILD_ROLE_CREATE, function (Role $role, Discord $discord) {
113                         try {
114                                 DiscordRole::onUpstreamCreate($role);
115                         } catch (\Exception $e) {
116                                 $this->error('guild role create: '.$e->getMessage());
117                         }
118                 });
119                 $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discord, ?Role $old) {
120                         try {
121                                 DiscordRole::onUpstreamUpdate($role);
122                         } catch (\Exception $e) {
123                                 $this->error('guild role update: '.$e->getMessage());
124                         }
125                 });
126                 $discord->on(Event::GUILD_ROLE_DELETE, function ($role, Discord $discord) {
127                         try {
128                                 DiscordRole::onUpstreamDelete($role);
129                         } catch (\Exception $e) {
130                                 $this->error('guild role delete: '.$e->getMessage());
131                         }
132                 });
133                 $discord->getLoop()->addSignal(SIGINT, function() use ($discord) {
134                         $discord->close();
135                 });
136                 $discord->run();
137                 return 0;
138         }
139 }