-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
113 lines (96 loc) · 3.89 KB
/
script.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const serverInput = document.getElementById('serverInput');
const sendButton = document.getElementById('sendButton');
const serverInfo = document.getElementById('serverInfo');
const serverName = document.getElementById('serverName');
const memberCounter = document.getElementById('memberCounter');
const onlineMemberCounter = document.getElementById('onlineMemberCounter');
const countdownTimer = document.getElementById('countdownTimer');
const themeSwitch = document.getElementById('themeSwitch');
let intervalId;
let countdownTimerId;
let remainingTime = 30000; // 30 seconds
sendButton.addEventListener('click', () => {
const inputValue = serverInput.value.trim();
const inviteCode = extractInviteCode(inputValue);
if (inviteCode) {
fetchServerData(inviteCode);
} else {
showError('Invalid invite URL or code');
}
});
// Theme Switch
themeSwitch.addEventListener('change', () => {
document.body.classList.toggle('light');
});
function extractInviteCode(input) {
const urlPattern = /^https?:\/\/(www\.)?discord\.(gg|com\/invite)\/(\w+)/;
const match = input.match(urlPattern);
return match ? match[3] : input;
}
function fetchServerData(inviteCode) {
const apiUrl = `https://discord.com/api/v9/invites/${encodeURIComponent(inviteCode)}?with_counts=true`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data.guild) {
updateServerInfo(data);
startUpdateInterval(inviteCode);
startCountdownTimer();
} else {
showError('Invalid invite code');
}
})
.catch(error => {
showError('Error fetching server data');
console.error('Error:', error);
});
}
function updateServerInfo(serverData) {
const serverIconUrl = `https://cdn.discordapp.com/icons/${serverData.guild.id}/${serverData.guild.icon}`;
const serverIcon = document.getElementById('serverIcon');
serverIcon.src = serverIconUrl;
serverName.textContent = serverData.guild.name;
const totalMembers = serverData.approximate_member_count;
const onlineMembers = serverData.approximate_presence_count;
const prevTotalMembers = parseInt(memberCounter.textContent) || 0;
const prevOnlineMembers = parseInt(onlineMemberCounter.textContent) || 0;
if (totalMembers !== prevTotalMembers) {
memberCounter.classList.remove('animate__animated', 'animate__flipInY');
void memberCounter.offsetWidth; // Trigger a reflow
memberCounter.classList.add('animate__animated', 'animate__flipInY');
memberCounter.textContent = `${totalMembers}`;
}
if (onlineMembers !== prevOnlineMembers) {
onlineMemberCounter.classList.remove('animate__animated', 'animate__flipInY');
void onlineMemberCounter.offsetWidth; // Trigger a reflow
onlineMemberCounter.classList.add('animate__animated', 'animate__flipInY');
onlineMemberCounter.textContent = `${onlineMembers}`;
}
serverInfo.classList.remove('hidden');
}
function startUpdateInterval(inviteCode) {
clearInterval(intervalId);
intervalId = setInterval(() => {
fetchServerData(inviteCode);
}, 30000); // Update every 30 seconds
}
function startCountdownTimer() {
clearInterval(countdownTimerId);
countdownTimerId = setInterval(updateCountdownTimer, 1000);
}
function updateCountdownTimer() {
const minutes = Math.floor(remainingTime / 60000);
const seconds = Math.floor((remainingTime % 60000) / 1000);
countdownTimer.textContent = `Next update in: ${minutes}:${seconds.toString().padStart(2, '0')}`;
remainingTime -= 1000;
if (remainingTime <= 0) {
remainingTime = 30000; // Reset to 30 seconds
clearInterval(countdownTimerId);
}
}
function showError(message) {
serverInfo.classList.add('hidden');
memberCounter.textContent = message;
onlineMemberCounter.textContent = '';
countdownTimer.textContent = '';
}