Advertisement
Guest User

Untitled

a guest
May 5th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.00 KB | None | 0 0
  1. const SteamEncryption = require("./steamencryption.js");
  2. const SteamIDConvert = require("steamidconvert")();
  3. const CountryCodes = require("country-data").countries;
  4. const entities = require('html-entities').AllHtmlEntities;
  5. const Discord = require("discord.js");
  6. const request = require("request");
  7. const MySQL = require("mysql");
  8.  
  9. const AdminServ = {};
  10. AdminServ.bot = new Discord.Client();
  11.  
  12. var sf_jar = request.jar();
  13.  
  14. function numberWithCommas(x)
  15. {
  16. return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  17. }
  18.  
  19. function escapeRegExp(str)
  20. {
  21. return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  22. }
  23.  
  24. AdminServ.channels = {};
  25. AdminServ.channels.general = "general";
  26.  
  27. AdminServ.facepunch_url = "1515567";
  28.  
  29. AdminServ.forums_channel = "general";
  30. AdminServ.forums_interval = 10000;
  31.  
  32. AdminServ.username = "richard_tanner";
  33. AdminServ.password = "richXg#88500";
  34.  
  35. AdminServ.botsecret = "MzA4MzY0OTAyMDMxMDMyMzMx.C-gkew.-UgkTie-IzLjh26aAlhdRpf9XaI";
  36.  
  37. AdminServ.useragent = "Tom the Bot";
  38. AdminServ.userGame = "dasdsad"
  39. AdminServ.prefix = "!"
  40.  
  41. AdminServ.database = MySQL.createConnection(
  42. {
  43. host : "localhost",
  44. user : "gmodsto.re",
  45. database: "gmodsto.re",
  46. });
  47.  
  48. AdminServ.escape_markdown = function(str, escape) {
  49. var r = new RegExp(escapeRegExp(escape),"g");
  50. return str.replace(r,escape.replace(/(.)/g,"\\$1"));
  51. };
  52.  
  53. AdminServ.api = function(path,shortcut,cb) {
  54. request.get({url:"https://gmodstore.com/api/" + path,headers:{"User-Agent":AdminServ.useragent},jar:sf_jar},function(err,httpobj,body) {
  55. if (err) {console.log(err);return;}
  56. body = JSON.parse(body);
  57. if (shortcut) {
  58. cb(body[shortcut]);
  59. } else {
  60. cb(body);
  61. }
  62. });
  63. };
  64.  
  65. AdminServ.script = function(message,id) {
  66. AdminServ.api("scripts/info/" + id,"script",function(data) {
  67. var msg = "";
  68.  
  69. msg += "**" + AdminServ.escape_markdown(data.name,"**") + "**";
  70.  
  71. AdminServ.api("scripts/reviews/" + id,"reviews",function(r_data) {
  72. var total = 0;
  73.  
  74. for(var i=0;i < r_data.length;i++) {
  75. total += Number(r_data[i].review_rating);
  76. }
  77.  
  78. if (total != 0) {
  79. total = total / r_data.length;
  80. }
  81.  
  82. var stars = "";
  83. if (total > 4.5) {
  84. stars = "★★★★★";
  85. } else if (total > 4) {
  86. stars = "★★★★☆";
  87. } else if (total > 3) {
  88. stars = "★★★☆☆";
  89. } else if (total > 2) {
  90. stars = "★★☆☆☆";
  91. } else if (total > 1) {
  92. stars = "★☆☆☆☆";
  93. } else if (total == 0) {
  94. stars = "☆☆☆☆☆";
  95. }
  96.  
  97. msg += "\n(" + +total.toFixed(2) + " - " + stars + ")";
  98.  
  99. msg += "\n\nBy " + data.author_name + " (" + SteamIDConvert.convertToText(data.author_steam64) + ")";
  100.  
  101. var price = data.price_discount;
  102. if (price == "0.00") {
  103. price = data.price;
  104. }
  105.  
  106. request.get({url:"https://gmodstore.com/scripts/view/" + id,headers:{"User-Agent":AdminServ.useragent},jar:sf_jar},function(err,httpobj,body) {
  107. if (err) {console.log(err);return;}
  108. var sales = /<strong>(\d+)<\/strong> sales/.exec(body)[1];
  109.  
  110. msg += "\n\nViews: " + numberWithCommas(data.views);
  111. msg += "\nPrice: $" + price;
  112. msg += "\nSales: " + numberWithCommas(Number(sales));
  113.  
  114. message.channel.sendMessage(msg);
  115. });
  116. });
  117. });
  118. };
  119.  
  120. AdminServ.job = function(message,id) {
  121. request.get({url:"https://gmodstore.com/jobs/view/" + id,headers:{"User-Agent":AdminServ.useragent},jar:sf_jar},function(err,httpobj,body) {
  122. if (err) {console.log(err);return;}
  123. var title = /<div class="row">\n<div class="col-md-12">\n<h1>(.*?)<\/h1>\n<\/div>\n<\/div>/.exec(body);
  124. var budget = /<tr>\n<td>Budget<\/td>\n<td>\$(.*?)<\/td>\n<\/tr>/.exec(body);
  125. var user = /<h2><a href="\/users\/view\/(\d+)"><i class="fa fa-.*?"><\/i><span style=""> (.*?) <span class="reputation".*?>(.*?)<\/span>/.exec(body);
  126. var doing = /<div class="alert alert-success" role="alert">\nThis job is being done by someone else, but you can still apply for it\n<\/div>/.exec(body);
  127. var done = /<div class="alert alert-success" role="alert">\nThis job has been done by someone else, you can no longer apply for it\n<\/div>/.exec(body);
  128. var license = /<div class="well license-header">\nThis job is using the "<a href=".*?">(.*?)<\/a>" license.\n<\/div>/.exec(body);
  129. var apps = /<span.*?><strong>(\d+)<\/strong> applications<\/span>/.exec(body);
  130. var views = /<tr>\n<td>Views<\/td>\n<td>(\d+)<\/td>\n<\/tr>/.exec(body);
  131.  
  132. if (title && budget && license && apps && views) {
  133.  
  134. var msg = "";
  135.  
  136. msg += "**" + AdminServ.escape_markdown(title[1],"**") + "**";
  137. if (doing) {
  138. msg += " (In progress)";
  139. } else if (done) {
  140. msg += " (Done)";
  141. }
  142.  
  143. if (user) {
  144. msg += "\n\nBy: " + user[2] + " (" + SteamIDConvert.convertToText(user[1]) + ") [" + user[3] + " rep]";
  145. }
  146.  
  147. msg += "\n\nBudget: $" + budget[1];
  148. msg += "\n\nApplications: " + apps[1];
  149.  
  150. msg += "\n\nViews: " + numberWithCommas(views[1]);
  151. msg += "\nLicense: " + license[1];
  152.  
  153. message.channel.sendMessage(msg);
  154.  
  155. }
  156. });
  157. };
  158.  
  159. AdminServ.shortlink = function(message,link) {
  160. AdminServ.database.query("SELECT `clicks`,`steamid64` FROM `links` WHERE `id`=" + AdminServ.database.escape(link),function(err,link_rows) {
  161. if (err) {console.log(err);return;}
  162. if (link_rows.length == 1) {
  163. AdminServ.database.query("SELECT `steamtable`,`lastupdated` FROM `users` WHERE `steamid64`=" + AdminServ.database.escape(link_rows[0].steamid64),function(err,user_rows) {
  164. if (err) {console.log(err);return;}
  165. var name = SteamIDConvert.convertToText(link_rows[0].steamid64);
  166. if (user_rows.length == 1) {
  167. if (user_rows[0].steamtable == "null" || user_rows[0].steamtable == null) {
  168.  
  169. }
  170. var steamtable = JSON.parse(user_rows[0].steamtable);
  171. name = steamtable.personaname;
  172. }
  173. message.channel.sendMessage("That link was created by **" + AdminServ.escape_markdown(name,"**") + "** and has been clicked " + link_rows[0].clicks + " times.");
  174. });
  175. }
  176. });
  177. };
  178.  
  179. var usergroups = [
  180.  
  181. "User",
  182. "Developer",
  183. "Staff",
  184. "Administrator",
  185.  
  186. ];
  187. var days = [
  188. "Mon",
  189. "Tue",
  190. "Wed",
  191. "Thu",
  192. "Fri",
  193. "Sat",
  194. "Sun",
  195. ];
  196. var months = [
  197. "Jan",
  198. "Feb",
  199. "Mar",
  200. "Apr",
  201. "May",
  202. "Jun",
  203. "Jul",
  204. "Aug",
  205. "Sep",
  206. "Oct",
  207. "Nov",
  208. "Dec",
  209. ];
  210. AdminServ.user = function(message,steamid64) {
  211. AdminServ.api("users/search/steam64/" + steamid64,"user",function(data) {
  212. var msg = "";
  213.  
  214. msg += "https://" + data.avatar.replace(/_full.jpg$/,"_medium.jpg") + "\n\n";
  215.  
  216. msg += "**" + AdminServ.escape_markdown(data.name,"**") + "** (" + SteamIDConvert.convertToText(steamid64) + ") (" + usergroups[Number(data.usergroup)] + ")";
  217.  
  218. if (CountryCodes[data.country_code]) {
  219. msg += "\n\nFrom " + CountryCodes[data.country_code].name;
  220. }
  221.  
  222. if (Number(data.scripts_rep) > 0) {
  223. msg += "\n\nScript Reputation: " + numberWithCommas(data.scripts_rep);
  224. }
  225. if (Number(data.jobs_rep) > 0) {
  226. msg += "\nJob Reputation: " + numberWithCommas(data.jobs_rep);
  227. }
  228.  
  229. AdminServ.api("users/banned/" + steamid64,"bans",function(data) {
  230. // need to do
  231.  
  232. message.channel.sendMessage(msg);
  233. });
  234. });
  235. };
  236.  
  237. AdminServ.forums = function() {
  238. function check() {
  239.  
  240. request.get({url:"https://gmodstore.com/community/",headers:{"User-Agent":AdminServ.useragent},jar:sf_jar},function(err,httpobj,body) {
  241. if (err) {console.log(err);return;}
  242. var r = /(?:<div class="forum-category-header">\n<span>(.*?)<\/span>[\s\S]*?)?<a href="\/community\/boards\/[\s\S]*?">(?!\n)([\s\S]*?)(?!\n)<\/a>[\s\S]*?<td class="forum-category-lastpost">[\s\S]*?<a href="(\/community\/threads\/.*?\/post-(.*?)(?:#post-.*?)?)" class="forum-category-lastpost-title">([\s\S]*?)<\/a>\n<div class="forum-category-lastpost-meta">\n<a href="\/users\/view\/(.*?)">(.*?)<\/a>[\s\S]*?<date>.*?>(.*?)</g;
  243. var latest_posts_n = body.match(r);
  244. if (latest_posts_n) {
  245. latest_posts_n = latest_posts_n.length;
  246. var latest_posts = [];
  247. var latest_posts_r;
  248. for(var i=0;i < latest_posts_n;i++) {
  249. latest_posts_r = r.exec(body);
  250. latest_posts.push(latest_posts_r);
  251. delete latest_posts[latest_posts.length - 1].input;
  252. delete latest_posts[latest_posts.length - 1].index;
  253. delete latest_posts[latest_posts.length - 1][0];
  254. }
  255.  
  256. var cur_forum;
  257.  
  258. for(i=0;i < latest_posts.length;i++) {
  259.  
  260. var board;
  261. var url;
  262. var id;
  263. var thread;
  264. var user;
  265. var username;
  266. var time;
  267.  
  268. if (latest_posts[i][1]) {
  269. cur_forum = entities.decode(latest_posts[i][1]);
  270. }
  271.  
  272. board = entities.decode(latest_posts[i][2]);
  273. url = entities.decode(latest_posts[i][3]);
  274. id = entities.decode(latest_posts[i][4]);
  275. thread = entities.decode(latest_posts[i][5]);
  276. user = entities.decode(latest_posts[i][6]);
  277. username = entities.decode(latest_posts[i][7]);
  278. time = entities.decode(latest_posts[i][8]);
  279.  
  280. if (thread == "") continue;
  281. if (cur_forum == "The BotAdminServ Of The Forums") continue;
  282.  
  283. AdminServ.database.query("SELECT `id` FROM `bot_forums` WHERE `board`=? AND `forum`=?",[board,cur_forum],(function(cur_forum,board,url,id,thread,user,username,time) {
  284. return function(err,rows) {
  285. if (err) {console.log(err);return;}
  286.  
  287. if (rows.length == 0) {
  288.  
  289. //AdminServ.database.query("INSERT INTO `bot_forums` (`forum`,`board`,`id`) VALUES(?,?,?)",[cur_forum,board,id]);
  290.  
  291. } else if (rows[0].id != id) {
  292.  
  293. AdminServ.channels.community.sendMessage("```\n" + cur_forum + " -> " + board + " -> " + thread + " • " + time + "\n\n" + username + " • " + SteamIDConvert.convertToText(user) + "\n```\nhttps://scriptfodder.com" + url);
  294.  
  295. //AdminServ.database.query("UPDATE `bot_forums` SET `id`=? WHERE `forum`=? AND `board`=?",[id,cur_forum,board]);
  296.  
  297. }
  298. };
  299. })(cur_forum,board,url,id,thread,user,username,time));
  300.  
  301. }
  302. }
  303. });
  304.  
  305. request.get({url:"https://facepunch.com/showthread.php?t=" + AdminServ.facepunch_url + "&page=200",headers:{"User-Agent":AdminServ.useragent}},function(err,httpobj,body) {
  306. if (err) {console.log(err);return;}
  307. var posts = /<span class="date" title=".*?">(.*?)<\/span>[\s\S]*?<a name="post(\d+)".*? Post #(\d+)[\s\S]*?<div class="userinfo user_(\d+).*?>[\s\S]*?<a class="username.*">(.*?)<\/span><\/a>/g;
  308. if (posts) {
  309. var latest_posts_n = body.match(posts);
  310. if (latest_posts_n) {
  311. latest_posts_n = latest_posts_n.length;
  312. var latest_posts = [];
  313. var latest_posts_r;
  314. for(var i=0;i < latest_posts_n;i++) {
  315. latest_posts_r = posts.exec(body);
  316. latest_posts.push(latest_posts_r);
  317. delete latest_posts[latest_posts.length - 1].input;
  318. delete latest_posts[latest_posts.length - 1].index;
  319. delete latest_posts[latest_posts.length - 1][0];
  320. }
  321. latest_posts = latest_posts[latest_posts.length - 1];
  322.  
  323. var time = latest_posts[1];
  324. var global_post_id = latest_posts[2];
  325. var thread_post_id = latest_posts[3];
  326. var user_id = latest_posts[4];
  327. var username = latest_posts[5];
  328.  
  329. AdminServ.database.query("SELECT `id` FROM `bot_forums` WHERE `board`='Facepunch' AND `forum`='Facepunch'",function(err,rows) {
  330. if (err) {console.log(err);return;}
  331.  
  332. if (rows.length == 0) {
  333.  
  334. AdminServ.database.query("INSERT INTO `bot_forums` (`forum`,`board`,`id`) VALUES('Facepunch','Facepunch',?)",[thread_post_id]);
  335.  
  336. } else if (rows[0].id != thread_post_id) {
  337.  
  338. AdminServ.channels.community.sendMessage("```\nFacepunch Thread • " + time + "\n\n" + username + " (#" + user_id + ")\n```\nhttps://facepunch.com/showthread.php?t=" + AdminServ.facepunch_url + "&p=" + global_post_id + "&viewfull=1#post" + global_post_id);
  339.  
  340. AdminServ.database.query("UPDATE `bot_forums` SET `id`=? WHERE `forum`='Facepunch' AND `board`='Facepunch'",[thread_post_id]);
  341.  
  342. }
  343. });
  344. }
  345. }
  346. });
  347.  
  348. }
  349. setInterval(check,AdminServ.forums_interval);
  350. check();
  351. };
  352.  
  353. AdminServ.bot.on("userUpdate",function(oldUser,newUser) {
  354. //AdminServ.database.query("INSERT INTO `bot_discordids` (`id`,`username`,`nickname`) VALUES(?,?,?) ON DUPLICATE KEY UPDATE `id`=?, `username`=?, `nickname`=?",[newUser.id,newUser.username,newUser.nickname,newUser.id,newUser.username,newUser.nickname]);
  355. });
  356.  
  357. AdminServ.bot.on("message",message => {
  358. if (message.author.id == AdminServ.bot.user.id) {return;}
  359.  
  360.  
  361. //AdminServ.database.query("INSERT INTO `bot_discordlog` (`channel`,`discord`,`datetime`,`message`) VALUES(?,?,CURRENT_TIMESTAMP,?)",[message.channel.name,message.author.id,message.content]);
  362.  
  363. var script = /gmodstore\.com\/scripts\/view\/(\d+)/g;
  364. var user = /gmodstore\.com\/users\/view\/(\d+)/g;
  365. var job = /gmodstore\.com\/jobs\/view\/(\d+)/g;
  366. var usrsearch = /search\/(\d+)/g;
  367. var shortlink = /https?:\/\/gmodsto\.re\/([a-zA-Z0-9-$%^*(),'\";:\|#@~`=_]+)/g;
  368.  
  369. var matches = 0;
  370.  
  371. if (message.content.match(script)) {
  372. if (message.content.match(script).length == 1) {
  373. matches += 1;
  374. }
  375. }
  376.  
  377. if (message.content.match(usrsearch)) {
  378. if (message.content.match(usrsearch).length == 1) {
  379. matches += 1;
  380. }
  381. }
  382.  
  383. if (message.content.match(user)) {
  384. if (message.content.match(user).length == 1) {
  385. matches += 1;
  386. }
  387. }
  388. if (message.content.match(job)) {
  389. if (message.content.match(job).length == 1) {
  390. matches += 1;
  391. }
  392. }
  393. if (message.content.match(shortlink)) {
  394. if (message.content.match(shortlink).length == 1) {
  395. matches += 1;
  396. }
  397. }
  398. if (matches > 1 || matches == 0) {
  399. return;
  400. }
  401.  
  402. script = script.exec(message.content);
  403. if (script) {
  404. AdminServ.script(message,script[1]);
  405. }
  406.  
  407. user = user.exec(message.content);
  408. if (user) {
  409. AdminServ.user(message,user[1]);
  410. }
  411.  
  412. usrsearch = usrsearch.exec(message.content);
  413. if (usrsearch) {
  414. AdminServ.channels.general.sendMessage(" Yup");
  415. }
  416.  
  417. job = job.exec(message.content);
  418. if (job) {
  419. AdminServ.job(message,job[1]);
  420. }
  421.  
  422. shortlink = shortlink.exec(message.content);
  423. if (shortlink) {
  424. AdminServ.shortlink(message,shortlink[1]);
  425. }
  426. });
  427.  
  428. AdminServ.bot.on("guildMemberAdd",function(_,member) {
  429. member.sendMessage("Welcome to the GModStore Discord. Please read the #welcome channel.\n\nTo link your GModStore profile to the Discord (which will also grant you with the Developer role if you're a developer), type `.link <GModStore profile URL>` and instructions will follow.\nType `.help` to see the commands you can use on me.");
  430.  
  431. member = member.user;
  432. AdminServ.channels.general.sendMessage(member.toString() + " has joined the Discord.");
  433. });
  434. AdminServ.bot.on("guildMemberRemove",function(_,member) {
  435. member = member.user;
  436. AdminServ.channels.general.sendMessage(member.toString() + " has left the Discord.");
  437. });
  438.  
  439. AdminServ.bot.on("ready",function() {
  440.  
  441. AdminServ.bot.user.setGame("JSBot");
  442.  
  443. AdminServ.guild = AdminServ.bot.guilds.array();
  444. for (var i=0;i < AdminServ.guild.length;i++) {
  445. if (AdminServ.guild[i].id == "191312474954268672") {
  446. AdminServ.guild = AdminServ.guild[i];
  447. AdminServ.users = AdminServ.guild.members.array();
  448. for (var i=0;i < AdminServ.users.length;i++) {
  449. //AdminServ.database.query("INSERT INTO `bot_discordids` (`id`,`username`,`nickname`) VALUES(?,?,?) ON DUPLICATE KEY UPDATE `id`=?, `username`=?, `nickname`=?",[AdminServ.users[i].id,AdminServ.users[i].user.username,AdminServ.users[i].nickname || AdminServ.users[i].user.username,AdminServ.users[i].id,AdminServ.users[i].user.username,AdminServ.users[i].nickname || AdminServ.users[i].user.username]);
  450. }
  451. }
  452. }
  453.  
  454. var jar = request.jar();
  455.  
  456. var channels = AdminServ.bot.channels.array();
  457. for (var i=0;i < channels.length;i++) {
  458. if (channels[i].type == "text") {
  459. AdminServ.channels[channels[i].name] = channels[i];
  460. }
  461. }
  462.  
  463. //AdminServ.channels.general.sendMessage("Hello, I am AdminServ -- Connection established.");
  464.  
  465. });
  466.  
  467. AdminServ.bot.on("message", (message) => {
  468.  
  469. let prefix = "!";
  470.  
  471. if (message.content.startsWith( prefix + "ping")) {
  472. message.channel.sendMessage("pong! your mom");
  473. }
  474. });
  475.  
  476. AdminServ.bot.login(AdminServ.botsecret);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement