47 lines
1.6 KiB
JavaScript
47 lines
1.6 KiB
JavaScript
const message = document.querySelector('[data-status-msg]');
|
||
const box = document.querySelector('[data-invite]');
|
||
const link = box.querySelector('a');
|
||
const code = decodeURIComponent(location.hash.slice(1));
|
||
const INVITE = /^https:\/\/discord\.(gg|com\/invite)\/[A-Za-z0-9-]+$/;
|
||
const POLL_MS = 15000;
|
||
|
||
const TEXT = {
|
||
pending: 'Your application has been received and is waiting for a moderator. This page updates on its own, so you can keep it open, or bookmark it and come back later.',
|
||
accepted: 'Good news: your application was approved! Use the invite below to join the server. It works once and expires 7 days after approval.',
|
||
declined: 'Your application was not approved at this time.',
|
||
unknown: 'We couldn’t find this application. It may be more than 14 days old, or the link may be incomplete.',
|
||
};
|
||
|
||
function show(state) {
|
||
message.textContent = TEXT[state];
|
||
}
|
||
|
||
async function check() {
|
||
try {
|
||
const res = await fetch('/api/join/status', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ code }),
|
||
});
|
||
if (res.status === 404) return show('unknown');
|
||
if (res.ok) {
|
||
const body = await res.json();
|
||
if (body.status === 'accepted' && INVITE.test(body.invite || '')) {
|
||
link.href = body.invite;
|
||
box.hidden = false;
|
||
return show('accepted');
|
||
}
|
||
if (body.status === 'declined') return show('declined');
|
||
show('pending');
|
||
}
|
||
} catch {
|
||
// Network hiccup: keep the last message and try again.
|
||
}
|
||
setTimeout(check, POLL_MS);
|
||
}
|
||
|
||
if (code) {
|
||
check();
|
||
} else {
|
||
show('unknown');
|
||
} |