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.

80 lines
2.7 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: 'lower',
  13. family: 'team',
  14. channel: ['734387503464710165'],
  15. description: 'Lower 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 lower 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 lower 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. if(key == member_id){
  43. return message.reply("You cannot lower yourself!")
  44. }else if (key == team.creator){
  45. return message.reply("You cannot lower the creator!")
  46. }
  47. if(mod_index !== -1){
  48. value.roles.add(team.member_role);
  49. value.roles.remove(team.mod_role);
  50. mods.splice(mod_index, 1)
  51. let result = await teamModel.findOneAndUpdate(
  52. {_id: team._id},
  53. {mods: mods},
  54. {
  55. new: true,
  56. runValidators: true,
  57. useFindAndModify: false
  58. }
  59. );
  60. message.channel.send(`Lowered user <@${key}>`)
  61. }else{
  62. message.channel.send(`<@${key}> not a mod!`)
  63. }
  64. }else{
  65. message.channel.send(`Can't lower user <@${key}>`);
  66. }
  67. });
  68. },
  69. };