183 lines
5.3 KiB
JavaScript
183 lines
5.3 KiB
JavaScript
require('dotenv').config({ path: __dirname + '/.env' });
|
|
const express = require('express');
|
|
const cors = require('cors');
|
|
|
|
const app = express();
|
|
const port = process.env.PORT || 3001;
|
|
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
const axios = require('axios');
|
|
|
|
app.get('/auth/discord', (req, res) => {
|
|
const url = `https://discord.com/api/oauth2/authorize?client_id=${process.env.DISCORD_CLIENT_ID}&redirect_uri=${encodeURIComponent('http://localhost:3002/auth/discord/callback')}&response_type=code&scope=identify%20guilds`;
|
|
res.redirect(url);
|
|
});
|
|
|
|
app.get('/auth/discord/callback', async (req, res) => {
|
|
const code = req.query.code;
|
|
if (!code) {
|
|
return res.status(400).send('No code provided');
|
|
}
|
|
|
|
try {
|
|
const params = new URLSearchParams();
|
|
params.append('client_id', process.env.DISCORD_CLIENT_ID);
|
|
params.append('client_secret', process.env.DISCORD_CLIENT_SECRET);
|
|
params.append('grant_type', 'authorization_code');
|
|
params.append('code', code);
|
|
params.append('redirect_uri', 'http://localhost:3002/auth/discord/callback');
|
|
|
|
const response = await axios.post('https://discord.com/api/oauth2/token', params, {
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
});
|
|
|
|
const { access_token } = response.data;
|
|
|
|
const userResponse = await axios.get('https://discord.com/api/users/@me', {
|
|
headers: {
|
|
Authorization: `Bearer ${access_token}`,
|
|
},
|
|
});
|
|
|
|
const guildsResponse = await axios.get('https://discord.com/api/users/@me/guilds', {
|
|
headers: {
|
|
Authorization: `Bearer ${access_token}`,
|
|
},
|
|
});
|
|
|
|
const adminGuilds = guildsResponse.data.filter(guild => (guild.permissions & 0x8) === 0x8);
|
|
|
|
const user = userResponse.data;
|
|
const db = readDb();
|
|
user.theme = db.users && db.users[user.id] ? db.users[user.id].theme : 'light';
|
|
const guilds = adminGuilds;
|
|
res.redirect(`http://localhost:3000/dashboard?user=${encodeURIComponent(JSON.stringify(user))}&guilds=${encodeURIComponent(JSON.stringify(guilds))}`);
|
|
} catch (error) {
|
|
console.error('Error during Discord OAuth2 callback:', error);
|
|
res.status(500).send('Internal Server Error');
|
|
}
|
|
});
|
|
|
|
const { readDb, writeDb } = require('./db');
|
|
|
|
app.get('/api/servers/:guildId/settings', (req, res) => {
|
|
const { guildId } = req.params;
|
|
const db = readDb();
|
|
const settings = db[guildId] || { pingCommand: false };
|
|
res.json(settings);
|
|
});
|
|
|
|
app.post('/api/servers/:guildId/settings', (req, res) => {
|
|
const { guildId } = req.params;
|
|
const { pingCommand } = req.body;
|
|
const db = readDb();
|
|
db[guildId] = { pingCommand };
|
|
writeDb(db);
|
|
res.json({ success: true });
|
|
});
|
|
|
|
app.get('/api/servers/:guildId/bot-status', (req, res) => {
|
|
const { guildId } = req.params;
|
|
const guild = bot.client.guilds.cache.get(guildId);
|
|
if (guild) {
|
|
res.json({ isBotInServer: true });
|
|
} else {
|
|
res.json({ isBotInServer: false });
|
|
}
|
|
});
|
|
|
|
app.get('/api/client-id', (req, res) => {
|
|
res.json({ clientId: process.env.DISCORD_CLIENT_ID });
|
|
});
|
|
|
|
app.post('/api/user/theme', (req, res) => {
|
|
const { userId, theme } = req.body;
|
|
const db = readDb();
|
|
if (!db.users) {
|
|
db.users = {};
|
|
}
|
|
if (!db.users[userId]) {
|
|
db.users[userId] = {};
|
|
}
|
|
db.users[userId].theme = theme;
|
|
writeDb(db);
|
|
res.json({ success: true });
|
|
});
|
|
|
|
app.post('/api/servers/:guildId/leave', async (req, res) => {
|
|
const { guildId } = req.params;
|
|
try {
|
|
const guild = await bot.client.guilds.fetch(guildId);
|
|
if (guild) {
|
|
await guild.leave();
|
|
res.json({ success: true });
|
|
} else {
|
|
res.status(404).json({ success: false, message: 'Bot is not in the specified server' });
|
|
}
|
|
} catch (error) {
|
|
console.error('Error leaving server:', error);
|
|
res.status(500).json({ success: false, message: 'Internal Server Error' });
|
|
}
|
|
});
|
|
|
|
app.get('/api/servers/:guildId/channels', async (req, res) => {
|
|
const { guildId } = req.params;
|
|
const guild = bot.client.guilds.cache.get(guildId);
|
|
if (!guild) {
|
|
return res.json([]);
|
|
}
|
|
try {
|
|
const channels = await guild.channels.fetch();
|
|
const textChannels = channels.filter(channel => channel.type === 0).map(channel => ({ id: channel.id, name: channel.name }));
|
|
res.json(textChannels);
|
|
} catch (error) {
|
|
console.error('Error fetching channels:', error);
|
|
res.status(500).json({ success: false, message: 'Internal Server Error' });
|
|
}
|
|
});
|
|
|
|
app.get('/api/servers/:guildId/welcome-leave-settings', (req, res) => {
|
|
const { guildId } = req.params;
|
|
const db = readDb();
|
|
const settings = db[`${guildId}_welcome_leave`] || {
|
|
welcome: {
|
|
enabled: false,
|
|
channel: '',
|
|
message: 'Welcome to the server, {user}!',
|
|
customMessage: '',
|
|
},
|
|
leave: {
|
|
enabled: false,
|
|
channel: '',
|
|
message: '{user} has left the server.',
|
|
customMessage: '',
|
|
},
|
|
};
|
|
res.json(settings);
|
|
});
|
|
|
|
app.post('/api/servers/:guildId/welcome-leave-settings', (req, res) => {
|
|
const { guildId } = req.params;
|
|
const newSettings = req.body;
|
|
const db = readDb();
|
|
db[`${guildId}_welcome_leave`] = newSettings;
|
|
writeDb(db);
|
|
res.json({ success: true });
|
|
});
|
|
|
|
app.get('/', (req, res) => {
|
|
res.send('Hello from the backend!');
|
|
});
|
|
|
|
const bot = require('../discord-bot');
|
|
|
|
bot.login();
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Server is running on port ${port}`);
|
|
});
|