mirror of
https://github.com/2004Scape/Website.git
synced 2026-11-22 21:50:40 -05:00
* fix: Updated ban date check logic * chore: Added all backend endpoints for complete v1 mod web tools * feat: Added unban, mute/unmute, kick to mod tools
282 lines
9.4 KiB
Plaintext
282 lines
9.4 KiB
Plaintext
<script>
|
|
const banButton = document.getElementById('ban-btn'),
|
|
muteButton = document.getElementById('mute-btn'),
|
|
changeUsernameButton = document.getElementById('name-change-btn'),
|
|
kickButton = document.getElementById('kick-btn');
|
|
|
|
const banModal = document.getElementById('modal-ban-player'),
|
|
banModalUsername = document.getElementById('modal-ban-player-username'),
|
|
banModalTimePeriodSelect = document.getElementById('modal-ban-player-time-select'),
|
|
banModalCustomTimePeriod = document.getElementById('modal-ban-player-time-custom'),
|
|
banModalBanButton = document.getElementById('modal-ban-player-btn'),
|
|
banModalMercyButton = document.getElementById('modal-cancel-ban-btn');
|
|
|
|
const changeUsernameModal = document.getElementById('modal-change-username'),
|
|
changeUsernameConfirmButton = document.getElementById('modal-confirm-change-username-btn'),
|
|
changeUsernameUnbanConfirmButton = document.getElementById('modal-confirm-change-unban-username-btn'),
|
|
changeUsernameCancelButton = document.getElementById('modal-cancel-change-username-btn'),
|
|
changeUsernameTitle = document.getElementById('modal-change-username-span'),
|
|
changeUsernameInput = document.getElementById('modal-change-username-input');
|
|
|
|
const muteModal = document.getElementById('modal-mute-player'),
|
|
muteModalUsername = document.getElementById('modal-mute-player-username'),
|
|
muteModalTimePeriodSelect = document.getElementById('modal-mute-player-time-select'),
|
|
muteModalCustomTimePeriod = document.getElementById('modal-mute-player-time-custom'),
|
|
muteModalMuteButton = document.getElementById('modal-mute-player-btn'),
|
|
muteModalMercyButton = document.getElementById('modal-cancel-mute-btn');
|
|
|
|
function addDaysToCurrentDate(days) {
|
|
const currentDate = new Date();
|
|
|
|
// Get the current timestamp, and add days converted to milliseconds because .setDate()/.getDate()
|
|
// give wildly inaccurate results for some reason
|
|
const futureTime = currentDate.getTime() + (days * 24 * 60 * 60 * 1000);
|
|
return new Date(futureTime);
|
|
}
|
|
|
|
function resetModalInputs() {
|
|
banModalTimePeriodSelect.value = 1;
|
|
banModalCustomTimePeriod.value = "";
|
|
muteModalTimePeriodSelect.value = 1;
|
|
muteModalCustomTimePeriod.value = "";
|
|
changeUsernameInput.value = "";
|
|
}
|
|
|
|
async function submitBanUserRequest() {
|
|
if (banModalTimePeriodSelect.value === -1) {
|
|
if (banModalCustomTimePeriod.value == "" || banModalCustomTimePeriod.value == null) {
|
|
alert('Please enter a new period of time:');
|
|
return;
|
|
}
|
|
}
|
|
|
|
let numberOfDays = banModalTimePeriodSelect.value == -1 ? banModalCustomTimePeriod.value : banModalTimePeriodSelect.value;
|
|
let banDate = addDaysToCurrentDate(numberOfDays);
|
|
|
|
if (confirm(`Are you SURE you want to ban ${account.username} for ${numberOfDays} day(s)?`)) {
|
|
fetch(`/mod/ban/${account.id}`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
banned_until: banDate.toISOString()
|
|
}),
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
})
|
|
.then(res => res.json())
|
|
.then(json => {
|
|
if (json.success) {
|
|
banModal.close();
|
|
alert(`Banned ${account.username} for ${numberOfDays} day(s).`);
|
|
window.location.reload();
|
|
} else {
|
|
alert(json.error);
|
|
}
|
|
}).catch(err => {
|
|
alert(`An unknown error occurred. Please report this issue.\n\n${err}`);
|
|
console.log(err);
|
|
});
|
|
}
|
|
}
|
|
|
|
async function submitUnbanUserRequest() {
|
|
if (confirm(`Are you sure you want to unban '${account.username}'?`)) {
|
|
fetch(`/mod/unban/${account.id}`, {
|
|
method: 'POST',
|
|
})
|
|
.then(res => res.json())
|
|
.then(json => {
|
|
if (json.success) {
|
|
alert(`Successfully unbanned '${account.username}'`);
|
|
location.reload();
|
|
} else {
|
|
alert(json.error);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
async function submitMuteUserRequest() {
|
|
if (muteModalTimePeriodSelect.value === -1) {
|
|
if (muteModalCustomTimePeriod.value == "" || muteModalCustomTimePeriod.value == null) {
|
|
alert('Please enter a new period of time:');
|
|
return;
|
|
}
|
|
}
|
|
|
|
let numberOfDays = muteModalTimePeriodSelect.value == -1 ? muteModalCustomTimePeriod.value : muteModalTimePeriodSelect.value;
|
|
let muteDate = addDaysToCurrentDate(numberOfDays);
|
|
|
|
if (confirm(`Are you SURE you want to mute '${account.username}' for ${numberOfDays} day(s)?`)) {
|
|
fetch(`/mod/mute/${account.id}`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
muted_until: muteDate.toISOString()
|
|
}),
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
})
|
|
.then(res => res.json())
|
|
.then(json => {
|
|
if (json.success) {
|
|
muteModal.close();
|
|
alert(`Muted ${account.username} for ${numberOfDays} day(s).`);
|
|
window.location.reload();
|
|
} else {
|
|
alert(json.error);
|
|
}
|
|
}).catch(err => {
|
|
alert(`An unknown error occurred. Please report this issue.\n\n${err}`);
|
|
console.log(err);
|
|
});
|
|
}
|
|
}
|
|
|
|
async function submitUnmuteUserRequest() {
|
|
if (confirm(`Are you sure you want to unmute '${account.username}'?`)) {
|
|
fetch(`/mod/unmute/${account.id}`, {
|
|
method: 'POST',
|
|
})
|
|
.then(res => res.json())
|
|
.then(json => {
|
|
if (json.success) {
|
|
alert(`Successfully unmuted '${account.username}'`);
|
|
location.reload();
|
|
} else {
|
|
alert(json.error);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
async function submitChangeUsernameRequest(unban = false) {
|
|
const newUsername = changeUsernameInput.value;
|
|
if (newUsername === "") {
|
|
alert('Please enter new username before submitting!');
|
|
return;
|
|
}
|
|
|
|
if (confirm(`Are you sure you want to ${unban ? 'unban and ' : ''}rename ${account.username} to '${newUsername}?'`)) {
|
|
let body = {
|
|
new_username: newUsername
|
|
};
|
|
|
|
if (unban) {
|
|
body.unban = true;
|
|
}
|
|
|
|
fetch(`/mod/change-name/${account.id}`, {
|
|
method: 'POST',
|
|
body: JSON.stringify(body),
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
})
|
|
.then(res => res.json())
|
|
.then(json => {
|
|
if (json.error) {
|
|
alert(json.error);
|
|
} else {
|
|
changeUsernameModal.close();
|
|
alert(`Successfully ${unban ? 'unbanned and ' : ''}changed ${account.username}'s username to '${newUsername}'.`);
|
|
location.href = `/mod/overview/${newUsername}`
|
|
}
|
|
}).catch(err => {
|
|
alert(`An unknown error occurred. Please report this issue.\n\n${err}`);
|
|
console.log(err);
|
|
});
|
|
}
|
|
}
|
|
|
|
async function submitKickUserRequest() {
|
|
if (confirm(`Are you sure you want to kick '${account.username}'?`)) {
|
|
fetch(`/mod/kick/${account.id}`, {
|
|
method: 'POST',
|
|
})
|
|
.then(res => res.json())
|
|
.then(json => {
|
|
if (json.success) {
|
|
alert(`Successfully kicked '${account.username}'`);
|
|
location.reload();
|
|
} else {
|
|
alert(json.error);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
banButton.addEventListener('click', (event) => {
|
|
if (account.banned_until) {
|
|
submitUnbanUserRequest();
|
|
} else {
|
|
resetModalInputs();
|
|
banModal.showModal();
|
|
}
|
|
});
|
|
banModalMercyButton.addEventListener('click', (event) => {
|
|
banModal.close();
|
|
});
|
|
banModalBanButton.addEventListener('click', submitBanUserRequest);
|
|
banModalTimePeriodSelect.addEventListener('change', (event) => {
|
|
const newValue = event.target.value;
|
|
if (newValue == -1) {
|
|
banModalCustomTimePeriod.classList.remove('hidden');
|
|
} else {
|
|
banModalCustomTimePeriod.classList.add('hidden');
|
|
}
|
|
});
|
|
|
|
muteButton.addEventListener('click', (event) => {
|
|
if (account.muted_until) {
|
|
submitUnmuteUserRequest();
|
|
} else {
|
|
resetModalInputs();
|
|
muteModal.showModal();
|
|
}
|
|
});
|
|
muteModalMuteButton.addEventListener('click', (event) => {
|
|
submitMuteUserRequest();
|
|
});
|
|
muteModalMercyButton.addEventListener('click', (event) => {
|
|
muteModal.close();
|
|
});
|
|
muteModalTimePeriodSelect.addEventListener('change', (event) => {
|
|
const newValue = event.target.value;
|
|
if (newValue == -1) {
|
|
muteModalCustomTimePeriod.classList.remove('hidden');
|
|
} else {
|
|
muteModalCustomTimePeriod.classList.add('hidden');
|
|
}
|
|
});
|
|
|
|
changeUsernameButton.addEventListener('click', (event) => {
|
|
resetModalInputs();
|
|
changeUsernameModal.showModal();
|
|
});
|
|
changeUsernameConfirmButton.addEventListener('click', (event) => {
|
|
submitChangeUsernameRequest();
|
|
});
|
|
changeUsernameUnbanConfirmButton.addEventListener('click', (event) => {
|
|
submitChangeUsernameRequest(true);
|
|
});
|
|
changeUsernameCancelButton.addEventListener('click', (event) => {
|
|
changeUsernameModal.close();
|
|
});
|
|
|
|
kickButton.addEventListener('click', (event) => {
|
|
submitKickUserRequest();
|
|
});
|
|
|
|
banModalUsername.textContent = account.username;
|
|
muteModalUsername.textContent = account.username;
|
|
changeUsernameTitle.textContent = account.username;
|
|
|
|
if (account.banned_until) {
|
|
document.getElementById('banned-until-display').textContent = (new Date(account.banned_until)).toLocaleString();
|
|
}
|
|
if (account.muted_until) {
|
|
document.getElementById('muted-until-display').textContent = (new Date(account.muted_until)).toLocaleString();
|
|
}
|
|
resetModalInputs();
|
|
</script> |