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.

282 lines
8.3 KiB

  1. const fs = require('fs')
  2. const Discord = require('discord.js');
  3. const Client = require('./client');
  4. let mongoose = require('mongoose');
  5. const {
  6. prefix,
  7. token,
  8. mongo_url,
  9. role_message_id
  10. } = require('./config.json');
  11. const {
  12. userModel,
  13. teamModel,
  14. } = require('./commands/schemas.js');
  15. const client = new Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
  16. client.commands = new Discord.Collection();
  17. const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
  18. for (const file of commandFiles) {
  19. const command = require(`./commands/${file}`);
  20. let prefix = ""
  21. if(!command.name) continue;
  22. if(command.family){
  23. prefix = command.family + "_"
  24. }
  25. client.commands.set(prefix + command.name, command);
  26. }
  27. console.log(client.commands);
  28. client.once('ready', async () => {
  29. console.log('Ready!');
  30. async function delete_messages(){
  31. let fetched;
  32. channel = client.channels.cache.get("734387503464710165")
  33. message_manager = channel.messages
  34. do {
  35. fetched = await message_manager.fetch({ limit: 100 });
  36. fetched.delete(role_message_id)
  37. channel.bulkDelete(fetched);
  38. }
  39. while(fetched.size >= 3);
  40. }
  41. await delete_messages();
  42. var minutes = 1, interval = minutes * 60 * 1000;
  43. setInterval(delete_messages, interval);
  44. });
  45. client.once('reconnecting', () => {
  46. console.log('Reconnecting!');
  47. });
  48. client.once('disconnect', () => {
  49. console.log('Disconnect!');
  50. });
  51. client.on('message', async message => {
  52. if (message.author.bot) return;
  53. if (!message.content.startsWith(prefix)) return;
  54. const args = message.content.slice(prefix.length).split(/ +/);
  55. const c1 = args.shift().toLowerCase();
  56. let c2 = args.shift();
  57. if(c2){
  58. c2 = c2.toLowerCase()
  59. }
  60. var commandName = null;
  61. if (client.commands.has(c1)){
  62. commandName = c1;
  63. }else if(client.commands.has(c1 + "_" + c2)){
  64. commandName = c1 + "_" + c2;
  65. }else{
  66. return;
  67. }
  68. const command = client.commands.get(commandName);
  69. const permitted_roles = client.commands.get(commandName)["roles"];
  70. const permitted_channels = client.commands.get(commandName)["channels"]
  71. has_roles = false
  72. if (permitted_roles){
  73. for(i = 0; i < permitted_roles.length; i++){
  74. if(message.member.roles.cache.has(permitted_roles[i])){
  75. has_roles = true
  76. }
  77. }
  78. if (!has_roles){
  79. message.reply('You are not allowed to run this command!');
  80. return;
  81. }
  82. }
  83. if(permitted_channels){
  84. msg_channel = message.channel;
  85. channel_allowed = false
  86. for(i = 0; i < permitted_channels.length; i++){
  87. if(permitted_channels[i] == "dm" && msg_channel instanceof Discord.DMChannel){
  88. channel_allowed = true;
  89. break;
  90. }else if(permitted_channels[i] == msg_channel.id){
  91. channel_allowed = true;
  92. break;
  93. }
  94. }
  95. if(!channel_allowed){
  96. return;
  97. }
  98. }
  99. try {
  100. command.execute(message,args);
  101. } catch (error) {
  102. console.error(error);
  103. message.reply('There was an error trying to execute that command!');
  104. }
  105. });
  106. client.on('guildMemberAdd', member => {
  107. let new_user = new userModel({
  108. _id: member.id,
  109. joined_at: Date.now(),
  110. team_id: "0",
  111. profession: "",
  112. rank: 1,
  113. xp: 0,
  114. email: "",
  115. trello_id: ""
  116. })
  117. member.guild.channels.cache.get('734387938418360362').send("Welcome <@"+ member.id +">. Xi up!");
  118. member.roles.add('732558019409346603');
  119. try {
  120. mongoose.connect( mongo_url, {useNewUrlParser: true, useUnifiedTopology: true}, () => null );
  121. let db = mongoose.connection;
  122. db.once("open", function callback () {
  123. new_user.save();
  124. });
  125. }catch (error) {
  126. console.log("ERROR WITH MONGODB CONNECTION");
  127. }
  128. });
  129. const reaction_to_prof = {
  130. "🔐" : ["734453192921710604" ,"Cryptology"],
  131. "🙃" : ["734453691318403072" ,"Reverse Engineer"],
  132. "📱" : ["734453796306026576" ,"Mobile Developer"],
  133. "🔩" : ["734453830669959218" ,"Hardware Developer"],
  134. "🕸️" : ["734453917345120316" ,"Web security Expert"],
  135. "🏭" : ["734453944700502186" ,"Backend Developer"],
  136. "🕷️" : ["734454002082775040" ,"Web developer"],
  137. "🌐" : ["734454060417024072" ,"Network Engineer"],
  138. "👮" : ["734454093833175151" ,"Network Security "],
  139. "🐧" : ["734454127551053924" ,"Linux Guru"]
  140. };
  141. const limits = {
  142. 1 : 1,
  143. 2 : 4,
  144. 3 : 6,
  145. 4 : 0,
  146. 5 : 0
  147. }
  148. client.on('messageReactionAdd', async (reaction, user) => {
  149. if(reaction.message.id === role_message_id){
  150. if (reaction.partial) {
  151. try {
  152. await reaction.fetch();
  153. } catch (error) {
  154. console.log('Something went wrong when fetching the message: ', error);
  155. return;
  156. }
  157. }
  158. let emoji = reaction.emoji.name;
  159. let member = await reaction.message.guild.members.fetch(user.id);
  160. try {
  161. mongoose.connect( mongo_url, {useNewUrlParser: true, useUnifiedTopology: true}, () => null );
  162. let db = mongoose.connection;
  163. db.once("open", async function callback () {
  164. let user_data = await userModel.find({_id: user.id}, "profession rank")
  165. let profession = user_data[0].profession
  166. let rank = user_data[0].rank
  167. if(limits[rank] < profession.length + 1 && limits[rank] !== 0){
  168. user.send(`You cannot have more than ${limits[rank]} professions at this rank!`)
  169. return;
  170. }
  171. profession.push(reaction_to_prof[emoji][1]);
  172. member.roles.add( reaction_to_prof[emoji][0]);
  173. userModel.findOneAndUpdate(
  174. {_id: user.id},
  175. {profession: profession},
  176. {
  177. new: true,
  178. runValidators: true,
  179. useFindAndModify: false
  180. }, function(err, result) {
  181. //if (err) {console.log(err); } else {console.log(result);}
  182. });
  183. user.send(`You now have the profession ${reaction_to_prof[emoji][1]}!`)
  184. });
  185. }catch (error) {
  186. console.log(error)
  187. console.log("ERROR WITH MONGODB CONNECTION");
  188. }
  189. }
  190. });
  191. client.on('messageReactionRemove', async (reaction, user) => {
  192. if(reaction.message.id === role_message_id){
  193. if (reaction.partial) {
  194. try {
  195. await reaction.fetch();
  196. } catch (error) {
  197. console.log('Something went wrong when fetching the message: ', error);
  198. return;
  199. }
  200. }
  201. let emoji = reaction.emoji.name;
  202. let member = await reaction.message.guild.members.fetch(user.id);
  203. try {
  204. mongoose.connect( mongo_url, {useNewUrlParser: true, useUnifiedTopology: true}, () => null );
  205. let db = mongoose.connection;
  206. db.once("open", async function callback () {
  207. let user_data = await userModel.find({_id: user.id}, "profession rank")
  208. let profession = user_data[0].profession
  209. let rank = user_data[0].rank
  210. if(profession_index != -1){
  211. profession.splice(profession_index, 1)
  212. }
  213. member.roles.remove(reaction_to_prof[emoji][0]);
  214. userModel.findOneAndUpdate(
  215. {_id: user.id},
  216. {profession: profession},
  217. {
  218. new: true,
  219. runValidators: true,
  220. useFindAndModify: false
  221. }, function(err, result) {
  222. // if (err) {console.log(err); } else {console.log(result);}
  223. });
  224. });
  225. user.send(`You now don't have the profession ${reaction_to_prof[emoji][1]}!`)
  226. }catch (error) {
  227. console.log(error)
  228. console.log("ERROR WITH MONGODB CONNECTION");
  229. }
  230. }
  231. });
  232. // login to Discord with your app's token
  233. client.login(token);