-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.js
64 lines (53 loc) · 2.17 KB
/
youtube.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const axios = require('axios');
const { Client, GatewayIntentBits } = require('discord.js');
require('dotenv').config(); // Load environment variables from the .env file
const fs = require('fs');
const client = new Client({
intents: [GatewayIntentBits.Guilds],
});
const channelId = '1184800068918841384';
const apiKey = process.env.YOUTUBE_KEY; // Access environment variable using process.env
const channelIdToTrack = 'UCtckgmUcpzqGnzcs7xEqMzQ';
// Load the last fetched video IDs from a file, or initialize it as an empty array
let lastFetchedVideoIds = [];
try {
const fileData = fs.readFileSync('./lastFetchedVideo.txt', 'utf8');
lastFetchedVideoIds = fileData.split('\n').filter(id => id.trim() !== '');
} catch (error) {
console.error('Error loading lastFetchedVideo:', error);
lastFetchedVideoIds = [];
}
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});
client.login(process.env.TOKEN); // Access the token from the .env file using process.env
async function checkForNewVideo() {
try {
// Fetch the latest video from the YouTube channel
const response = await axios.get(
`https://www.googleapis.com/youtube/v3/search?key=${apiKey}&channelId=${channelIdToTrack}&part=snippet,id&order=date&maxResults=1`
);
if (response.data.items.length > 0) {
const video = response.data.items[0];
// Extract the video ID
const videoId = video.id.videoId;
// Check if this is a new video by comparing with the existing video IDs
if (!lastFetchedVideoIds.includes(videoId)) {
// Append the new video ID to the array
lastFetchedVideoIds.push(videoId);
// Write the updated array to the file
fs.writeFileSync('./lastFetchedVideo.txt', lastFetchedVideoIds.join('\n'), 'utf8');
// Send a message to the Discord channel
const channel = client.channels.cache.get(channelId);
if (channel) {
channel.send(`New video: ${video.snippet.title}\n${video.snippet.description}\nhttps://www.youtube.com/watch?v=${videoId}`);
}
}
}
} catch (error) {
console.error('Error fetching YouTube data:', error);
}
}
module.exports = {
checkForNewVideo,
};