pigeon/pigeon.php

52 lines
1.5 KiB
PHP

<?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
$conf = new Config('config/config.yaml');
// create YouTube API object
$youtube = new Youtube(array('key' => $conf->get('youtube.api_key')));
// get channel
$yt_res = $youtube->getChannelById($conf->get('youtube.channel_id'));
// get channel info
$channel_name = $yt_res->snippet->title;
$channel_uploads_id = $yt_res->contentDetails->relatedPlaylists->uploads;
// get latest video from uploads playlist
$uploads = $youtube->getPlaylistItemsByPlaylistId($channel_uploads_id);
$latest_video_id = $uploads[0]->contentDetails->videoId;
$latest_url = "https://youtu.be/$latest_video_id";
// instantiate Discord API object
$discord = new Discord([
'token' => $conf->get('discord.api_key'),
'intents' => Intents::getDefaultIntents() | Intents::GUILDS,
]);
// run tasks when Discord is ready
$discord->on('ready', function (Discord $discord) use ($conf, $latest_url) {
echo "Bot is ready!", PHP_EOL;
$message_text = str_replace('{link}', $latest_url, $conf->get('discord.message_template'));
$discord->guilds->first()->channels->find(function ($channel) use ($conf) {
return $channel->name === $conf->get('discord.announcement_channel');
})->sendMessage($message_text)->done(function ($message) use ($discord) {
echo 'Message sent!', PHP_EOL;
$discord->close();
});
});
// run the Discord event loop
$discord->run();