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