Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. const Discord = require('discord.js');
  2. var fs = require('fs');
  3. const bot = new Discord.Client();
  4.  
  5. const IDLE = 0;
  6. const LEARN_RESPONSE = 1;
  7. const LEARN_MOOD = 2;
  8. const DISABLED = 3
  9.  
  10.  
  11. var Word = class{
  12. constructor(str, feeling){
  13. this.str = str;
  14. this.feeling;
  15. }
  16. };
  17.  
  18. var Response = class{
  19. constructor(prompt, resp){
  20. this.prompt = prompt;
  21. this.resp = resp;
  22. }
  23. };
  24.  
  25.  
  26. String.prototype.replaceAll = function(str1, str2, ignore)
  27. {
  28. return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
  29. }
  30.  
  31.  
  32.  
  33. var AI = [mode = 0, mood = 0, lastMessage = "", myLastMessage = "", nextWord = '', learnWords = [], learnIndex = 0, learnPhrase = '', debugStr = '',otherMessage = ''];
  34.  
  35. var words = [new Word('overwatch', 5)];
  36. var responses = [new Response('you suck', 'no u')];
  37.  
  38. var responseJSONArray = fs.readFileSync('responses.json').toString();
  39. responses = JSON.parse(responseJSONArray);
  40.  
  41. AI.mode = IDLE;
  42.  
  43. function saveResponses()
  44. {
  45. var str = JSON.stringify(responses);
  46.  
  47. fs.writeFileSync('responses.json',str);
  48. }
  49.  
  50.  
  51. commonWords = ['you','i','the','a','and','me','is', 'an', 'are', 'be', 'yo', 'to', 'so', 'no', 'your', 'si'];
  52.  
  53.  
  54. function findResponse(prompt)
  55. {
  56. var index = -1;
  57. var matchScore = 0;
  58. var matchTemp = 0;
  59. var respArray = [];
  60. var promptArray = [];
  61.  
  62. prompt = prompt.replaceAll('?', '');
  63. prompt = prompt.replaceAll('.', '');
  64. prompt = prompt.replaceAll('!', '');
  65. prompt = prompt.replaceAll('<', '');
  66. prompt = prompt.replaceAll('>', '');
  67. prompt = prompt.replaceAll(',', '');
  68. prompt = prompt.replaceAll('+', '');
  69. prompt = prompt.replaceAll('-', '');
  70. prompt = prompt.replaceAll('*', '');
  71. prompt = prompt.replaceAll('\'', '');
  72. prompt = prompt.replaceAll('!', '');
  73.  
  74. for(i=0; responses.length > i; i++){
  75. matchTemp = 0;
  76. respArray = responses[i].prompt.split(" ");
  77. if(responses[i].prompt == prompt){
  78. index = i;
  79. }
  80. else{
  81. for(j=0; respArray.length > j; j++){
  82.  
  83. if(prompt.includes(respArray[j]) && commonWords.indexOf(respArray[j]) == -1){
  84. if(respArray[j].length > 2){
  85. matchTemp = matchTemp + respArray[j].length;
  86. }
  87. //console.log("matched: " + respArray[j]);
  88. }
  89. }
  90. if(matchTemp > matchScore){
  91. index = i;
  92. matchScore = matchTemp;
  93. matchTemp = 0;
  94. }
  95. }
  96. }
  97. if(prompt && index > -1){
  98. console.log(prompt + " was matched with " + responses[index].prompt + " with a score of " + matchScore);
  99. }
  100.  
  101. return index;
  102.  
  103. }
  104.  
  105. function translatePhrase(phrase)
  106. {
  107. var str = phrase;
  108. str = str.replace('#mlm', AI.myLastMessage);
  109. str = str.replace('#lm', AI.lastMessage);
  110.  
  111. if(str.includes('#_disable'))
  112. {
  113. AI.mode = DISABLED;
  114. }
  115.  
  116.  
  117. return str;
  118. }
  119.  
  120. function findWord(word)
  121. {
  122.  
  123. }
  124.  
  125.  
  126. bot.on('message', (message) => {
  127.  
  128. //(message.author.id == '87019827985666048' || message.author.id == '198635694187347970')
  129. var authorized = true;
  130. var msg = message.toString().toLowerCase();
  131. if(AI.mode != IDLE){
  132. authorized = false;
  133. if(message.author.id == '87019827985666048' || message.author.id == '198635694187347970')
  134. {
  135. authorized = true;
  136. }
  137. }
  138. if(message.channel.id != '284401695188844544'){
  139. authorized = false;
  140. }
  141. if(msg == 'wrong'){
  142. authorized = false;
  143. }
  144. if(msg == 'bobe')
  145. {
  146. message.channel.sendMessage("yes?");
  147. authorized = false;
  148. AI.mode = IDLE;
  149. }
  150.  
  151. if(AI.otherMessage == 'what' && AI.lastMessage == 'what'){
  152. message.channel.sendMessage("I see what you're doing. Good bye.");
  153. authorized = false;
  154. AI.otherMessage = '';
  155. AI.mode = DISABLED;
  156. }
  157.  
  158. if(AI.otherMessage == 'no' && AI.lastMessage == 'no'){
  159. message.channel.sendMessage("Fine, I'll just be quiet.");
  160. authorized = false;
  161. AI.otherMessage = '';
  162. AI.mode = DISABLED;
  163. }
  164.  
  165.  
  166. if((message.author.id != '284164256818266113') && authorized)
  167. {
  168. ignoreList = '!wrong,!debug,!bobe,#_disable';
  169.  
  170. if((AI.mode == IDLE) && ignoreList.includes(msg) == false){
  171. console.log('setting last message to [' + msg + ']');
  172. AI.otherMessage = AI.lastMessage;
  173. AI.lastMessage = msg;
  174. }
  175.  
  176. switch(AI.mode){
  177.  
  178. case IDLE:
  179. if(msg == 'shut up bobe'){
  180. AI.mode = DISABLED;
  181. message.channel.sendMessage("ok.");
  182. break;
  183. }
  184.  
  185. if(msg == '!wrong'){
  186.  
  187. AI.mode = LEARN_MOOD;
  188.  
  189. }
  190.  
  191. if(msg == 'wrong'){
  192.  
  193. AI.mode = LEARN_MOOD;
  194.  
  195. }
  196.  
  197. if(msg == '!debug')
  198. {
  199. message.channel.sendMessage(AI.debugStr);
  200. }
  201.  
  202.  
  203.  
  204. if(findResponse(msg) > -1 && ignoreList.includes(msg) == false){
  205. var myResponse = translatePhrase(responses[findResponse(msg)].resp);
  206. if(AI.mode == IDLE && myResponse != '#_'){
  207. message.channel.sendMessage(myResponse);
  208. AI.myLastMessage = myResponse;
  209. AI.debugStr = "I translated:\n[" + msg + "]\nas\n[" + responses[findResponse(msg)].prompt + "]\nand responded with\n[" + translatePhrase(responses[findResponse(msg)].resp) + "]";
  210. }
  211. break;
  212. }
  213. else
  214. {
  215. AI.mode = LEARN_MOOD;
  216. message.channel.sendMessage("I don't know what to *freaking* say!\n\nI need Griff or Rob to tell me");
  217. AI.learnPhrase = AI.lastMessage;
  218. AI.learnWords = AI.lastMessage.split(" ");
  219. AI.learnIndex = AI.learnWords.length - 1;
  220. }
  221. case LEARN_MOOD:
  222. if(
  223. //AI.learnIndex > -1
  224. 1==2){
  225. var word = AI.learnWords[AI.learnIndex];
  226. var mystr = "And how should I feel about **¿**?[0 to 5]\n0 is neutral(don't feel anything)\n5 is good\n1 is bad";
  227. message.channel.sendMessage(mystr.replace('¿', word));
  228. AI.learnIndex = AI.learnIndex - 1;
  229. AI.mode = LEARN_MOOD;
  230. }
  231. else{
  232. AI.mode = LEARN_RESPONSE;
  233. message.channel.sendMessage("What should I say in response to **" + AI.learnPhrase + "**?");
  234. }
  235.  
  236. break;
  237. case LEARN_RESPONSE:
  238. AI.learnPhrase = AI.learnPhrase.replaceAll('?', '');
  239. AI.learnPhrase = AI.learnPhrase.replaceAll('.', '');
  240. AI.learnPhrase = AI.learnPhrase.replaceAll('!', '');
  241. AI.learnPhrase = AI.learnPhrase.replaceAll('<', '');
  242. AI.learnPhrase = AI.learnPhrase.replaceAll('>', '');
  243. AI.learnPhrase = AI.learnPhrase.replaceAll(',', '');
  244. AI.learnPhrase = AI.learnPhrase.replaceAll('+', '');
  245. AI.learnPhrase = AI.learnPhrase.replaceAll('-', '');
  246. AI.learnPhrase = AI.learnPhrase.replaceAll('*', '');
  247. AI.learnPhrase = AI.learnPhrase.replaceAll('\'', '');
  248. AI.learnPhrase = AI.learnPhrase.replaceAll('"', '');
  249. AI.learnPhrase = AI.learnPhrase.replaceAll('!', '');
  250. responses.push(new Response(AI.learnPhrase, msg));
  251. saveResponses();
  252. console.log('Added response\nprompt: ' + AI.learnPhrase + '\nresp: ' + msg);
  253. message.channel.sendMessage("Alright. I'll remember that.");
  254. AI.mode = IDLE;
  255. break;
  256. case DISABLED:
  257. if(msg == '!bobe'){
  258. AI.mode = IDLE;
  259. message.channel.sendMessage("huh");
  260. }
  261. default:
  262. break;
  263. }
  264. }
  265.  
  266.  
  267. })
  268.  
  269. bot.login('Mjg0MTY0MjU2ODE4MjY2MTEz.C4_oJQ.2D6o_lbVIZFXIi1NtEmshsiQGiM');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement