Retain token
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
const { createAudioPlayer, createAudioResource, AudioPlayerStatus, joinVoiceChannel } = require('@discordjs/voice');
|
const { createAudioPlayer, createAudioResource, AudioPlayerStatus, joinVoiceChannel } = require('@discordjs/voice');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const { spawn } = require('child_process')
|
const { spawn, exec } = require('child_process');
|
||||||
|
const path = require('path');
|
||||||
|
const { v4: uuidv4 } = require('uuid');
|
||||||
|
|
||||||
const queueMap = new Map();
|
const queueMap = new Map();
|
||||||
const playerMap = new Map();
|
const playerMap = new Map();
|
||||||
@ -8,6 +10,44 @@ const currentTrackMap = new Map();
|
|||||||
const repeatMap = new Map();
|
const repeatMap = new Map();
|
||||||
const voiceChannelMap = new Map();
|
const voiceChannelMap = new Map();
|
||||||
|
|
||||||
|
const lastActivityMap = new Map();
|
||||||
|
const cookieRefreshTimers = new Map();
|
||||||
|
const IDLE_THRESHOLD = 3 * 60 * 60 * 1000;
|
||||||
|
const COOKIE_REFRESH_URL = 'https://www.youtube.com/watch?v=ORMRfFYMwVU';
|
||||||
|
|
||||||
|
function refreshCookies() {
|
||||||
|
console.log('Refreshing cookies by downloading video...');
|
||||||
|
const tempFilePath = path.join(__dirname, 'tmp', `cookie_refresh_${uuidv4()}.mp3`);
|
||||||
|
const ytDlpPath = path.join(__dirname, '../yt-dlp');
|
||||||
|
|
||||||
|
exec(`"${ytDlpPath}" --cookies ${path.join(__dirname, '../cookies.txt')} --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" --sleep-interval 1 --max-sleep-interval 3 --format bestaudio --output "${tempFilePath}" "${COOKIE_REFRESH_URL}"`, (error, stdout, stderr) => {
|
||||||
|
if (error) {
|
||||||
|
console.error('Cookie refresh failed:', error);
|
||||||
|
} else {
|
||||||
|
console.log('Cookie refresh successful');
|
||||||
|
fs.unlink(tempFilePath, (err) => {
|
||||||
|
if (err) console.error('Error deleting cookie refresh file:', err);
|
||||||
|
else console.log('Cookie refresh file deleted');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateActivity(guildId) {
|
||||||
|
lastActivityMap.set(guildId, Date.now());
|
||||||
|
|
||||||
|
if (cookieRefreshTimers.has(guildId)) {
|
||||||
|
clearTimeout(cookieRefreshTimers.get(guildId));
|
||||||
|
}
|
||||||
|
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
console.log(`Guild ${guildId} has been idle for 3 hours, refreshing cookies...`);
|
||||||
|
refreshCookies();
|
||||||
|
}, IDLE_THRESHOLD);
|
||||||
|
|
||||||
|
cookieRefreshTimers.set(guildId, timer);
|
||||||
|
}
|
||||||
|
|
||||||
function addToQueue(guildId, filePath, title, voiceChannel, url, requester, avatarURL, thumbnailUrl) {
|
function addToQueue(guildId, filePath, title, voiceChannel, url, requester, avatarURL, thumbnailUrl) {
|
||||||
if (!queueMap.has(guildId)) {
|
if (!queueMap.has(guildId)) {
|
||||||
queueMap.set(guildId, []);
|
queueMap.set(guildId, []);
|
||||||
@ -18,6 +58,8 @@ function addToQueue(guildId, filePath, title, voiceChannel, url, requester, avat
|
|||||||
voiceChannelMap.set(guildId, voiceChannel);
|
voiceChannelMap.set(guildId, voiceChannel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateActivity(guildId);
|
||||||
|
|
||||||
const audioPlayer = playerMap.get(guildId);
|
const audioPlayer = playerMap.get(guildId);
|
||||||
|
|
||||||
if (!audioPlayer || audioPlayer.state.status !== AudioPlayerStatus.Playing) {
|
if (!audioPlayer || audioPlayer.state.status !== AudioPlayerStatus.Playing) {
|
||||||
@ -95,6 +137,8 @@ function playTrack(guildId, voiceChannel, track) {
|
|||||||
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
|
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
updateActivity(guildId);
|
||||||
|
|
||||||
let audioPlayer = playerMap.get(guildId);
|
let audioPlayer = playerMap.get(guildId);
|
||||||
|
|
||||||
if (!audioPlayer) {
|
if (!audioPlayer) {
|
||||||
@ -187,4 +231,13 @@ function clearQueue(guildId) {
|
|||||||
queueMap.set(guildId, []);
|
queueMap.set(guildId, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack, toggleRepeat, clearQueue, removeFromQueue, getPlayer };
|
function cleanupGuild(guildId) {
|
||||||
|
// Clear the cookie refresh timer when bot leaves or guild is cleaned up
|
||||||
|
if (cookieRefreshTimers.has(guildId)) {
|
||||||
|
clearTimeout(cookieRefreshTimers.get(guildId));
|
||||||
|
cookieRefreshTimers.delete(guildId);
|
||||||
|
}
|
||||||
|
lastActivityMap.delete(guildId);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack, toggleRepeat, clearQueue, removeFromQueue, getPlayer, cleanupGuild };
|
Reference in New Issue
Block a user