aboutsummaryrefslogtreecommitdiff
path: root/bot/api/server.js
diff options
context:
space:
mode:
authorAndrew Lee <andrew@alee14.me>2025-03-02 16:24:26 -0500
committerAndrew Lee <andrew@alee14.me>2025-03-02 16:24:26 -0500
commit1c253d25cb1d35aa987d76e07806999c562712d6 (patch)
tree02655b6a3bed5b3604b56deb4c2af199f2609a64 /bot/api/server.js
parentf98f7e6a34f02e8d6ea6673fbe68ab6db28a2e89 (diff)
downloadAleeBot-1c253d25cb1d35aa987d76e07806999c562712d6.tar.gz
AleeBot-1c253d25cb1d35aa987d76e07806999c562712d6.tar.bz2
AleeBot-1c253d25cb1d35aa987d76e07806999c562712d6.zip
Bringing more features from 2.x; ESLint; API
Diffstat (limited to 'bot/api/server.js')
-rw-r--r--bot/api/server.js75
1 files changed, 75 insertions, 0 deletions
diff --git a/bot/api/server.js b/bot/api/server.js
new file mode 100644
index 0000000..ac4f8ca
--- /dev/null
+++ b/bot/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}`);
+ });
+};
+