]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/DiscordBotCommand.php
add aosr slash command
[alttp.git] / app / Console / Commands / DiscordBotCommand.php
1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\DiscordAppCommands\AosrPresetCommand;
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 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                         'logger' => $logger,
49                         'token' => config('discord.token'),
50                 ]);
51                 $discord->on('ready', function (Discord $discord) {
52                         $activity = new Activity($discord);
53                         $activity->type = Activity::TYPE_LISTENING;
54                         $activity->name = 'HolySmoke';
55                         $activity->url = 'https://alttp.localhorst.tv/';
56                         $discord->updatePresence($activity);
57
58                         $discord->getLoop()->addPeriodicTimer(1, function () use ($discord) {
59                                 $command = CommandModel::where('status', '=', 'pending')->oldest()->first();
60                                 if ($command) {
61                                         try {
62                                                 $command->execute($discord);
63                                         } catch (\Exception $e) {
64                                         }
65                                 }
66                         });
67
68                         if (config('discord.enable_commands')) {
69                                 AosrPresetCommand::listen($discord);
70                         }
71                         if (config('discord.create_commands')) {
72                                 AosrPresetCommand::create($discord);
73                         }
74                 });
75                 $discord->on(Event::GUILD_CREATE, function (Guild $guild, Discord $discord) {
76                         try {
77                                 DiscordGuild::onUpstreamCreate($guild);
78                         } catch (\Exception $e) {
79                                 $this->error('guild create: '.$e->getMessage());
80                         }
81                 });
82                 $discord->on(Event::GUILD_UPDATE, function (Guild $guild, Discord $discord, ?Guild $old) {
83                         try {
84                                 DiscordGuild::onUpstreamUpdate($guild);
85                         } catch (\Exception $e) {
86                                 $this->error('guild update: '.$e->getMessage());
87                         }
88                 });
89                 $discord->on(Event::CHANNEL_CREATE, function (Channel $channel, Discord $discord) {
90                         try {
91                                 DiscordChannel::onUpstreamCreate($channel);
92                         } catch (\Exception $e) {
93                                 $this->error('channel create: '.$e->getMessage());
94                         }
95                 });
96                 $discord->on(Event::CHANNEL_UPDATE, function (Channel $channel, Discord $discord, ?Channel $old) {
97                         try {
98                                 DiscordChannel::onUpstreamUpdate($channel);
99                         } catch (\Exception $e) {
100                                 $this->error('channel update: '.$e->getMessage());
101                         }
102                 });
103                 $discord->on(Event::CHANNEL_DELETE, function ($channel, Discord $discord) {
104                         try {
105                                 DiscordChannel::onUpstreamDelete($channel);
106                         } catch (\Exception $e) {
107                                 $this->error('channel delete: '.$e->getMessage());
108                         }
109                 });
110                 $discord->on(Event::GUILD_ROLE_CREATE, function (Role $role, Discord $discord) {
111                         try {
112                                 DiscordRole::onUpstreamCreate($role);
113                         } catch (\Exception $e) {
114                                 $this->error('guild role create: '.$e->getMessage());
115                         }
116                 });
117                 $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discord, ?Role $old) {
118                         try {
119                                 DiscordRole::onUpstreamUpdate($role);
120                         } catch (\Exception $e) {
121                                 $this->error('guild role update: '.$e->getMessage());
122                         }
123                 });
124                 $discord->on(Event::GUILD_ROLE_DELETE, function ($role, Discord $discord) {
125                         try {
126                                 DiscordRole::onUpstreamDelete($role);
127                         } catch (\Exception $e) {
128                                 $this->error('guild role delete: '.$e->getMessage());
129                         }
130                 });
131                 $discord->getLoop()->addSignal(SIGINT, function() use ($discord) {
132                         $discord->close();
133                 });
134                 $discord->run();
135                 return 0;
136         }
137 }