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.

93 lines
3.1 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. module.exports = {
  14. name: 'email',
  15. channels: ['dm'],
  16. description: 'Update the email address of the user',
  17. async execute(message) {
  18. let email = message.content.split(" ")[1];
  19. if(!email){
  20. return message.reply("You must supply an email!")
  21. }
  22. if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
  23. return message.reply("You must supply an email address, not some other input!.");
  24. }
  25. var member_id = message.author.id;
  26. try {
  27. await mongoose.connect( mongo_url, {useNewUrlParser: true, useUnifiedTopology: true}, () => null );
  28. }catch (error) {
  29. console.log("ERROR WITH MONGODB CONNECTION");
  30. }
  31. const user_data = await userModel.find({_id: member_id}, "team_id email trello_id");
  32. if(user_data.length == 0){
  33. return message.reply("You should be a member of the ξ.network discord channel to run this command!")
  34. }
  35. if(user_data[0].email && user_data[0].team_id !== 0){
  36. console.log("Deleting user")
  37. let team_data = await teamModel.find({_id: user_data[0].team_id}, "board_id");
  38. await fetch(`https://api.trello.com/1/boards/${team_data[0].board_id}/members/${user_data[0].trello_id}?key=${trello_key}&token=${trello_token}`, {
  39. method: 'DELETE'
  40. })
  41. .catch(err => console.error(err));
  42. }
  43. if(user_data[0].team_id !== "0"){
  44. let team_data = await teamModel.find({_id: user_data[0].team_id}, "board_id creator");
  45. let type = "normal";
  46. if(team_data[0].creator == member_id){
  47. type = "admin"
  48. }
  49. let addition = await fetch(`https://api.trello.com/1/boards/${team_data[0].board_id}/members?key=${trello_key}&token=${trello_token}&email=${email}&type=${type}`, {
  50. method: 'PUT',
  51. })
  52. .catch(err => console.error(err));
  53. let data = await addition.json()
  54. let result = await userModel.findOneAndUpdate(
  55. {_id: member_id},
  56. {email: email, trello_id: data.memberships[data.memberships.length - 1].idMember},
  57. {
  58. new: true,
  59. runValidators: true,
  60. useFindAndModify: false
  61. }
  62. );
  63. }else{
  64. userModel.findOneAndUpdate(
  65. {_id: member_id},
  66. {email: email},
  67. {
  68. new: true,
  69. runValidators: true,
  70. useFindAndModify: false
  71. }
  72. );
  73. }
  74. message.reply("Email set successfully! You should receive an invite email from trello if you are a member of a team.")
  75. },
  76. };