const express = require("express"); const { guildMap } = require("../common"); const { dangerousAdminMode } = require("../config"); const { checkAuth, createToken } = require("../tokens"); const router = express(); router.get("/", (req, res) => { res.status(200).send({ error: false, message: "SUCCESS_API_OK" }); }); router.post("/tokens/create", async (req, res) => { if (!dangerousAdminMode) return res.status(403).send({ error: true, message: "ERROR_FEATURE_DISABLED" }); const { username, avatarURL, discordID, guildAccess } = req.body; if (!username || !discordID || !guildAccess) return res.status(400).send({ error: true, message: "ERROR_BAD_REQUEST" }); try { const token = await createToken({ username, avatarURL, discordID, guildAccess }); res.status(200).send({ error: false, message: "SUCCESS_TOKEN_CREATED", token }); } catch(e) { res.status(500).send({ error: true, message: "ERROR_TOKEN_CREATE_FAILURE" }); } }); router.post("/guilds/:guildId/channels/:channelId/messages/create", checkAuth(async (req, res) => { const messageContent = req.body.content; if (!messageContent) return res.status(400).send({ error: true, message: "ERROR_NO_MESSAGE_CONTENT" }); const guildId = req.params.guildId; if (!guildId) return res.status(400).send({ error: true, message: "ERROR_NO_GUILD_ID" }); const channelId = req.params.channelId; if (!channelId) return res.status(400).send({ error: true, message: "ERROR_NO_CHANNEL_ID" }); const { username, avatarURL, guildAccess } = req.user; if (guildAccess.indexOf(guildId) === -1) return res.status(403).send({ error: true, message: "ERROR_NO_GUILD_ACCESS" }); const guild = guildMap.get(guildId); try { await guild.discordSendMessage(messageContent, channelId, username, avatarURL); res.status(201).send({ error: false, message: "SUCCESS_MESSAGE_CREATED" }); } catch(e) { console.error("server main: api: message create: error: ", e); res.status(500).send({ error: true, message: "ERROR_MESSAGE_SEND_FAILURE" }); } })); module.exports = router;