3 namespace App\Console\Commands;
5 use App\DiscordAppCommands\AlttpCommand;
6 use App\Models\DiscordBotCommand as CommandModel;
7 use App\Models\DiscordChannel;
8 use App\Models\DiscordGuild;
9 use App\Models\DiscordRole;
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 Discord\WebSockets\Intents;
18 use Illuminate\Console\Command;
19 use Monolog\Handler\StreamHandler;
21 use React\EventLoop\Loop;
23 class DiscordBotCommand extends Command
26 * The name and signature of the console command.
30 protected $signature = 'discord:bot';
33 * The console command description.
37 protected $description = 'Runs the discord bot';
40 * Execute the console command.
44 public function handle()
46 $logger = new Logger('DiscordBot');
47 $logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO));
48 $discord = new Discord([
49 'intents' => Intents::getDefaultIntents() | Intents::GUILD_MEMBERS,
51 'token' => config('discord.token'),
53 $discord->on('ready', function (Discord $discord) {
54 $activity = new Activity($discord);
55 $activity->type = Activity::TYPE_LISTENING;
56 $activity->name = 'HolySmoke';
57 $activity->url = 'https://alttp.localhorst.tv/';
58 $discord->updatePresence($activity);
60 $discord->getLoop()->addPeriodicTimer(1, function () use ($discord) {
61 $command = CommandModel::where('status', '=', 'pending')->oldest()->first();
64 $command->execute($discord);
65 } catch (\Exception $e) {
70 if (config('discord.enable_commands')) {
71 AlttpCommand::listen($discord);
73 if (config('discord.create_commands')) {
74 AlttpCommand::create($discord);
77 $discord->on(Event::GUILD_CREATE, function (Guild $guild, Discord $discord) {
79 DiscordGuild::onUpstreamCreate($guild);
80 } catch (\Exception $e) {
81 $this->error('guild create: '.$e->getMessage());
84 $discord->on(Event::GUILD_UPDATE, function (Guild $guild, Discord $discord, ?Guild $old) {
86 DiscordGuild::onUpstreamUpdate($guild);
87 } catch (\Exception $e) {
88 $this->error('guild update: '.$e->getMessage());
91 $discord->on(Event::CHANNEL_CREATE, function (Channel $channel, Discord $discord) {
93 DiscordChannel::onUpstreamCreate($channel);
94 } catch (\Exception $e) {
95 $this->error('channel create: '.$e->getMessage());
98 $discord->on(Event::CHANNEL_UPDATE, function (Channel $channel, Discord $discord, ?Channel $old) {
100 DiscordChannel::onUpstreamUpdate($channel);
101 } catch (\Exception $e) {
102 $this->error('channel update: '.$e->getMessage());
105 $discord->on(Event::CHANNEL_DELETE, function ($channel, Discord $discord) {
107 DiscordChannel::onUpstreamDelete($channel);
108 } catch (\Exception $e) {
109 $this->error('channel delete: '.$e->getMessage());
112 $discord->on(Event::GUILD_ROLE_CREATE, function (Role $role, Discord $discord) {
114 DiscordRole::onUpstreamCreate($role);
115 } catch (\Exception $e) {
116 $this->error('guild role create: '.$e->getMessage());
119 $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discord, ?Role $old) {
121 DiscordRole::onUpstreamUpdate($role);
122 } catch (\Exception $e) {
123 $this->error('guild role update: '.$e->getMessage());
126 $discord->on(Event::GUILD_ROLE_DELETE, function ($role, Discord $discord) {
128 DiscordRole::onUpstreamDelete($role);
129 } catch (\Exception $e) {
130 $this->error('guild role delete: '.$e->getMessage());
133 $discord->getLoop()->addSignal(SIGINT, function() use ($discord) {