jillo-bot/src/web.ts

36 lines
949 B
TypeScript
Raw Normal View History

2023-11-17 21:11:50 +01:00
import express from 'express';
import * as log from './lib/log';
2023-11-18 00:55:39 +01:00
import { CustomItem, db } from './lib/db';
import { defaultItems } from './lib/rpg/items';
2023-11-18 13:49:50 +01:00
import { Client } from 'discord.js';
2023-11-17 21:11:50 +01:00
2023-11-18 13:49:50 +01:00
export async function startServer(bot: Client, port: number) {
2023-11-17 21:11:50 +01:00
const app = express();
app.use(express.static('static/'));
2023-11-18 00:55:39 +01:00
app.get('/api/items', async (req, res) => {
const guildID = req.query.guild;
let customItems : Partial<CustomItem>[];
if (guildID) {
customItems = await db<CustomItem>('customItems')
.select('emoji', 'name', 'id', 'description')
.where('guild', guildID)
.limit(25);
} else {
customItems = [];
}
res.json([...defaultItems, ...customItems]);
});
2023-11-18 13:49:50 +01:00
app.get('/api/status', async (_, res) => {
res.json({
guilds: bot.guilds.cache.size,
uptime: bot.uptime
});
});
2023-11-17 21:11:50 +01:00
app.listen(port, () => log.info(`web interface listening on ${port}`));
}