1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
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: 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;
}
|