Skip to content

Commit

Permalink
WebAPI: Run and return requested user-data
Browse files Browse the repository at this point in the history
  • Loading branch information
cataclym committed Aug 26, 2024
1 parent 95e72e5 commit 1e89615
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/lib/Kaiki/KaikiSapphireClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import { container } from "@sapphire/pieces";
import NeofetchCommand from "../../commands/Fun/neofetch";
import DiscordBotListService from "../DiscordBotList/DiscordBotListService";
import express from "express";
import * as https from "node:https";

export default class KaikiSapphireClient<Ready extends true>
extends SapphireClient<Ready>
Expand Down Expand Up @@ -148,6 +147,8 @@ export default class KaikiSapphireClient<Ready extends true>
`Bot owner: ${colorette.greenBright(client.owner.username)}`
);

this.WebListener();

await Promise.all([
client.filterOptionalCommands(),
client.sendOnlineMsg(),
Expand Down Expand Up @@ -431,8 +432,19 @@ export default class KaikiSapphireClient<Ready extends true>
this.logger.info(`WebListener server is listening on port: ${process.env.SELF_API_PORT}`);

const app = express();
app.get("/API/UserCache/:id", async (req, res) => {
res.send({ title: "hello" })
app.get("/API/UserCache/:id", (req, res, next) => {
if (Number.isNaN(req.params.id)) return next(new Error("Invalid format"));

const requestedUser = this.users.cache.get(req.params.id);

if (!requestedUser) return next(new Error("User not found"));

return res.send({
user: requestedUser,
guilds: this.guilds.cache
.filter(guild => guild.members.cache.has(requestedUser.id))
.map(guild => ({ id: guild.id, name: guild.name }))
});
})

app.listen(process.env.SELF_API_PORT)
Expand Down

0 comments on commit 1e89615

Please sign in to comment.