Program Commit
This commit is contained in:
111
frontend/src/components/ServerSettings.js
Normal file
111
frontend/src/components/ServerSettings.js
Normal file
@@ -0,0 +1,111 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { useParams, useNavigate, useLocation } from 'react-router-dom';
|
||||
import axios from 'axios';
|
||||
import { Button, Typography, Card, CardContent, Box, IconButton } from '@mui/material';
|
||||
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
|
||||
import UserSettings from './UserSettings';
|
||||
|
||||
const ServerSettings = () => {
|
||||
const { guildId } = useParams();
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
const [settings, setSettings] = useState({ pingCommand: false });
|
||||
const [isBotInServer, setIsBotInServer] = useState(false);
|
||||
const [clientId, setClientId] = useState(null);
|
||||
const [server, setServer] = useState(null);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch settings
|
||||
axios.get(`http://localhost:3002/api/servers/${guildId}/settings`)
|
||||
.then(response => {
|
||||
setSettings(response.data);
|
||||
});
|
||||
|
||||
// Check if bot is in server
|
||||
axios.get(`http://localhost:3002/api/servers/${guildId}/bot-status`)
|
||||
.then(response => {
|
||||
setIsBotInServer(response.data.isBotInServer);
|
||||
});
|
||||
|
||||
// Fetch client ID
|
||||
axios.get('http://localhost:3002/api/client-id')
|
||||
.then(response => {
|
||||
setClientId(response.data.clientId);
|
||||
});
|
||||
|
||||
}, [guildId, location.state]);
|
||||
|
||||
const togglePingCommand = () => {
|
||||
const newSettings = { ...settings, pingCommand: !settings.pingCommand };
|
||||
axios.post(`http://localhost:3002/api/servers/${guildId}/settings`, newSettings)
|
||||
.then(response => {
|
||||
if (response.data.success) {
|
||||
setSettings(newSettings);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
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 handleBack = () => {
|
||||
navigate('/dashboard');
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{ padding: '20px' }}>
|
||||
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||||
<IconButton onClick={handleBack} sx={{ borderRadius: '50%', boxShadow: '0 8px 16px 0 rgba(0,0,0,0.2)' }}>
|
||||
<ArrowBackIcon />
|
||||
</IconButton>
|
||||
<Box sx={{ display: 'flex', alignItems: 'center', gap: 2 }}>
|
||||
<Typography variant="h4" component="h1" sx={{ margin: 0 }}>
|
||||
{server ? `Server Settings for ${server.name}` : 'Loading...'}
|
||||
</Typography>
|
||||
{isBotInServer ? (
|
||||
<Typography>The bot is already in this server.</Typography>
|
||||
) : (
|
||||
<Button variant="contained" size="small" onClick={handleInviteBot} disabled={!clientId}>
|
||||
Invite Bot
|
||||
</Button>
|
||||
)}
|
||||
</Box>
|
||||
<UserSettings />
|
||||
</Box>
|
||||
<Card sx={{ borderRadius: '20px', boxShadow: '0 8px 16px 0 rgba(0,0,0,0.2)', marginTop: '20px' }}>
|
||||
<CardContent>
|
||||
<Typography variant="h6">Commands</Typography>
|
||||
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: '10px' }}>
|
||||
<Typography>Ping Command</Typography>
|
||||
<Button variant="contained" onClick={togglePingCommand}>
|
||||
{settings.pingCommand ? 'Disable' : 'Enable'}
|
||||
</Button>
|
||||
</Box>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card sx={{ borderRadius: '20px', boxShadow: '0 8px 16px 0 rgba(0,0,0,0.2)', marginTop: '20px' }}>
|
||||
<CardContent>
|
||||
<Typography variant="h6">Admin Commands</Typography>
|
||||
<Typography>Coming soon...</Typography>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ServerSettings;
|
||||
Reference in New Issue
Block a user