aboutsummaryrefslogtreecommitdiff
path: root/bot/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'bot/src/api')
-rw-r--r--bot/src/api/routes/settings.js62
-rw-r--r--bot/src/api/server.js2
2 files changed, 64 insertions, 0 deletions
diff --git a/bot/src/api/routes/settings.js b/bot/src/api/routes/settings.js
new file mode 100644
index 0000000..68d8745
--- /dev/null
+++ b/bot/src/api/routes/settings.js
@@ -0,0 +1,62 @@
+import { ChannelType } from 'discord.js';
+import { Router } from 'express';
+import { guildSettings } from '../../models/guild-settings.js';
+
+export function settingsRouter(client) {
+ const router = Router();
+
+ router.get('/settings/guild', async (req, res) => {
+ try {
+ const { guildID } = req.body;
+ if (!guildID) return res.status(400).send('Guild ID not provided');
+ const settings = await guildSettings.findOne({ where: { guildID: guildID } });
+ res.json(settings);
+ } catch (e) {
+ console.error('Error fetching settings:', e);
+ res.status(500).send('Internal Server Error');
+ }
+ });
+
+ router.get('/settings/guild/:id', async (req, res) => {
+ try {
+ const settings = await guildSettings.findOne({ where: { guildID: req.params.id } });
+
+ let channels = [];
+
+ client.guilds.cache.get(settings.guildID).channels.cache
+ .filter((channel) => channel.type === ChannelType.GuildText)
+ .forEach((channel) => {
+ const channelInfo = {
+ name: channel.name,
+ id: channel.id,
+ category: channel.parent ? channel.parent.name : 'No Category'
+ };
+
+ channels.push(channelInfo);
+ });
+
+ res.json(channels);
+ } catch (e) {
+ console.error('Error fetching settings:', e);
+ res.status(500).send('Internal Server Error');
+ }
+ });
+
+ router.post('/settings/guild', async (req, res) => {
+ try {
+ const { guildID, ...newSettings } = req.body;
+ const [updated] = await guildSettings.update(newSettings, { where: { guildID: guildID } });
+ if (updated) {
+ const updatedSettings = await guildSettings.findOne({ where: { guildID: guildID } });
+ res.json(updatedSettings);
+ } else {
+ res.status(404).send('Settings not found');
+ }
+ } catch (e) {
+ console.error('Error updating settings:', e);
+ res.status(500).send('Internal Server Error');
+ }
+ });
+
+ return router;
+}
diff --git a/bot/src/api/server.js b/bot/src/api/server.js
index 0b0397e..74d8511 100644
--- a/bot/src/api/server.js
+++ b/bot/src/api/server.js
@@ -5,6 +5,7 @@ import 'dotenv/config';
import { readFileSync } from 'node:fs';
import { quoteRouter } from './routes/quotes.js';
+import { settingsRouter } from './routes/settings.js';
const app = express();
@@ -13,6 +14,7 @@ export const apiServer = (client) => {
app.use(express.json());
app.use('/api', quoteRouter);
+ app.use('/api', settingsRouter(client));
app.get('/api/version', (req, res) => {
const { version } = JSON.parse(readFileSync('./package.json', 'utf-8'));