From c0439a39303be416370fc9351c6490c68c376174 Mon Sep 17 00:00:00 2001 From: JandereDev Date: Tue, 1 Feb 2022 07:39:12 +0100 Subject: [PATCH] setting prefix from web now actually works --- api/src/routes/dash/server.ts | 43 +++++++++++++++++++++++++++++-- api/src/routes/dash/servers.ts | 2 +- web/src/pages/ServerDashboard.tsx | 22 ++++++++++++++-- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/api/src/routes/dash/server.ts b/api/src/routes/dash/server.ts index 9793f02..eecaf67 100644 --- a/api/src/routes/dash/server.ts +++ b/api/src/routes/dash/server.ts @@ -37,11 +37,11 @@ app.get('/dash/server/:server', async (req: Request, res: Response) => { app.put('/dash/server/:server/:option', async (req: Request, res: Response) => { try { const user = await isAuthenticated(req, res, true); - if (!user) return unauthorized(res); + if (!user) return; const { server } = req.params; const { item } = req.body; - if (!server || typeof server != 'string' || !item || typeof item != 'string') return badRequest(res); + if (!server || typeof server != 'string') return badRequest(res); const permissionLevelRes = await getPermissionLevel(user, server); if (!permissionLevelRes.success) @@ -53,6 +53,7 @@ app.put('/dash/server/:server/:option', async (req: Request, res: Response) => { switch(req.params.option) { case 'managers': { + if (!item || typeof item != 'string') return badRequest(res); if (permissionLevel < 3) return res.status(403).send({ error: 'You are not allowed to add other bot managers.' }); const userRes = await botReq('getUser', { user: item }); @@ -75,6 +76,7 @@ app.put('/dash/server/:server/:option', async (req: Request, res: Response) => { } case 'mods': { + if (!item || typeof item != 'string') return badRequest(res); if (permissionLevel < 2) return res.status(403).send({ error: 'You are not allowed to add other moderators.' }); const userRes = await botReq('getUser', { user: item }); @@ -96,9 +98,46 @@ app.put('/dash/server/:server/:option', async (req: Request, res: Response) => { return; } + case 'config': { + function validateField(field: string, type: string[], level: 0|1|2|3): boolean { + if (permissionLevel < level) { + res.status(403).send({ error: `You are not authorized to change '${field}'` }); + return false; + } + + if (req.body?.[field] != undefined && !type.includes(typeof req.body?.[field])) { + res.status(400).send({ error: `Field '${field}' needs to be of type ${type} or null` }); + return false; + } + + return true; + } + + type RequestBody = { + prefix?: string, + spaceAfterPrefix?: boolean, + } + + if (!validateField('prefix', ['string'], 2) || + !validateField('spaceAfterPrefix', ['boolean'], 2) + ) return; + + const body: RequestBody = req.body; + + await db.get('servers').update({ id: server }, { + $set: JSON.parse(JSON.stringify({ // Get rid of undefined fields + prefix: body.prefix || null, + spaceAfterPrefix: body.spaceAfterPrefix, + })), + }); + + return res.send({ success: true }); + } + default: return badRequest(res); } } catch(e: any) { + console.error(e); res.status(500).send({ error: e }); } }); diff --git a/api/src/routes/dash/servers.ts b/api/src/routes/dash/servers.ts index 4504c8c..fd36317 100644 --- a/api/src/routes/dash/servers.ts +++ b/api/src/routes/dash/servers.ts @@ -6,7 +6,7 @@ import { botReq } from '../internal/ws'; type Server = { id: string, perms: 0|1|2|3, name: string, iconURL?: string, bannerURL?: string } app.get('/dash/servers', async (req: Request, res: Response) => { - const user = await isAuthenticated(req, res, true); + const user = await isAuthenticated(req); if (!user) return unauthorized(res); const response = await botReq('getUserServers', { user }); diff --git a/web/src/pages/ServerDashboard.tsx b/web/src/pages/ServerDashboard.tsx index 322a21c..5f4747c 100644 --- a/web/src/pages/ServerDashboard.tsx +++ b/web/src/pages/ServerDashboard.tsx @@ -30,6 +30,7 @@ const ServerDashboard: FunctionComponent = () => { const [serverInfo, setServerInfo] = useState({} as Server); const [status, setStatus] = useState(''); + const [changed, setChanged] = useState({} as { prefix?: boolean, prefixAllowSpace?: boolean }); const [prefix, setPrefix] = useState('' as string|undefined); const [prefixAllowSpace, setPrefixAllowSpace] = useState(false); @@ -39,8 +40,23 @@ const ServerDashboard: FunctionComponent = () => { const { serverid } = useParams(); const saveConfig = useCallback(async () => { - alert('server config saved (not really)'); - }, [ prefix, prefixAllowSpace ]); + if (Object.values(changed).filter(i => i).length == 0) return; + + const payload = { + ...(changed.prefix ? { prefix } : undefined), + ...(changed.prefixAllowSpace ? { spaceAfterPrefix: prefixAllowSpace } : undefined), + } + + const res = await axios.put( + API_URL + `/dash/server/${serverid}/config`, + payload, + { headers: await getAuthHeaders() } + ); + + if (res.data.success) { + setChanged({}); + } + }, [ prefix, prefixAllowSpace, changed ]); const loadInfo = useCallback(async () => { try { @@ -78,6 +94,7 @@ const ServerDashboard: FunctionComponent = () => { value={prefix} onChange={e => { setPrefix(e.currentTarget.value); + setChanged({ ...changed, prefix: true }); }} /> { value={prefixAllowSpace} onChange={() => { setPrefixAllowSpace(!prefixAllowSpace); + setChanged({ ...changed, prefixAllowSpace: true }); }} title="Allow space after prefix" description={'Whether the bot recognizes a command if the prefix is followed by a space. Enable if your prefix is a word.'}