3 namespace App\Console\Commands;
5 use App\Models\DiscordChannel;
6 use App\Models\DiscordGuild;
7 use App\Models\DiscordRole;
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\WebSockets\Event;
14 use Illuminate\Console\Command;
15 use Monolog\Handler\StreamHandler;
17 use React\EventLoop\Loop;
19 class DiscordBotCommand extends Command
22 * The name and signature of the console command.
26 protected $signature = 'discord:bot';
29 * The console command description.
33 protected $description = 'Runs the discord bot';
36 * Execute the console command.
40 public function handle()
42 $logger = new Logger('DiscordBot');
43 $logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO));
44 $discord = new Discord([
46 'token' => config('discord.token'),
48 $discord->on(Event::GUILD_CREATE, function (Guild $guild, Discord $discord) {
50 DiscordGuild::onUpstreamCreate($guild);
51 } catch (\Exception $e) {
52 $this->error('guild create: '.$e->getMessage());
55 $discord->on(Event::GUILD_UPDATE, function (Guild $guild, Discord $discord, ?Guild $old) {
57 DiscordGuild::onUpstreamUpdate($guild);
58 } catch (\Exception $e) {
59 $this->error('guild update: '.$e->getMessage());
62 $discord->on(Event::CHANNEL_CREATE, function (Channel $channel, Discord $discord) {
64 DiscordGuild::onUpstreamCreate($channel);
65 } catch (\Exception $e) {
66 $this->error('channel create: '.$e->getMessage());
69 $discord->on(Event::CHANNEL_UPDATE, function (Channel $channel, Discord $discord, ?Channel $old) {
71 DiscordGuild::onUpstreamUpdate($channel);
72 } catch (\Exception $e) {
73 $this->error('channel update: '.$e->getMessage());
76 $discord->on(Event::CHANNEL_DELETE, function ($channel, Discord $discord) {
78 DiscordGuild::onUpstreamDelete($channel);
79 } catch (\Exception $e) {
80 $this->error('channel delete: '.$e->getMessage());
83 $discord->on(Event::GUILD_ROLE_CREATE, function (Role $role, Discord $discord) {
85 DiscordRole::onUpstreamCreate($role);
86 } catch (\Exception $e) {
87 $this->error('guild role create: '.$e->getMessage());
90 $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discord, ?Role $old) {
92 DiscordRole::onUpstreamUpdate($role);
93 } catch (\Exception $e) {
94 $this->error('guild role update: '.$e->getMessage());
97 $discord->on(Event::GUILD_ROLE_DELETE, function ($role, Discord $discord) {
99 DiscordRole::onUpstreamDelete($role);
100 } catch (\Exception $e) {
101 $this->error('guild role delete: '.$e->getMessage());
104 $discord->on('ready', function (Discord $discord) {
105 $discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) {
106 if (!empty($message->guild_id)) return;
107 if (!empty($message->webhook_id)) return;
108 if (!empty($message->application_id)) return;
109 if (is_null($message->author)) return;
110 if ($message->author->bot) return;
111 $discord->getLoop()->addTimer(0.6, function() use ($message) {
112 $message->react('😄');
114 if (!is_null($message->channel)) {
115 $discord->getLoop()->addTimer(2.0, function() use ($message) {
116 $message->channel->sendMessage('bugger off');
119 $message->delayedReply('bugger off', 2000);
123 $discord->getLoop()->addSignal(SIGINT, function() use ($discord) {