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