import React, { useState, useEffect } from 'react'; import { useParams, useNavigate, useLocation } from 'react-router-dom'; import axios from 'axios'; import { Button, Typography, Box, IconButton, Switch, Select, MenuItem, FormControl, FormControlLabel, Radio, RadioGroup, TextField, Accordion, AccordionSummary, AccordionDetails, Snackbar, Alert } from '@mui/material'; import ArrowBackIcon from '@mui/icons-material/ArrowBack'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; // UserSettings moved to NavBar import ConfirmDialog from './ConfirmDialog'; import ContentCopyIcon from '@mui/icons-material/ContentCopy'; import DeleteIcon from '@mui/icons-material/Delete'; const ServerSettings = () => { const { guildId } = useParams(); const navigate = useNavigate(); const location = useLocation(); // settings state removed (not used) to avoid lint warnings const [isBotInServer, setIsBotInServer] = useState(false); const [clientId, setClientId] = useState(null); const [server, setServer] = useState(null); const [dialogOpen, setDialogOpen] = useState(false); const [channels, setChannels] = useState([]); const [roles, setRoles] = useState([]); const [autoroleSettings, setAutoroleSettings] = useState({ enabled: false, roleId: '', }); const [commandsList, setCommandsList] = useState([]); const [invites, setInvites] = useState([]); const [deleting, setDeleting] = useState({}); const [confirmOpen, setConfirmOpen] = useState(false); const [pendingDeleteInvite, setPendingDeleteInvite] = useState(null); const [snackbarOpen, setSnackbarOpen] = useState(false); const [snackbarMessage, setSnackbarMessage] = useState(''); const [inviteForm, setInviteForm] = useState({ channelId: '', maxAge: 0, maxUses: 0, temporary: false }); const [commandsExpanded, setCommandsExpanded] = useState(false); const [welcomeLeaveSettings, setWelcomeLeaveSettings] = useState({ welcome: { enabled: false, channel: '', message: 'Welcome to the server, {user}!', customMessage: '', }, leave: { enabled: false, channel: '', message: '{user} has left the server.', customMessage: '', }, }); const defaultWelcomeMessages = ["Welcome to the server, {user}!", "Hey {user}, welcome!", "{user} has joined the party!"]; const defaultLeaveMessages = ["{user} has left the server.", "Goodbye, {user}.", "We'll miss you, {user}."]; useEffect(() => { if (location.state && location.state.guild) { setServer(location.state.guild); } else { // Fallback if guild data is not passed in state const storedGuilds = localStorage.getItem('guilds'); if (storedGuilds) { const guild = JSON.parse(storedGuilds).find(g => g.id === guildId); setServer(guild); } } const API_BASE = process.env.REACT_APP_API_BASE || 'http://localhost:3002'; // Fetch settings (not used directly in this component) axios.get(`${API_BASE}/api/servers/${guildId}/settings`).catch(() => {}); // Check if bot is in server axios.get(`${API_BASE}/api/servers/${guildId}/bot-status`) .then(response => { setIsBotInServer(response.data.isBotInServer); }); // Fetch client ID axios.get(`${API_BASE}/api/client-id`) .then(response => { setClientId(response.data.clientId); }); // Fetch channels axios.get(`${API_BASE}/api/servers/${guildId}/channels`) .then(response => { setChannels(response.data); }); // Fetch welcome/leave settings axios.get(`${API_BASE}/api/servers/${guildId}/welcome-leave-settings`) .then(response => { if (response.data) { setWelcomeLeaveSettings(response.data); } }); // Fetch roles axios.get(`${API_BASE}/api/servers/${guildId}/roles`) .then(response => { setRoles(response.data); }); // Fetch autorole settings axios.get(`${API_BASE}/api/servers/${guildId}/autorole-settings`) .then(response => { if (response.data) { setAutoroleSettings(response.data); } }); // Fetch commands/help list axios.get(`${API_BASE}/api/servers/${guildId}/commands`) .then(response => { setCommandsList(response.data || []); }) .catch(() => setCommandsList([])); // Fetch invites axios.get(`${API_BASE}/api/servers/${guildId}/invites`) .then(resp => setInvites(resp.data || [])) .catch(() => setInvites([])); // Open commands accordion if navigated from Help back button if (location.state && location.state.openCommands) { setCommandsExpanded(true); } }, [guildId, location.state]); const handleAutoroleSettingUpdate = (newSettings) => { axios.post(`${process.env.REACT_APP_API_BASE || 'http://localhost:3002'}/api/servers/${guildId}/autorole-settings`, newSettings) .then(response => { if (response.data.success) { setAutoroleSettings(newSettings); } }); }; const handleAutoroleToggleChange = (event) => { const newSettings = { ...autoroleSettings, enabled: event.target.checked }; handleAutoroleSettingUpdate(newSettings); }; const handleAutoroleRoleChange = (event) => { const newSettings = { ...autoroleSettings, roleId: event.target.value }; handleAutoroleSettingUpdate(newSettings); }; const handleSettingUpdate = (newSettings) => { axios.post(`${process.env.REACT_APP_API_BASE || 'http://localhost:3002'}/api/servers/${guildId}/welcome-leave-settings`, newSettings) .then(response => { if (response.data.success) { setWelcomeLeaveSettings(newSettings); } }); } const handleToggleChange = (type) => (event) => { const newSettings = { ...welcomeLeaveSettings }; newSettings[type].enabled = event.target.checked; handleSettingUpdate(newSettings); }; const handleChannelChange = (type) => (event) => { const newSettings = { ...welcomeLeaveSettings }; newSettings[type].channel = event.target.value; handleSettingUpdate(newSettings); }; const handleMessageOptionChange = (type) => (event) => { const newSettings = { ...welcomeLeaveSettings }; if (event.target.value !== 'custom') { newSettings[type].message = event.target.value; handleSettingUpdate(newSettings); } else { const tempSettings = { ...welcomeLeaveSettings }; // Set message to custom message to get the radio button to select custom tempSettings[type].message = tempSettings[type].customMessage; setWelcomeLeaveSettings(tempSettings); } }; const handleCustomMessageChange = (type) => (event) => { const newSettings = { ...welcomeLeaveSettings }; newSettings[type].customMessage = event.target.value; setWelcomeLeaveSettings(newSettings); }; const handleApplyCustomMessage = (type) => () => { const newSettings = { ...welcomeLeaveSettings }; newSettings[type].message = newSettings[type].customMessage; handleSettingUpdate(newSettings); }; const getMessageValue = (type) => { const currentMessage = welcomeLeaveSettings[type].message; const defaultMessages = type === 'welcome' ? defaultWelcomeMessages : defaultLeaveMessages; if (defaultMessages.includes(currentMessage)) { return currentMessage; } return 'custom'; } const handleInviteBot = () => { if (!clientId) return; const permissions = 8; // Administrator const url = `https://discord.com/api/oauth2/authorize?client_id=${clientId}&permissions=${permissions}&scope=bot%20applications.commands&guild_id=${guildId}&disable_guild_select=true`; window.open(url, '_blank'); }; const handleLeaveBot = () => { setDialogOpen(true); }; const handleConfirmLeave = async () => { try { await axios.post(`${process.env.REACT_APP_API_BASE || 'http://localhost:3002'}/api/servers/${guildId}/leave`); setIsBotInServer(false); } catch (error) { console.error('Error leaving server:', error); } setDialogOpen(false); }; const handleBack = () => { navigate('/dashboard'); } return (