diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index dd69ba658..281edf624 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -46,6 +46,7 @@ 1. Install [Mongodb Community Edition](https://docs.mongodb.com/manual/administration/install-community/) and ensure that it is running 1. Optional - Install [Mongodb-compass](https://www.mongodb.com/try/download/compass?tck=docs_compass). This tool can be used to see and manipulate your data visually. + 1. To connect, type `mongodb://localhost:27017` in the connection string box and press connect. The monkeytype database will be created and shown` after the server is started. ## Building and Running diff --git a/backend/server.js b/backend/server.js index b944ffda5..6e1e92941 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1752,6 +1752,117 @@ app.get("/getLeaderboard/:type/:mode/:mode2", (req, res) => { ); }); +// BOT API +// Might want to move this to a seperate file and add some sort of middleware that can send error if the user is not found + +app.get("/getBananas/:discordId", (req, res) => { + User.findOne({ discordId: req.params.discordId }, (err, user) => { + if (user) { + res.send({ t60bananas: user.bananas.t60bananas }); + } else { + res.send({ t60bananas: 0, message: "User not found" }); + } + }); +}); + +app.get("/getUserDiscordData/:uid", (req, res) => { + //for announceDailyLbResult + User.findOne({ uid: req.body.uid }, (err, user) => { + res.send({ name: user.name, discordId: user.discordId }); + return; + }); +}); + +app.get("/getUserPbs/:discordId", (req, res) => { + //for fix wpm role + User.findOne({ discordId: req.params.discordId }, (err, user) => { + if (user) { + res.send({ personalBests: user.personalBests }); + return; + } else { + res.send({ error: "No user found with that id" }); + return; + } + }); +}); + +app.get("/getUserPbsByUid/:uid", (req, res) => { + //for verify + User.findOne({ uid: req.params.uid }, (err, user) => { + if (user) { + res.send({ personalBests: user.personalBests }); + return; + } else { + res.send({ error: "No user found with that id" }); + return; + } + }); +}); + +app.get("/getTimeLeaderboard/:mode2/:type", (req, res) => { + //for lb + Leaderboard.findOne({ + mode: "time", + mode2: req.params.mode2, + type: req.params.type, + }).then((err, lb) => { + //get top 10 leaderboard + lb.board.length = 10; + res.send({ board: lb.board }); + return; + }); +}); + +app.get("/getUserByDiscordId/:discordId", (req, res) => { + //for lb + User.findOne({ discordId: req.params.discordId }, (err, user) => { + if (user) { + res.send({ uid: user.uid }); + } else { + res.send({ error: "No user found with that id" }); + } + return; + }); +}); + +app.get("/getRecentScore/:discordId", (req, res) => { + User.findOne({ discordId: req.params.discordId }, (err, user) => { + if (user) { + if (user.results.length == 0) { + res.send({ recentScore: -1 }); + } else { + res.send({ recentScore: user.results[user.results.length - 1] }); + } + } else { + res.send({ error: "No user found with that id" }); + } + return; + }); +}); + +app.get("/getUserStats/:discordId", (req, res) => { + //for stats + User.findOne({ discordId: req.params.discordId }, (err, user) => { + if (user) { + res.send({ stats: user.globalStats }); + } else { + res.send({ error: "No user found with that id" }); + } + return; + }); +}); + +app.post("/newBotCommand", (req, res) => { + let newBotCommand = new BotCommand({ + command: req.body.command, //is always "updateRole" + arguments: req.body.arguments, + executed: req.body.executed, //is always false + requestTimestamp: req.body.requestTimestamp, + }); + newBotCommand.save(); + res.status(200); +}); + // LISTENER app.listen(port, () => { console.log(`Listening to requests on http://localhost:${port}`);