aboutsummaryrefslogtreecommitdiff
path: root/bot/src/api/routes
diff options
context:
space:
mode:
Diffstat (limited to 'bot/src/api/routes')
-rw-r--r--bot/src/api/routes/settings.js62
1 files changed, 62 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;
+}