3 namespace App\Console\Commands;
5 use App\Models\DiscordBotCommand as CommandModel;
6 use App\Models\DiscordChannel;
7 use App\Models\DiscordGuild;
8 use App\Models\DiscordRole;
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 Discord\WebSockets\Intents;
17 use Illuminate\Console\Command;
18 use Monolog\Handler\StreamHandler;
20 use React\EventLoop\Loop;
22 class DiscordBotCommand extends Command
25 * The name and signature of the console command.
29 protected $signature = 'discord:bot';
32 * The console command description.
36 protected $description = 'Runs the discord bot';
39 * Execute the console command.
43 public function handle()
45 $logger = new Logger('DiscordBot');
46 $logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO));
47 $discord = new Discord([
48 'intents' => Intents::getDefaultIntents() | Intents::GUILD_MEMBERS,
50 'token' => config('discord.token'),
52 $discord->on('ready', function (Discord $discord) {
53 $activity = new Activity($discord);
54 $activity->type = Activity::TYPE_LISTENING;
55 $activity->name = 'HolySmoke';
56 $activity->url = 'https://alttp.localhorst.tv/';
57 $discord->updatePresence($activity);
59 $discord->getLoop()->addPeriodicTimer(1, function () use ($discord) {
60 $command = CommandModel::where('status', '=', 'pending')->oldest()->first();
63 $command->execute($discord);
64 } catch (\Exception $e) {
69 if (config('discord.enable_commands')) {
70 //AosrPresetCommand::listen($discord);
72 if (config('discord.create_commands')) {
73 //AosrPresetCommand::delete($discord);
76 $discord->on(Event::GUILD_CREATE, function (Guild $guild, Discord $discord) {
78 DiscordGuild::onUpstreamCreate($guild);
79 } catch (\Exception $e) {
80 $this->error('guild create: '.$e->getMessage());
83 $discord->on(Event::GUILD_UPDATE, function (Guild $guild, Discord $discord, ?Guild $old) {
85 DiscordGuild::onUpstreamUpdate($guild);
86 } catch (\Exception $e) {
87 $this->error('guild update: '.$e->getMessage());
90 $discord->on(Event::CHANNEL_CREATE, function (Channel $channel, Discord $discord) {
92 DiscordChannel::onUpstreamCreate($channel);
93 } catch (\Exception $e) {
94 $this->error('channel create: '.$e->getMessage());
97 $discord->on(Event::CHANNEL_UPDATE, function (Channel $channel, Discord $discord, ?Channel $old) {
99 DiscordChannel::onUpstreamUpdate($channel);
100 } catch (\Exception $e) {
101 $this->error('channel update: '.$e->getMessage());
104 $discord->on(Event::CHANNEL_DELETE, function ($channel, Discord $discord) {
106 DiscordChannel::onUpstreamDelete($channel);
107 } catch (\Exception $e) {
108 $this->error('channel delete: '.$e->getMessage());
111 $discord->on(Event::GUILD_ROLE_CREATE, function (Role $role, Discord $discord) {
113 DiscordRole::onUpstreamCreate($role);
114 } catch (\Exception $e) {
115 $this->error('guild role create: '.$e->getMessage());
118 $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discord, ?Role $old) {
120 DiscordRole::onUpstreamUpdate($role);
121 } catch (\Exception $e) {
122 $this->error('guild role update: '.$e->getMessage());
125 $discord->on(Event::GUILD_ROLE_DELETE, function ($role, Discord $discord) {
127 DiscordRole::onUpstreamDelete($role);
128 } catch (\Exception $e) {
129 $this->error('guild role delete: '.$e->getMessage());
132 $discord->getLoop()->addSignal(SIGINT, function() use ($discord) {