Advertisement
Guest User

Untitled

a guest
Jan 27th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.17 KB | None | 0 0
  1. const Discord = require('discord.js');
  2. const client = new Discord.Client();
  3. const fileExists = require('file-exists');
  4. const mysql = require('mysql');
  5.  
  6. var result = [];
  7. var pictureInfos = [];
  8.  
  9. var con = mysql.createConnection({
  10. host: "192.168.0.130",
  11. user: "discord",
  12. password: "PASSWORD",
  13. database: "discord"
  14. });
  15.  
  16. con.connect(err => {
  17. if (err) throw err;
  18. console.log("Connected to database!");
  19. })
  20.  
  21. function commandIs(str, msg) { //Will set the start of the command by ! + the command
  22. return msg.content.toLowerCase().startsWith("~" + str);
  23. }
  24.  
  25. client.on('ready', () => {
  26. console.log('The bot is online!');
  27. });
  28.  
  29. client.on('message', message => {
  30. var args = message.content.split(/[ ]+/); //Will split the message at every spaces there is
  31.  
  32.  
  33.  
  34. if (commandIs("sql-inject", message)) { //meant only when the database hasn't been created
  35. if (args.length === 1 && message.author.id == "194125820418064385") {
  36. message.channel.sendMessage('Executing Level 3 command as Haidson');
  37.  
  38. for (i = 0; i < 1600; ++i) {
  39. if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + i + '.jpg')) {
  40. con.query(`INSERT INTO picture (pictureid) VALUES ('` + i + `');`);
  41. } else if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + i + '.jpeg')) {
  42. con.query(`INSERT INTO picture (pictureid) VALUES ('` + i + `');`);
  43. } else if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + i + '.png')) {
  44. con.query(`INSERT INTO picture (pictureid) VALUES ('` + i + `');`);
  45. }
  46. }
  47. } else {
  48. message.channel.sendMessage('`Cannot execute a Level 3 command without being Haidson!`');
  49. }
  50. }
  51.  
  52. if (commandIs("guildid", message)) {
  53. message.channel.sendMessage(message.guild.id);
  54. }
  55.  
  56. if (commandIs("help", message)) {
  57. message.channel.sendMessage("```Useful commands for the Tidepod-Chan bot: \nLevel 1 commands: \n~+1 : Vote for the first picture that appeared\n~+2 : Vote for the second picture that appeared\n~fav (1/2) : Will save in the database the pictures you love most\n~fav show : Will PM you all the pictures you saved\n----------------------------------------------------------------\nLevel 2 commands:\n~start [args1] [args2]\n[args1] : Set the type of pictures to be sent ** ~help start for the list of types\n[args2] : Set the minimal rating of pictures that can be sent in the channel (default rating: 1400)\n----------------------------------------------------------------\nLevel 3 commands:\nNote: if you don't have access to those commands, it's because you have nothing to do here\n~sql-inject : update the database to add the newest pictures available and set their default configs\n~sql-wipe : Drops the database and create one from scratch\n~server-blacklist : Blacklist a server based on it's guild ID, blocking all access to the bot```");
  58. }
  59.  
  60. if (commandIs("start", message)) {
  61. let tempvar = 0;
  62. con.query(`SELECT * from vote WHERE guildid=` + message.guild.id, (err, rows) => {
  63.  
  64. if (err) throw (err);
  65. if (rows.length > 1) {
  66. message.channel.sendMessage("There is already another process running! Vote before trying to run another! or stop the current process!");
  67. } else {
  68. if (args.length === 1) { //Error message in case of no args
  69.  
  70. message.channel.sendMessage("Not enough arguments!");
  71.  
  72. } else if (args.length === 2) {
  73.  
  74. if (!message.channel.nsfw) {
  75. message.channel.sendMessage('`Cannot fully execute the command as the channel is not NSFW.`');
  76. } else {
  77.  
  78. message.channel.sendMessage('Starting the bot in this channel based on category: ' + args[1]);
  79.  
  80. var counter = 0;
  81. do {
  82.  
  83. con.query(`SELECT pictureid, elo FROM picture where type = ` + args[1] + ` ORDER BY RAND() LIMIT 1;`, (err, rows) => {
  84. if (err) throw err;
  85.  
  86. if (rows.length < 1) {
  87. message.channel.sendMessage("No result found for the criterias mentioned!");
  88. } else {
  89. let pictureid = rows[0].pictureid; //IMPORTANT: WILL CONVERT TO A REGULAR INT
  90. let elo = rows[0].elo;
  91. let guildid = message.guild.id;
  92. con.query(`INSERT INTO vote (pictureid, currentelo, votes, guildid) VALUES (` + pictureid + `, ` + elo + `, 0, ` + guildid + `)`);
  93.  
  94. if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpg')) {
  95. message.channel.sendFile('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpg');
  96. }
  97. else if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpeg')) {
  98. message.channel.sendFile('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpeg');
  99. }
  100. else if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.png')) {
  101. message.channel.sendFile('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.png');
  102. }
  103. }
  104. });
  105. counter++;
  106. setTimeout(function () { //System sleep function
  107. }, 1000);
  108.  
  109. } while (counter < 2);
  110.  
  111. }
  112.  
  113. } else if (args.length === 3) {
  114.  
  115. if (!message.channel.nsfw) {
  116. message.channel.sendMessage('`Cannot fully execute the command as the channel is not NSFW.`');
  117. } else {
  118. message.channel.sendMessage('Starting the bot in this channel based on category: ' + args[1] + ' and minimal rating of: ' + args[2]);
  119. var counter = 0;
  120. do {
  121.  
  122. con.query(`SELECT pictureid, elo FROM picture where type = ` + args[1] + ` AND elo >=` + args[2] + ` ORDER BY RAND() LIMIT 1;`, (err, rows) => {
  123. if (err) throw err;
  124.  
  125. if (rows.length < 1) {
  126. message.channel.sendMessage("No result found for the criterias mentioned!");
  127. } else {
  128. let pictureid = rows[0].pictureid; //IMPORTANT: WILL CONVERT TO A REGULAR INT
  129. let elo = rows[0].elo;
  130. let guildid = message.guild.id;
  131. con.query(`INSERT INTO vote (pictureid, currentelo, votes, guildid) VALUES (` + pictureid + `, ` + elo + `, 0, ` + guildid + `)`);
  132.  
  133. if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpg')) {
  134. message.channel.sendFile('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpg');
  135. }
  136. else if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpeg')) {
  137. message.channel.sendFile('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpeg');
  138. }
  139. else if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.png')) {
  140. message.channel.sendFile('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.png');
  141. }
  142. }
  143. });
  144.  
  145. counter++;
  146. setTimeout(function () { //System sleep function
  147. }, 1000);
  148.  
  149. } while (counter < 2);
  150. }
  151.  
  152. } else {
  153. message.channel.sendMessage('`Too many arguments! Please verify syntax.`');
  154. }
  155. }
  156. });
  157.  
  158. }
  159.  
  160. if (commandIs("+1", message)) {
  161. con.query(`SELECT * FROM vote WHERE guildid=` + message.guild.id, (err, rows) => {
  162. if (rows.length < 1 ){
  163. message.channel.sendMessage("There is nothing to vote for! Start another round using ~start !");
  164. }else{
  165. let pictureid1 = rows[0].pictureid;
  166. let picture1Votes = rows[0].votes;
  167. let pictureid2 = rows[1].pictureid;
  168. con.query(`UPDATE vote SET votes= votes+1 WHERE pictureid=` + pictureid1);
  169.  
  170. if ((picture1Votes + 1) >= 2) {
  171. let previousEloPicture1 = rows[0].currentelo;
  172. let previousEloPicture2 = rows[1].currentelo;
  173. var expectedOutcomePicture1 = (1 / (1 + Math.pow(10, ((previousEloPicture2 - previousEloPicture1) / 400))));
  174. var newElo = previousEloPicture1 + (25 * (2 - expectedOutcomePicture1));
  175. console.log(previousEloPicture1);
  176. console.log(previousEloPicture2);
  177. console.log(expectedOutcomePicture1);
  178. console.log(newElo);
  179. con.query(`UPDATE picture SET elo=` + newElo + ` WHERE pictureid=` + pictureid1);
  180.  
  181. var expectedOutcomePicture2 = (1 / (1 + Math.pow(10, ((previousEloPicture1 - previousEloPicture2) / 400)))); //Based on loss
  182. var newEloPicture2 = previousEloPicture2 - (25 * (2 - expectedOutcomePicture2));
  183. console.log(expectedOutcomePicture2);
  184. console.log(newEloPicture2);
  185. con.query(`UPDATE picture SET elo=` + newEloPicture2 + ` WHERE pictureid=` + pictureid2);
  186. con.query(`DELETE FROM vote WHERE guildid=` + message.guild.id);
  187. }
  188. message.channel.sendMessage("Vote casted!");
  189. }
  190. });
  191. }
  192.  
  193.  
  194. if (commandIs("+2", message)) {
  195. con.query(`SELECT * FROM vote WHERE guildid=` + message.guild.id, (err, rows) => {
  196. if (rows.length < 1 ){
  197. message.channel.sendMessage("There is nothing to vote for! Start another round using ~start !");
  198. }else{
  199. let pictureid1 = rows[0].pictureid;
  200. let picture2Votes = rows[1].votes;
  201. let pictureid2 = rows[1].pictureid;
  202. con.query(`UPDATE vote SET votes= votes+1 WHERE pictureid=` + pictureid2);
  203.  
  204. if ((picture2Votes + 1) >= 2) {
  205. let previousEloPicture1 = rows[0].currentelo;
  206. let previousEloPicture2 = rows[1].currentelo;
  207. var expectedOutcomePicture2 = (1 / (1 + Math.pow(10, ((previousEloPicture1 - previousEloPicture2) / 400))));
  208. var newElo = previousEloPicture2 + (25 * (2 - expectedOutcomePicture2));
  209. con.query(`UPDATE picture SET elo=` + newElo + ` WHERE pictureid=` + pictureid2);
  210.  
  211. var expectedOutcomePicture1 = (1 / (1 + Math.pow(10, ((previousEloPicture2 - previousEloPicture1) / 400)))); //Based on loss
  212. var newEloPicture1 = previousEloPicture1 - (25 * (2 - expectedOutcomePicture1));
  213. con.query(`UPDATE picture SET elo=` + newEloPicture1 + ` WHERE pictureid=` + pictureid1);
  214. con.query(`DELETE FROM vote WHERE guildid=` + message.guild.id);
  215. }
  216. message.channel.sendMessage("Vote casted!");
  217. }
  218. });
  219. }
  220.  
  221. if (commandIs("fav", message)) {
  222. if (args.length === 1) {
  223. message.channel.sendMessage("Please use ~fav 1, ~fav 2 or ~fav show!");
  224. } else if (args.length === 2) {
  225. if (args[1] == "1") {
  226. con.query(`SELECT * FROM vote WHERE guildid=` + message.guild.id, (err, rows) => {
  227. let pictureID = rows[0].pictureid;
  228. con.query(`INSERT INTO favorites (uid, pictureid, serverid) VALUES (` + message.author.id + `,` + pictureID + `,` + message.guild.id + `);`);
  229. }); uid =
  230. + message.author.id, (err, rows) => {
  231.  
  232. }
  233. } else if (args[1] == "2") {
  234. con.query(`SELECT * FROM vote WHERE guildid=` + message.guild.id, (err, rows) => {
  235. let pictureID = rows[1].pictureid;
  236. con.query(`INSERT INTO favorites (uid, pictureid, serverid) VALUES (` + message.author.id + `,` + pictureID + `,` + message.guild.id + `);`);
  237. });
  238.  
  239. } else if (args[1] == "show") {
  240. con.query(`SELECT * FROM favorites WHERE uid=` + message.author.id, (err, rows) => { //TODO: SET A LIMIT IF USER ABOVE 5
  241.  
  242. for (i = 0; i < rows.length; ++i) {
  243. let pictureid = rows[i].pictureid
  244. if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpg')) {
  245. message.author.sendFile('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpg');
  246. }
  247. else if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpeg')) {
  248. message.author.sendFile('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.jpeg');
  249. }
  250. else if (fileExists.sync('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.png')) {
  251. message.author.sendFile('C:/Users/Haidson/Desktop/bobs/' + pictureid + '.png');
  252. }
  253. }
  254. });
  255. } else {
  256. message.channel.sendMessage("Wrong arguments! Please use ~fav 1, ~fav 2 or ~fav show!")
  257. }
  258.  
  259. } else {
  260. message.channel.sendMessage("Wrong arguments! Please use ~help!");
  261. }
  262. }
  263. });
  264.  
  265.  
  266. client.login('LOGINKEY');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement