You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.9 KiB

  1. const {
  2. mongo_url,
  3. trello_token,
  4. trello_key
  5. } = require('../config.json');
  6. const {
  7. userModel,
  8. teamModel,
  9. } = require('./schemas.js');
  10. let mongoose = require('mongoose');
  11. var Trello = require('trello-node-api')(trello_key, trello_token);
  12. const fetch = require('node-fetch');
  13. const { strict } = require('assert');
  14. const { parse } = require('path');
  15. module.exports = {
  16. name: 'quit',
  17. family: 'team',
  18. channels: ['734387503464710165'],
  19. description: 'Quit from a team',
  20. async execute(message) {
  21. var member_id = message.member.id;
  22. try {
  23. await mongoose.connect( mongo_url, {useNewUrlParser: true, useUnifiedTopology: true}, () => null);
  24. }catch (error) {
  25. console.log("could not connect");
  26. }
  27. const user_data = await userModel.find({_id: member_id},"team_id trello_id email");
  28. const team_id = user_data[0].team_id
  29. if(team_id == '0'){
  30. return message.reply('You must be in a team to quit!');
  31. }
  32. const filter = { _id : member_id };
  33. const update = { team_id : '0' };
  34. var def = await userModel.findOneAndUpdate(filter, update,{
  35. new: true,
  36. runValidators: true,
  37. useFindAndModify: false
  38. });
  39. var teamdata = await teamModel.find({ _id :team_id },'member_role mod_role text_channel voice_channel mods board_id');
  40. var team = teamdata[0];
  41. var mods = team.mods;
  42. let mod_index = team.mods.indexOf(member_id)
  43. if(mod_index === -1){
  44. message.member.roles.remove(team.member_role)
  45. }else{
  46. message.member.roles.remove(team.mod_role)
  47. mods.splice(mod_index, 1)
  48. }
  49. teamModel.findOneAndUpdate({_id :team_id},{mods : mods});
  50. if(user_data[0].email){
  51. await fetch(`https://api.trello.com/1/boards/${team.board_id}/members/${user_data[0].trello_id}?key=${trello_key}&token=${trello_token}`, {
  52. method: 'DELETE'
  53. })
  54. .catch(err => console.error(err));
  55. }
  56. let team_members = await userModel.find({team_id: team_id})
  57. if(team_members.length == 0){
  58. message.channel.send("No users left in this team. Deleting");
  59. message.guild.roles.fetch(team.member_role).then((role) => role.delete());
  60. message.guild.roles.fetch(team.mod_role).then((role) => role.delete());
  61. message.client.channels.fetch(team.text_channel).then((channel) => channel.delete());
  62. message.client.channels.fetch(team.voice_channel).then((channel) => channel.delete());
  63. team.mods = mods;
  64. let old_team = new teamModel(team);
  65. old_team.remove();
  66. Trello.board.del(team.board_id)
  67. .catch(function (error) {
  68. console.log('error', error);
  69. });
  70. }
  71. return message.reply('Done!');
  72. },
  73. };