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.

288 lines
8.5 KiB

5 years ago
5 years ago
5 years ago
  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(profession.indexOf(reaction_to_prof[emoji][1]) != -1){
  168. return;
  169. }
  170. if(limits[rank] < profession.length + 1 && limits[rank] !== 0){
  171. user.send(`You cannot have more than ${limits[rank]} professions at this rank!`)
  172. return;
  173. }
  174. profession.push(reaction_to_prof[emoji][1]);
  175. member.roles.add( reaction_to_prof[emoji][0]);
  176. userModel.findOneAndUpdate(
  177. {_id: user.id},
  178. {profession: profession},
  179. {
  180. new: true,
  181. runValidators: true,
  182. useFindAndModify: false
  183. }, function(err, result) {
  184. //if (err) {console.log(err); } else {console.log(result);}
  185. });
  186. user.send(`You now have the profession ${reaction_to_prof[emoji][1]}!`)
  187. });
  188. }catch (error) {
  189. console.log(error)
  190. console.log("ERROR WITH MONGODB CONNECTION");
  191. }
  192. }
  193. });
  194. client.on('messageReactionRemove', async (reaction, user) => {
  195. if(reaction.message.id === role_message_id){
  196. if (reaction.partial) {
  197. try {
  198. await reaction.fetch();
  199. } catch (error) {
  200. console.log('Something went wrong when fetching the message: ', error);
  201. return;
  202. }
  203. }
  204. let emoji = reaction.emoji.name;
  205. let member = await reaction.message.guild.members.fetch(user.id);
  206. try {
  207. mongoose.connect( mongo_url, {useNewUrlParser: true, useUnifiedTopology: true}, () => null );
  208. let db = mongoose.connection;
  209. db.once("open", async function callback () {
  210. let user_data = await userModel.find({_id: user.id}, "profession rank")
  211. let profession = user_data[0].profession
  212. let rank = user_data[0].rank
  213. let profession_index = profession.indexOf(reaction_to_prof[emoji][1])
  214. if(profession_index != -1){
  215. profession.splice(profession_index, 1)
  216. }else{
  217. return;
  218. }
  219. member.roles.remove(reaction_to_prof[emoji][0]);
  220. userModel.findOneAndUpdate(
  221. {_id: user.id},
  222. {profession: profession},
  223. {
  224. new: true,
  225. runValidators: true,
  226. useFindAndModify: false
  227. }, function(err, result) {
  228. // if (err) {console.log(err); } else {console.log(result);}
  229. });
  230. });
  231. user.send(`You now don't have the profession ${reaction_to_prof[emoji][1]}!`)
  232. }catch (error) {
  233. console.log(error)
  234. console.log("ERROR WITH MONGODB CONNECTION");
  235. }
  236. }
  237. });
  238. // login to Discord with your app's token
  239. client.login(token);