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.

102 lines
3.6 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. const fetch = require('node-fetch');
  12. module.exports = {
  13. name: 'kick',
  14. family: 'team',
  15. channels: ['734387503464710165'],
  16. description: 'Add user to team',
  17. async execute(message) {
  18. let targets = message.mentions.members
  19. const member_id = message.member.id;
  20. if(targets.size == 0){
  21. return message.reply("You must supply at least one member!")
  22. }else if(targets.size > 20){
  23. return message.reply("You can add at most 20 users!")
  24. }
  25. try {
  26. await mongoose.connect( mongo_url, {useNewUrlParser: true, useUnifiedTopology: true}, () => null );
  27. }catch (error) {
  28. console.log("ERROR WITH MONGODB CONNECTION");
  29. }
  30. const user_data = await userModel.find({_id: member_id}, "team_id");
  31. if(user_data[0].team_id === "0"){
  32. return message.reply("You must be in a team to kick a user!");
  33. }
  34. const team_data = await teamModel.find({_id: user_data[0].team_id},
  35. "creator board_id mods team_name member_role mod_role");
  36. const team = team_data[0]
  37. if(member_id !== team.creator && team.mods.indexOf(member_id) === -1){
  38. return message.reply("You do not have enough privileges for this action!")
  39. }
  40. let mods = team.mods
  41. targets.forEach(async(value, key, map) => {
  42. if(key == member_id){
  43. return message.reply("You cannot kick yourself!")
  44. }else if (key == team.creator){
  45. return message.reply("You cannot kick the creator!")
  46. }
  47. let target_data = await userModel.find({_id: key}, "team_id trello_id email");
  48. if(target_data[0].team_id == team._id){
  49. let mod_index = team.mods.indexOf(key)
  50. if(mod_index == -1){
  51. value.roles.remove(team.member_role)
  52. }else{
  53. value.roles.remove(team.mod_role)
  54. mods.splice(mod_index, 1)
  55. let result = await teamModel.findOneAndUpdate(
  56. {_id: team._id},
  57. {mods: mods},
  58. {
  59. new: true,
  60. runValidators: true,
  61. useFindAndModify: false
  62. }
  63. );
  64. }
  65. let result = await userModel.findOneAndUpdate(
  66. {_id: key},
  67. {team_id: "0"},
  68. {
  69. new: true,
  70. runValidators: true,
  71. useFindAndModify: false
  72. }
  73. );
  74. message.client.users.fetch(key).then((user) => user.send(`You have been kicked from the team ${team.team_name}`))
  75. if(target_data[0].email){
  76. console.log("Deleting user from trello")
  77. await fetch(`https://api.trello.com/1/boards/${team.board_id}/members/${target_data[0].trello_id}?key=${trello_key}&token=${trello_token}`, {
  78. method: 'DELETE'
  79. })
  80. .catch(err => console.error(err));
  81. }
  82. message.channel.send(`Kicked user <@${key}>`)
  83. }else{
  84. message.channel.send(`Can't kick user <@${key}>`);
  85. }
  86. });
  87. },
  88. };