Polarcraft/discord-bot/routes/Team.js

146 lines
4.6 KiB
JavaScript
Raw Normal View History

2023-05-11 19:46:59 +02:00
const express = require('express');
const index = require('../index');
const { PermissionsBitField, ChannelType } = require('discord.js');
const router = express.Router();
router.post('/createchannels', async (req, res) => {
const { name, discordId } = req.body;
2023-05-27 12:02:33 +02:00
if (!name || !discordId) return res.status(400).send({ error: 'Name en discordId zijn vereist' });
2023-05-11 19:46:59 +02:00
try {
const guild = await index.client.guilds.fetch(process.env.GUILD_ID);
2023-05-27 12:02:33 +02:00
2023-05-11 19:46:59 +02:00
const category = await guild.channels.fetch(process.env.TEAM_CATEGORY_ID);
2023-05-27 12:02:33 +02:00
const member = await guild.members.fetch(discordId);
2023-05-11 19:46:59 +02:00
const textChannel = await guild.channels.create({
name: name,
type: ChannelType.GuildText,
parent: category,
permissionOverwrites: [
{
id: guild.id,
deny: [PermissionsBitField.Flags.ViewChannel],
},
{
id: member.id,
2023-05-27 12:02:33 +02:00
allow: [PermissionsBitField.Flags.ViewChannel],
},
],
2023-05-11 19:46:59 +02:00
});
const voiceChannel = await guild.channels.create({
name: name,
type: ChannelType.GuildVoice,
parent: category,
permissionOverwrites: [
{
id: guild.id,
deny: [PermissionsBitField.Flags.ViewChannel],
},
{
id: member.id,
2023-05-27 12:02:33 +02:00
allow: [PermissionsBitField.Flags.ViewChannel],
},
],
2023-05-11 19:46:59 +02:00
});
res.send({ textChannel, voiceChannel });
} catch (e) {
console.log(e);
2023-05-27 12:02:33 +02:00
return res.status(500).send({ error: 'Error tijdens het maken van discord channels' });
2023-05-11 19:46:59 +02:00
}
});
router.post('/deletechannels', async (req, res) => {
const { textChannelId, voiceChannelId } = req.body;
2023-05-27 12:02:33 +02:00
if (!textChannelId, !voiceChannelId) return res.status(400).send({ error: 'textChannelId en voiceChannelId zijn vereist' });
2023-05-11 19:46:59 +02:00
try {
const guild = await index.client.guilds.fetch(process.env.GUILD_ID);
2023-05-27 12:02:33 +02:00
2023-05-11 19:46:59 +02:00
const textChannel = await guild.channels.fetch(textChannelId);
const voiceChannel = await guild.channels.fetch(voiceChannelId);
2023-05-27 12:02:33 +02:00
await textChannel.delete();
await voiceChannel.delete();
2023-05-11 19:46:59 +02:00
res.send({ status: 'success' });
} catch (e) {
console.log(e);
2023-05-27 12:02:33 +02:00
return res.status(500).send({ error: 'Error tijdens het verwijderen van discord channels' });
2023-05-11 19:46:59 +02:00
}
});
router.post('/removeteammember', async (req, res) => {
const { textChannelId, voiceChannelId, discordId } = req.body;
2023-05-27 12:02:33 +02:00
if (!textChannelId, !voiceChannelId, !discordId) return res.status(400).send({ error: 'textChannelId, voiceChannelId en discordId zijn vereist' });
2023-05-11 19:46:59 +02:00
try {
const guild = await index.client.guilds.fetch(process.env.GUILD_ID);
2023-05-27 12:02:33 +02:00
const member = await guild.members.fetch(discordId);
2023-05-11 19:46:59 +02:00
const textChannel = await guild.channels.fetch(textChannelId);
const voiceChannel = await guild.channels.fetch(voiceChannelId);
2023-05-27 12:02:33 +02:00
await textChannel.permissionOverwrites.delete(member);
await voiceChannel.permissionOverwrites.delete(member);
2023-05-11 19:46:59 +02:00
res.send({ status: 'success' });
} catch (e) {
console.log(e);
2023-05-27 12:02:33 +02:00
return res.status(500).send({ error: 'Error tijdens het verwijderen van een team member' });
2023-05-11 19:46:59 +02:00
}
});
router.post('/addteammember', async (req, res) => {
const { textChannelId, voiceChannelId, discordId } = req.body;
2023-05-27 12:02:33 +02:00
if (!textChannelId, !voiceChannelId, !discordId) return res.status(400).send({ error: 'textChannelId, voiceChannelId en discordId zijn vereist' });
2023-05-11 19:46:59 +02:00
try {
const guild = await index.client.guilds.fetch(process.env.GUILD_ID);
2023-05-27 12:02:33 +02:00
const member = await guild.members.fetch(discordId);
2023-05-11 19:46:59 +02:00
const textChannel = await guild.channels.fetch(textChannelId);
const voiceChannel = await guild.channels.fetch(voiceChannelId);
2023-05-27 12:02:33 +02:00
await textChannel.permissionOverwrites.edit(member, { ViewChannel: true });
await voiceChannel.permissionOverwrites.edit(member, { ViewChannel: true });
2023-05-11 19:46:59 +02:00
res.send({ status: 'success' });
} catch (e) {
console.log(e);
2023-05-27 12:02:33 +02:00
return res.status(500).send({ error: 'Error tijdens het toevoegen van team member' });
2023-05-11 19:46:59 +02:00
}
});
router.post('/edit', async (req, res) => {
const { textChannelId, voiceChannelId, name } = req.body;
2023-05-27 12:02:33 +02:00
if (!textChannelId, !voiceChannelId, !name) return res.status(400).send({ error: 'textChannelId, voiceChannelId en name zijn vereist' });
2023-05-11 19:46:59 +02:00
try {
const guild = await index.client.guilds.fetch(process.env.GUILD_ID);
2023-05-27 12:02:33 +02:00
2023-05-11 19:46:59 +02:00
const textChannel = await guild.channels.fetch(textChannelId);
const voiceChannel = await guild.channels.fetch(voiceChannelId);
2023-05-27 12:02:33 +02:00
await textChannel.edit({ name: name });
await voiceChannel.edit({ name: name });
2023-05-11 19:46:59 +02:00
res.send({ status: 'success' });
} catch (e) {
console.log(e);
2023-05-27 12:02:33 +02:00
return res.status(500).send({ error: 'Error tijds het veranderen van Discord channel naam' });
2023-05-11 19:46:59 +02:00
}
});
module.exports = router;