aboutsummaryrefslogtreecommitdiff
path: root/bot/src/api/server.js
diff options
context:
space:
mode:
authorAndrew Lee <andrew@alee14.me>2025-03-02 22:27:57 -0500
committerAndrew Lee <andrew@alee14.me>2025-03-02 22:27:57 -0500
commitcf1382d88c5e3298923c8cb243b7bc5751e68b53 (patch)
treea38351317594ed660fc80c784cdf7dbc71f5656b /bot/src/api/server.js
parent1c253d25cb1d35aa987d76e07806999c562712d6 (diff)
downloadAleeBot-cf1382d88c5e3298923c8cb243b7bc5751e68b53.tar.gz
AleeBot-cf1382d88c5e3298923c8cb243b7bc5751e68b53.tar.bz2
AleeBot-cf1382d88c5e3298923c8cb243b7bc5751e68b53.zip
Ported more commands; Made embed colour a const; Cleanup
Diffstat (limited to 'bot/src/api/server.js')
-rw-r--r--bot/src/api/server.js75
1 files changed, 75 insertions, 0 deletions
diff --git a/bot/src/api/server.js b/bot/src/api/server.js
new file mode 100644
index 0000000..ac4f8ca
--- /dev/null
+++ b/bot/src/api/server.js
@@ -0,0 +1,75 @@
+import express from 'express';
+import cors from 'cors';
+
+import 'dotenv/config';
+import { readFileSync } from 'node:fs';
+
+const app = express();
+
+export const apiServer = (client) => {
+ app.use(cors()); // Allow cross-origin requests
+ app.use(express.json());
+
+ app.get('/api/version', (req, res) => {
+ const { version } = JSON.parse(readFileSync('./package.json', 'utf-8'));
+ res.json({
+ version: version
+ });
+
+ });
+
+ app.get('/api/uptime', (req, res) => {
+ res.json({
+ uptime: client.uptime
+ });
+ });
+
+ app.get('/api/servers', (req, res) => {
+ const guildsInfo = [];
+
+ if (client.guilds.cache.size === 0) {
+ res.json({
+ message: 'No servers found'
+ });
+ } else {
+ client.guilds.cache.forEach((guild) => {
+ const guildInfo = {
+ name: guild.name,
+ members: guild.memberCount,
+ id: guild.id
+ };
+ guildsInfo.push(guildInfo);
+ });
+ }
+
+ res.json(guildsInfo);
+
+ });
+
+ app.post('/api/leave', (req, res) => {
+ const { id } = req.body;
+ let guild = client.guilds.cache.get(id);
+
+ try {
+ guild.leave().then(guild => {
+ res.json({
+ guild: guild.name,
+ left: true
+ });
+ });
+
+ } catch (error) {
+ console.error('Error leaving server:', error);
+ res.status(500).res.json({
+ guild: guild.name,
+ left: false
+ });
+ }
+ });
+
+ // Start the server
+ app.listen(process.env.port, () => {
+ console.log(`[i] Starting API at http://localhost:${process.env.port}`);
+ });
+};
+