Moved code to under the src/ directory (to keep the directory structure tidy); created a separate instance of the EventLoop to have a periodic YouTube check
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
Gregory Ballantine 2022-03-07 23:03:48 -05:00
parent eb25a475ab
commit 803ad25cda
4 changed files with 57 additions and 6 deletions

View File

@ -21,6 +21,6 @@
}, },
"scripts": { "scripts": {
"phpcs": "phpcs --standard=./phpcs.xml", "phpcs": "phpcs --standard=./phpcs.xml",
"phpmd": "phpmd pigeon.php text phpmd.xml" "phpmd": "phpmd src/ text phpmd.xml"
} }
} }

View File

@ -11,8 +11,8 @@
<arg value="p" /> <arg value="p" />
<arg value="s" /> <arg value="s" />
<!-- pigeon.php is the only file to check currently --> <!-- Check PHP files in the src/ directory -->
<file>pigeon.php</file> <file>src/</file>
<!-- Our base rule: set to PSR12--> <!-- Our base rule: set to PSR12-->
<rule ref="PSR12"/> <rule ref="PSR12"/>

View File

@ -11,9 +11,7 @@
</description> </description>
<!-- Import some rule sets --> <!-- Import some rule sets -->
<rule ref="rulesets/cleancode.xml"> <rule ref="rulesets/cleancode.xml" />
<exclude name="StaticAccess" />
</rule>
<rule ref="rulesets/codesize.xml" /> <rule ref="rulesets/codesize.xml" />
<rule ref="rulesets/design.xml" /> <rule ref="rulesets/design.xml" />
<rule ref="rulesets/naming.xml" /> <rule ref="rulesets/naming.xml" />

53
src/pigeon.php Normal file
View File

@ -0,0 +1,53 @@
<?php
include __DIR__ . '/../vendor/autoload.php';
use Discord\Discord;
use Discord\Parts\Channel\Message;
use Discord\WebSockets\Intents;
use Madcoda\Youtube\Youtube;
use Noodlehaus\Config;
// load config file
$config = new Config('config/config.yaml');
// create YouTube API object
$youtube = new Youtube(array('key' => $config->get('youtube.api_key')));
// get channel
$yt_res = $youtube->getChannelById($config->get('youtube.channel_id'));
// get channel info
$config->set('youtube.channel_name', $yt_res->snippet->title);
$config->set('youtube.uploads_id', $yt_res->contentDetails->relatedPlaylists->uploads);
// create our event loop
$loop = React\EventLoop\Factory::create();
// instantiate Discord API object
$discord = new Discord([
'token' => $config->get('discord.api_key'),
'intents' => Intents::getDefaultIntents() | Intents::GUILDS,
'loop' => $loop,
]);
// periodic youtube uploads check
$loop->addPeriodicTimer($config->get('pigeon.check_interval') * 60, function () use ($config, $discord, $youtube) {
// get latest video from uploads playlist
$uploads = $youtube->getPlaylistItemsByPlaylistId($config->get('youtube.uploads_id'));
$latest_video_id = $uploads[0]->contentDetails->videoId;
$latest_url = "https://youtu.be/$latest_video_id";
$message_text = str_replace('{link}', $latest_url, $config->get('discord.message_template'));
$discord->guilds->first()->channels->find(function ($channel) use ($config) {
return $channel->name === $config->get('discord.announcement_channel');
})->sendMessage($message_text)->done(function ($message) use ($discord) {
echo 'Message sent!', PHP_EOL;
});
});
// run tasks when Discord is ready
$discord->on('ready', function (Discord $discord) use ($conf, $latest_url) {
echo "Bot is ready!", PHP_EOL;
});
$discord->run();