Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Discord = require('discord.js');
- var fs = require('fs');
- const bot = new Discord.Client();
- const IDLE = 0;
- const LEARN_RESPONSE = 1;
- const LEARN_MOOD = 2;
- const DISABLED = 3
- var Word = class{
- constructor(str, feeling){
- this.str = str;
- this.feeling;
- }
- };
- var Response = class{
- constructor(prompt, resp){
- this.prompt = prompt;
- this.resp = resp;
- }
- };
- String.prototype.replaceAll = function(str1, str2, ignore)
- {
- return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
- }
- var AI = [mode = 0, mood = 0, lastMessage = "", myLastMessage = "", nextWord = '', learnWords = [], learnIndex = 0, learnPhrase = '', debugStr = '',otherMessage = ''];
- var words = [new Word('overwatch', 5)];
- var responses = [new Response('you suck', 'no u')];
- var responseJSONArray = fs.readFileSync('responses.json').toString();
- responses = JSON.parse(responseJSONArray);
- AI.mode = IDLE;
- function saveResponses()
- {
- var str = JSON.stringify(responses);
- fs.writeFileSync('responses.json',str);
- }
- commonWords = ['you','i','the','a','and','me','is', 'an', 'are', 'be', 'yo', 'to', 'so', 'no', 'your', 'si'];
- function findResponse(prompt)
- {
- var index = -1;
- var matchScore = 0;
- var matchTemp = 0;
- var respArray = [];
- var promptArray = [];
- prompt = prompt.replaceAll('?', '');
- prompt = prompt.replaceAll('.', '');
- prompt = prompt.replaceAll('!', '');
- prompt = prompt.replaceAll('<', '');
- prompt = prompt.replaceAll('>', '');
- prompt = prompt.replaceAll(',', '');
- prompt = prompt.replaceAll('+', '');
- prompt = prompt.replaceAll('-', '');
- prompt = prompt.replaceAll('*', '');
- prompt = prompt.replaceAll('\'', '');
- prompt = prompt.replaceAll('!', '');
- for(i=0; responses.length > i; i++){
- matchTemp = 0;
- respArray = responses[i].prompt.split(" ");
- if(responses[i].prompt == prompt){
- index = i;
- }
- else{
- for(j=0; respArray.length > j; j++){
- if(prompt.includes(respArray[j]) && commonWords.indexOf(respArray[j]) == -1){
- if(respArray[j].length > 2){
- matchTemp = matchTemp + respArray[j].length;
- }
- //console.log("matched: " + respArray[j]);
- }
- }
- if(matchTemp > matchScore){
- index = i;
- matchScore = matchTemp;
- matchTemp = 0;
- }
- }
- }
- if(prompt && index > -1){
- console.log(prompt + " was matched with " + responses[index].prompt + " with a score of " + matchScore);
- }
- return index;
- }
- function translatePhrase(phrase)
- {
- var str = phrase;
- str = str.replace('#mlm', AI.myLastMessage);
- str = str.replace('#lm', AI.lastMessage);
- if(str.includes('#_disable'))
- {
- AI.mode = DISABLED;
- }
- return str;
- }
- function findWord(word)
- {
- }
- bot.on('message', (message) => {
- //(message.author.id == '87019827985666048' || message.author.id == '198635694187347970')
- var authorized = true;
- var msg = message.toString().toLowerCase();
- if(AI.mode != IDLE){
- authorized = false;
- if(message.author.id == '87019827985666048' || message.author.id == '198635694187347970')
- {
- authorized = true;
- }
- }
- if(message.channel.id != '284401695188844544'){
- authorized = false;
- }
- if(msg == 'wrong'){
- authorized = false;
- }
- if(msg == 'bobe')
- {
- message.channel.sendMessage("yes?");
- authorized = false;
- AI.mode = IDLE;
- }
- if(AI.otherMessage == 'what' && AI.lastMessage == 'what'){
- message.channel.sendMessage("I see what you're doing. Good bye.");
- authorized = false;
- AI.otherMessage = '';
- AI.mode = DISABLED;
- }
- if(AI.otherMessage == 'no' && AI.lastMessage == 'no'){
- message.channel.sendMessage("Fine, I'll just be quiet.");
- authorized = false;
- AI.otherMessage = '';
- AI.mode = DISABLED;
- }
- if((message.author.id != '284164256818266113') && authorized)
- {
- ignoreList = '!wrong,!debug,!bobe,#_disable';
- if((AI.mode == IDLE) && ignoreList.includes(msg) == false){
- console.log('setting last message to [' + msg + ']');
- AI.otherMessage = AI.lastMessage;
- AI.lastMessage = msg;
- }
- switch(AI.mode){
- case IDLE:
- if(msg == 'shut up bobe'){
- AI.mode = DISABLED;
- message.channel.sendMessage("ok.");
- break;
- }
- if(msg == '!wrong'){
- AI.mode = LEARN_MOOD;
- }
- if(msg == 'wrong'){
- AI.mode = LEARN_MOOD;
- }
- if(msg == '!debug')
- {
- message.channel.sendMessage(AI.debugStr);
- }
- if(findResponse(msg) > -1 && ignoreList.includes(msg) == false){
- var myResponse = translatePhrase(responses[findResponse(msg)].resp);
- if(AI.mode == IDLE && myResponse != '#_'){
- message.channel.sendMessage(myResponse);
- AI.myLastMessage = myResponse;
- AI.debugStr = "I translated:\n[" + msg + "]\nas\n[" + responses[findResponse(msg)].prompt + "]\nand responded with\n[" + translatePhrase(responses[findResponse(msg)].resp) + "]";
- }
- break;
- }
- else
- {
- AI.mode = LEARN_MOOD;
- message.channel.sendMessage("I don't know what to *freaking* say!\n\nI need Griff or Rob to tell me");
- AI.learnPhrase = AI.lastMessage;
- AI.learnWords = AI.lastMessage.split(" ");
- AI.learnIndex = AI.learnWords.length - 1;
- }
- case LEARN_MOOD:
- if(
- //AI.learnIndex > -1
- 1==2){
- var word = AI.learnWords[AI.learnIndex];
- var mystr = "And how should I feel about **¿**?[0 to 5]\n0 is neutral(don't feel anything)\n5 is good\n1 is bad";
- message.channel.sendMessage(mystr.replace('¿', word));
- AI.learnIndex = AI.learnIndex - 1;
- AI.mode = LEARN_MOOD;
- }
- else{
- AI.mode = LEARN_RESPONSE;
- message.channel.sendMessage("What should I say in response to **" + AI.learnPhrase + "**?");
- }
- break;
- case LEARN_RESPONSE:
- AI.learnPhrase = AI.learnPhrase.replaceAll('?', '');
- AI.learnPhrase = AI.learnPhrase.replaceAll('.', '');
- AI.learnPhrase = AI.learnPhrase.replaceAll('!', '');
- AI.learnPhrase = AI.learnPhrase.replaceAll('<', '');
- AI.learnPhrase = AI.learnPhrase.replaceAll('>', '');
- AI.learnPhrase = AI.learnPhrase.replaceAll(',', '');
- AI.learnPhrase = AI.learnPhrase.replaceAll('+', '');
- AI.learnPhrase = AI.learnPhrase.replaceAll('-', '');
- AI.learnPhrase = AI.learnPhrase.replaceAll('*', '');
- AI.learnPhrase = AI.learnPhrase.replaceAll('\'', '');
- AI.learnPhrase = AI.learnPhrase.replaceAll('"', '');
- AI.learnPhrase = AI.learnPhrase.replaceAll('!', '');
- responses.push(new Response(AI.learnPhrase, msg));
- saveResponses();
- console.log('Added response\nprompt: ' + AI.learnPhrase + '\nresp: ' + msg);
- message.channel.sendMessage("Alright. I'll remember that.");
- AI.mode = IDLE;
- break;
- case DISABLED:
- if(msg == '!bobe'){
- AI.mode = IDLE;
- message.channel.sendMessage("huh");
- }
- default:
- break;
- }
- }
- })
- bot.login('Mjg0MTY0MjU2ODE4MjY2MTEz.C4_oJQ.2D6o_lbVIZFXIi1NtEmshsiQGiM');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement