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.

76 lines
2.5 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. module.exports = {
  12. name: 'elevate',
  13. family: 'team',
  14. channels: ['734387503464710165'],
  15. description: 'Elevate a user in a team ',
  16. async execute(message) {
  17. let targets = message.mentions.members
  18. const member_id = message.member.id;
  19. if(targets.size == 0){
  20. return message.reply("You must supply at least one member!")
  21. }else if(targets.size > 20){
  22. return message.reply("You can elevate at most 20 users!")
  23. }
  24. try {
  25. await mongoose.connect( mongo_url, {useNewUrlParser: true, useUnifiedTopology: true}, () => null );
  26. }catch (error) {
  27. console.log("ERROR WITH MONGODB CONNECTION");
  28. }
  29. const user_data = await userModel.find({_id: member_id}, "team_id");
  30. if(user_data[0].team_id === "0"){
  31. return message.reply("You must be in a team to elevate a user!");
  32. }
  33. const team_data = await teamModel.find({_id: user_data[0].team_id}, "creator mods team_name member_role mod_role");
  34. const team = team_data[0]
  35. let mods = team.mods
  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");
  41. if(target_data[0].team_id == team._id){
  42. let mod_index = team.mods.indexOf(key)
  43. if(mod_index == -1){
  44. value.roles.remove(team.member_role);
  45. value.roles.add(team.mod_role);
  46. mods.push(key);
  47. let result = await teamModel.findOneAndUpdate(
  48. {_id: team._id},
  49. {mods: mods},
  50. {
  51. new: true,
  52. runValidators: true,
  53. useFindAndModify: false
  54. }
  55. );
  56. message.channel.send(`Elevated user <@${key}>`)
  57. }else{
  58. message.channel.send(`<@${key}> already elevated!`)
  59. }
  60. }else{
  61. message.channel.send(`Can't elavate user <@${key}>`);
  62. }
  63. });
  64. },
  65. };