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.

89 lines
3.4 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: 'add',
  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 add a user!");
  33. }
  34. const team_data = await teamModel.find({_id: user_data[0].team_id}, "creator mods team_name member_role board_id");
  35. const team = team_data[0]
  36. if(member_id !== team.creator && team.mods.indexOf(member_id) === -1){
  37. return message.reply("You do not have enough privileges for this action!")
  38. }
  39. targets.forEach(async(value, key, map) => {
  40. let target_data = await userModel.find({_id: key}, "team_id email");
  41. if(target_data[0].team_id === "0"){
  42. value.roles.add(team.member_role)
  43. let result = await userModel.findOneAndUpdate(
  44. {_id: key},
  45. {team_id: team._id},
  46. {
  47. new: true,
  48. runValidators: true,
  49. useFindAndModify: false
  50. }
  51. );
  52. message.client.users.fetch(key).then((user) => user.send(`You have been added to the team ${team.team_name}`))
  53. if(!target_data[0].email){
  54. message.client.users.fetch(key).then((user) => user.send("You need an email to access the team's trello board! Add your email after the !email prefix to set your email address."))
  55. }else{
  56. let addition = await fetch(`https://api.trello.com/1/boards/${team.board_id}/members?key=${trello_key}&token=${trello_token}&email=${target_data[0].email}&type=normal`, {
  57. method: 'PUT'
  58. })
  59. .catch(err => console.error(err));
  60. let data = await addition.json()
  61. let result = await userModel.findOneAndUpdate(
  62. {_id: key},
  63. {email: target_data[0].email, trello_id: data.memberships[data.memberships.length - 1].idMember},
  64. {
  65. new: true,
  66. runValidators: true,
  67. useFindAndModify: false
  68. }
  69. );
  70. }
  71. message.channel.send(`Added user <@${key}>`)
  72. }else{
  73. message.channel.send(`Can't add user <@${key}>`);
  74. }
  75. });
  76. },
  77. };