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