const express = require('express');
const index = require('../index');

const router = express.Router();

router.post('/changenickname', async (req, res) => {
  const { nickname, discordId } = req.body;

  if (!nickname || !discordId) return res.status(400).send({ error: 'Nickname en discordId zijn vereist' });

  const nick = nickname.length > 32 ? nickname.slice(0, 32) : nickname;

  try {
    const guild = await index.client.guilds.fetch(process.env.GUILD_ID);

    const member = await guild.members.fetch(discordId);

    await member.edit({ nick: nick });
  } catch (e) {
    console.log(e);
    return res.status(500).send({ error: 'Error tijds het veranderen van de nickname' });
  }

  res.send({ status: 'success' });
});

router.post('/ban', async (req, res) => {
  const { discordId, reason } = req.body;

  if (!reason || !discordId) return res.status(400).send({ error: 'Reason en discordId zijn vereist' });

  try {
    const guild = await index.client.guilds.fetch(process.env.GUILD_ID);

    const member = await guild.members.fetch(discordId);

    await member.ban({ deleteMessageSeconds: 24 * 3600, reason: reason });
  } catch (e) {
    console.log(e);
    return res.status(500).send({ error: 'Error tijds het bannen van gebruiker' });
  }

  res.send({ status: 'success' });
});


module.exports = router;
