setting prefix from web now actually works

This commit is contained in:
JandereDev 2022-02-01 07:39:12 +01:00
parent 27a984fecc
commit c0439a3930
No known key found for this signature in database
GPG key ID: 5D5E18ACB990F57A
3 changed files with 62 additions and 5 deletions

View file

@ -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) => { app.put('/dash/server/:server/:option', async (req: Request, res: Response) => {
try { try {
const user = await isAuthenticated(req, res, true); const user = await isAuthenticated(req, res, true);
if (!user) return unauthorized(res); if (!user) return;
const { server } = req.params; const { server } = req.params;
const { item } = req.body; 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); const permissionLevelRes = await getPermissionLevel(user, server);
if (!permissionLevelRes.success) if (!permissionLevelRes.success)
@ -53,6 +53,7 @@ app.put('/dash/server/:server/:option', async (req: Request, res: Response) => {
switch(req.params.option) { switch(req.params.option) {
case 'managers': { 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.' }); 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 }); const userRes = await botReq('getUser', { user: item });
@ -75,6 +76,7 @@ app.put('/dash/server/:server/:option', async (req: Request, res: Response) => {
} }
case 'mods': { 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.' }); if (permissionLevel < 2) return res.status(403).send({ error: 'You are not allowed to add other moderators.' });
const userRes = await botReq('getUser', { user: item }); const userRes = await botReq('getUser', { user: item });
@ -96,9 +98,46 @@ app.put('/dash/server/:server/:option', async (req: Request, res: Response) => {
return; 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); default: return badRequest(res);
} }
} catch(e: any) { } catch(e: any) {
console.error(e);
res.status(500).send({ error: e }); res.status(500).send({ error: e });
} }
}); });

View file

@ -6,7 +6,7 @@ import { botReq } from '../internal/ws';
type Server = { id: string, perms: 0|1|2|3, name: string, iconURL?: string, bannerURL?: string } type Server = { id: string, perms: 0|1|2|3, name: string, iconURL?: string, bannerURL?: string }
app.get('/dash/servers', async (req: Request, res: Response) => { 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); if (!user) return unauthorized(res);
const response = await botReq('getUserServers', { user }); const response = await botReq('getUserServers', { user });

View file

@ -30,6 +30,7 @@ const ServerDashboard: FunctionComponent = () => {
const [serverInfo, setServerInfo] = useState({} as Server); const [serverInfo, setServerInfo] = useState({} as Server);
const [status, setStatus] = useState(''); const [status, setStatus] = useState('');
const [changed, setChanged] = useState({} as { prefix?: boolean, prefixAllowSpace?: boolean });
const [prefix, setPrefix] = useState('' as string|undefined); const [prefix, setPrefix] = useState('' as string|undefined);
const [prefixAllowSpace, setPrefixAllowSpace] = useState(false); const [prefixAllowSpace, setPrefixAllowSpace] = useState(false);
@ -39,8 +40,23 @@ const ServerDashboard: FunctionComponent = () => {
const { serverid } = useParams(); const { serverid } = useParams();
const saveConfig = useCallback(async () => { const saveConfig = useCallback(async () => {
alert('server config saved (not really)'); if (Object.values(changed).filter(i => i).length == 0) return;
}, [ prefix, prefixAllowSpace ]);
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 () => { const loadInfo = useCallback(async () => {
try { try {
@ -78,6 +94,7 @@ const ServerDashboard: FunctionComponent = () => {
value={prefix} value={prefix}
onChange={e => { onChange={e => {
setPrefix(e.currentTarget.value); setPrefix(e.currentTarget.value);
setChanged({ ...changed, prefix: true });
}} }}
/> />
<Checkbox <Checkbox
@ -85,6 +102,7 @@ const ServerDashboard: FunctionComponent = () => {
value={prefixAllowSpace} value={prefixAllowSpace}
onChange={() => { onChange={() => {
setPrefixAllowSpace(!prefixAllowSpace); setPrefixAllowSpace(!prefixAllowSpace);
setChanged({ ...changed, prefixAllowSpace: true });
}} }}
title="Allow space after prefix" 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.'} description={'Whether the bot recognizes a command if the prefix is followed by a space. Enable if your prefix is a word.'}