function setCookie(name, value, days) {
const now = new Date();
now.setTime(now.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = "expires=" + now.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
let savedLink = getCookie(cookieName);
if (savedLink) {
const foundLinkObj = links.find(item => item.url === savedLink);
if (foundLinkObj) {
window.location.href = foundLinkObj.url;
return;
}
}
const activeLinks = links.filter(item => item.active);
if (activeLinks.length === 0) {
alert("No momento, não há grupos disponíveis.");
} else {
const randomLink = activeLinks[Math.floor(Math.random() * activeLinks.length)].url;
setCookie(cookieName, randomLink, 30);
window.location.href = randomLink;
}