diff --git a/app.js b/app.js index 0de3e97..afd1fef 100644 --- a/app.js +++ b/app.js @@ -3,7 +3,8 @@ let kara = new Vue({ data: { features: { changeName: false, - themes: false, + themes: true, + ownReactions: true, setNameAtStart: true }, messages: [], @@ -18,6 +19,7 @@ let kara = new Vue({ username: '', themes: null, activeTheme: 'slate', + talk: false, addModal: { includeAll: false, keywords: '', @@ -31,12 +33,33 @@ let kara = new Vue({ location: '' }, notes: [], - reactions: [] + answers: [ + { + includeAll: false, + keywords: [ + 'hi', 'hallo', 'servas', 'servus', 'hello' + ], + responses: [ + 'Hey! :)', + 'Hello!', + 'How are you?' + ] + }, + { + includeAll: true, + keywords: [ + 'how', 'are', 'you' + ], + responses: [ + 'I\'m good! Thanks for asking! :) How about you?', + 'A bit tired from trying to do that think called "thinking".. i\'ll figure it out one day.', + 'Wooo! I\'m hyped for party! You\'re in?' + ] + } + ] }, mounted() { this.getSavedData(); - this.getReactions(); - if (this.features.themes) { this.getBootswatchThemes(); } @@ -65,7 +88,38 @@ let kara = new Vue({ this.scrollDown(); }, methods: { - // Initial + karaTalks(message) { + var speech = new SpeechSynthesisUtterance(); + + // Set the text and voice attributes. + speech.text = message; + speech.volume = 1; + speech.rate = 1; + speech.pitch = 1; + + window.speechSynthesis.speak(speech); + }, + addMessage(body, bot, me = false) { + this.messages.push({ + body: body, + bot: bot, + command: body.search('/') === 0, + me: me, + time: Date.now() + }) + }, + botMessage(body) { + this.addMessage(body, true); + if (this.talk) { + this.karaTalks(body); + } + }, + userMessage(body) { + this.addMessage(body, false); + this.lastMessage = body; + + this.updateStorage(); + }, initialGreeting() { this.botMessage( this.oneOf( @@ -75,6 +129,8 @@ let kara = new Vue({ ] ) ); + + this.updateStorage(); }, welcomeBack() { this.botMessage( @@ -87,66 +143,6 @@ let kara = new Vue({ ) ); }, - - // Name - askForName() { - this.botMessage( - this.oneOf([ - 'May i ask for your name?', - 'Whats your name? :)', - 'How can i call you?', - 'How did your developers call you? :)' - ]) - ); - - this.askedForName = true; - }, - setName(message) { - this.username = message.trim(); - this.settingsModal.username = this.username; - this.askedForName = false; - - this.botMessage( - this.oneOf([ - "That's a beautiful name!", - "Okay, i'll call you " + this.username + " from now on :)", - "Nice to meet you, " + this.username + ". :D" - ]) - ) - - this.updateStorage(); - }, - - // Messages / Chat - addMessage(body, bot, me = false) { - this.messages.push({ - body: body, - bot: bot, - command: body.search('/') === 0, - me: me, - time: Date.now() - }) - }, - addImageMessage(imageObject, bot) { - this.lastMessageData = imageObject; - - this.messages.push({ - body: imageObject.body, - bot: bot, - src: imageObject.src - }); - - this.updateStorage(); - }, - botMessage(message) { - this.addMessage(message, true); - }, - userMessage(body) { - this.addMessage(body, false); - this.lastMessage = body; - - this.updateStorage(); - }, sendMessage() { if (this.chatbox.trim() === '') { return false; @@ -159,10 +155,6 @@ let kara = new Vue({ this.react(this.chatbox); this.chatbox = ''; }, - meMessage(message) { - this.addMessage(this.username + ' ' + message, false, true); - this.react(message, true); - }, react(message, recursive = false) { this.isTyping = true; @@ -181,7 +173,7 @@ let kara = new Vue({ } else if (this.takeNote === true) { this.saveNote(message); } else { - let answer = this.getReaction(message); + let answer = this.getAnswer(message); if (answer) { this.botMessage(answer); } @@ -194,80 +186,139 @@ let kara = new Vue({ }, 1800); } }, - getReaction(message) { - let preserveLastMessageData = false; - message = this.cleanupMessage(message); + processCommands(message) { + if (this.checkForCommands(message, 'note')) { + let noteToSave = this.checkForCommands(message, 'note'); + this.saveNote(noteToSave); + } - let keywords = message.split(' '); - let answer = undefined; + if (this.checkForCommands(message, 'clear')) { + this.clearChat(); + } - if (this.lastMessageData.joke && this.includesOneOf(keywords, ['another', 'more'])) { - this.tellJoke(this.lastMessageData.category); - return false; + if (this.checkForCommands(message, 'weather')) { + this.checkWeather() } - if (this.lastMessageData.meme && this.includesOneOf(keywords, ['another', 'more'])) { - this.getRandomMeme(this.lastMessageData.category); - return false; + if (this.checkForCommands(message, 'joke')) { + this.tellJoke(this.checkForCommands(message, 'joke')) + } + + if (this.checkForCommands(message, 'me')) { + this.meMessage(this.checkForCommands(message, 'me')) + } + + if (this.checkForCommands(message, 'talk')) { + this.talk = !this.talk; + alert(this.talk); } + }, + checkForCommands(message, commands) { + if (!Array.isArray(commands)) { + commands = [commands]; + } + + let commandFound = false; + let parameter = false; - if (this.lastMessageData.isJeopardy) { - if (this.includesAllOf(keywords, ['give', 'up'])) { - this.lastMessageData = {}; - return 'The right question would have been: "' + this.lastMessageData.question + '"'; + commands.forEach((command) => { + if (commandFound) { + return; } + let commandString = '/' + command; - if (this.isSimilar(message, this.lastMessageData.question)) { - this.lastMessageData = {}; - return this.oneOf([ - 'Wow! This is the right question.', - 'Correct!' - ]); - } else { - return this.oneOf([ - "Sorry, that's not correct.", - "That's not what we're searching for.", - "Nope, sorry. You can always give up by typing \"I give up\" if you wan't to.", - ]); + if (message.search(commandString) === 0) { + parameter = message.replace(commandString, '').trim(); + commandFound = true; } - } + }); + + return parameter ? parameter : commandFound; + }, + scrollDown() { + $('#chat-box').stop().animate({ + scrollTop: ($('#chat-box')[0].scrollHeight * 10) + }, 800); + }, + oneOf(answers) { + let amountOfAnswers = answers.length; + let randomIndex = Math.floor(Math.random() * (amountOfAnswers)); + + return this.convertWildcards(answers[randomIndex]); + }, + convertWildcards(message) { + message = message.replace('$name$', this.username); + message = message.replace('$botname$', this.name); + + return message; + }, + includesOneOf(phrases, wordsToSearch) { + let includes = false; - if (this.lastMessageData.isTrivia) { - if (this.includesAllOf(keywords, ['give', 'up'])) { - this.lastMessageData = {}; - return this.oneOf([ - 'The right answer would have been: "' + this.lastMessageData.question + '"' - ]); + wordsToSearch.forEach((searchWord) => { + if (phrases.includes(searchWord)) { + includes = true; } + }) - if (this.isSimilar(message, this.lastMessageData.answer)) { - this.lastMessageData = {}; - return this.oneOf([ - 'Congratulations! This is the correct answer.', - 'Correct!' - ]); - } else { - return this.oneOf([ - "Sorry, that's not correct.", - "That's not what we're searching for.", - "Nope, sorry. You can always give up by typing \"I give up\" if you wan't to.", - ]); + return includes; + }, + includesAllOf(phrases, wordsToSearch) { + let includesAllPhrases = true; + + wordsToSearch.forEach((searchWord) => { + if (!phrases.includes(searchWord)) { + includesAllPhrases = false; } - } + }) + + return includesAllPhrases; + }, + cleanupMessage(message) { + message = message.toLowerCase(); + return message.replace('?', '') + .replace('!', '') + .replace('.', '') + .replace(',', '') + .replace('-', '') + .replace('_', '') + .replace('#', '') + .replace('\'', '') + .replace('"', '') + .replace('+', '') + .replace('*', '') + .replace('ยง', '') + .replace('$', '') + .replace('%', '') + .replace('&', '') + .replace('/', '') + .replace('(', '') + .replace(')', '') + .replace('=', '') + .replace('\\', '') + .replace('@', '') + .replace('~', '') + .replace('โ€ฆ', ''); + }, + getAnswer(message) { + message = this.cleanupMessage(message); + + let phrases = message.split(' '); + let answer = undefined; - if (!preserveLastMessageData) { - this.lastMessageData = {}; + if (this.lastMessageData.joke && this.includesOneOf(phrases, ['another', 'more'])) { + this.tellJoke(this.lastMessageData.category); } - if (this.includesAllOf(keywords, ['change', 'my', 'name'])) { + if (this.includesAllOf(phrases, ['change', 'my', 'name'])) { this.askedForName = true; return "Please tell me how i should call you."; } - if (this.includesAllOf(keywords, ['new', 'note']) || - this.includesAllOf(keywords, ['new', 'task']) || - this.includesAllOf(keywords, ['take', 'note']) || - this.includesAllOf(keywords, ['save', 'to', 'clipboard']) + if (this.includesAllOf(phrases, ['new', 'note']) || + this.includesAllOf(phrases, ['new', 'task']) || + this.includesAllOf(phrases, ['take', 'note']) || + this.includesAllOf(phrases, ['save', 'to', 'clipboard']) ) { this.takeNote = true; return this.oneOf([ @@ -277,114 +328,56 @@ let kara = new Vue({ ]); } - if (this.includesAllOf(keywords, ['clear', 'chat'])) { - this.clearChat(); - return false; - } - - if (this.includesAllOf(keywords, ['play']) && - this.includesOneOf(keywords, ['quiz', 'trivia']) - ) { - this.startQuiz(); - return false; - } - - if (this.includesAllOf(keywords, ['play', 'jeopardy'])) { - this.startJeopardy(); - return false; - } - - if (this.includesAllOf(keywords, ['roll', 'dice'])) { - this.rollDice(); - return false; - } - - if (this.includesAllOf(keywords, ['clear', 'chat'])) { + if (this.includesAllOf(phrases, ['clear', 'chat'])) { this.clearChat(); return false; } - if (this.includesAllOf(keywords, ['weather']) && - this.includesOneOf(keywords, ['how', 'whats']) + if (this.includesAllOf(phrases, ['weather']) && + this.includesOneOf(phrases, ['how', 'whats']) ) { this.checkWeather(); return false; } - if (this.includesAllOf(keywords, ['knock', 'joke'])) { + if (this.includesAllOf(phrases, ['knock', 'joke'])) { this.tellJoke('knock-knock'); return false; } - if (this.includesAllOf(keywords, ['joke']) && this.includesOneOf(keywords, ['coding', 'programming', 'code', 'it'])) { - this.tellJoke('programming'); - return false; - } - - if (this.includesAllOf(keywords, ['cat']) && - this.includesOneOf(keywords, ['images', 'photos', 'send']) - ) { - this.getRandomCat(); - return false; - } - - if (this.includesOneOf(keywords, ['doggo', 'dog', 'shiba']) && - this.includesOneOf(keywords, ['images', 'photos', 'send']) - ) { - this.getRandomShiba(); - return false; - } - - if (this.includesAllOf(keywords, ['dank', 'meme'])) { - this.getRandomMeme('dankmemes') - return false; - } - - if (this.includesAllOf(keywords, ['dank', 'meme'])) { - this.getRandomMeme('dankmemes'); - return false; - } - - if (this.includesAllOf(keywords, ['meme']) && - this.includesOneOf(keywords, ['get', 'send']) - ) { - this.getRandomMeme(); - return false; - } - - if (this.includesAllOf(keywords, ['joke']) && this.includesOneOf(keywords, ['coding', 'programming', 'code', 'it'])) { + if (this.includesAllOf(phrases, ['joke']) && this.includesOneOf(phrases, ['coding', 'programming', 'code', 'it'])) { this.tellJoke('programming'); return false; } - if (this.includesAllOf(keywords, ['tell', 'joke']) || - this.includesAllOf(keywords, ['something', 'funny']) || - this.includesAllOf(keywords, ['cheer', 'me', 'up']) + if (this.includesAllOf(phrases, ['tell', 'joke']) || + this.includesAllOf(phrases, ['something', 'funny']) || + this.includesAllOf(phrases, ['cheer', 'me', 'up']) ) { this.tellJoke('general'); return false; } - if (this.includesAllOf(keywords, ['whats', 'the', 'time']) || this.includesAllOf(keywords, ['how', 'late'])) { + if (this.includesAllOf(phrases, ['whats', 'the', 'time']) || this.includesAllOf(phrases, ['how', 'late'])) { return "It's " + moment().format('LT'); } - if (this.includesAllOf(keywords, ['what', 'day', 'it'])) { + if (this.includesAllOf(phrases, ['what', 'day', 'it'])) { return "It's " + moment().format('dddd') + "."; } - if (this.includesAllOf(keywords, ['what', 'date', 'it']) || this.includesAllOf(keywords, ['whats', 'the', 'date'])) { + if (this.includesAllOf(phrases, ['what', 'date', 'it']) || this.includesAllOf(phrases, ['whats', 'the', 'date'])) { return "It's " + moment().format('dddd') + ", " + moment().format('MMMM Do YYYY') + "."; } - this.reactions.forEach((reactionOption) => { - if (reactionOption.includeAll === true) { - if (this.includesAllOf(keywords, reactionOption.keywords)) { - answer = this.oneOf(reactionOption.responses); + this.answers.forEach((answerOption) => { + if (answerOption.includeAll === true) { + if (this.includesAllOf(phrases, answerOption.keywords)) { + answer = this.oneOf(answerOption.responses); } } else { - if (this.includesOneOf(keywords, reactionOption.keywords)) { - answer = this.oneOf(reactionOption.responses); + if (this.includesOneOf(phrases, answerOption.keywords)) { + answer = this.oneOf(answerOption.responses); } } }); @@ -393,92 +386,142 @@ let kara = new Vue({ return answer; } - this.botMessage("I don't know what to say.."); + return 'I don\'t know what to say..'; + }, + addResponseToInput() { + this.addModal.responses.push(''); }, + addNewResponseContainer() { + let includeAll = this.addModal.includeAll; + let keywords = this.cleanupMessage(this.addModal.keywords).split(','); + let responses = this.addModal.responses; - // Forms - saveSettings() { - this.name = this.settingsModal.name; - this.username = this.settingsModal.username; - this.location = this.settingsModal.location; - this.updateStorage(); + this.answers.push({ + includeAll: includeAll, + keywords: keywords, + responses: responses + }); - // this.botMessage('Settings saved! :)'); - this.scrollDown(); + this.addModal.includeAll = false; + this.addModal.keywords = ''; + this.addModal.responses = ['']; + + this.updateStorage(); }, + updateStorage() { + localStorage.setItem('name', this.name); + localStorage.setItem('username', this.username); + localStorage.setItem('activeTheme', this.activeTheme); + localStorage.setItem('messages', JSON.stringify(this.messages)); + localStorage.setItem('answers', JSON.stringify(this.answers)); + localStorage.setItem('notes', JSON.stringify(this.notes)); + localStorage.setItem('lastMessage', JSON.stringify(this.lastMessage)); + localStorage.setItem('location', JSON.stringify(this.location)); + }, + getSavedData() { + let savedName = localStorage.getItem('name'); - // Commands - processCommands: function (message) { - if (this.checkForCommands(message, 'note')) { - let noteToSave = this.checkForCommands(message, 'note'); - this.saveNote(noteToSave); - } + this.name = savedName ? savedName : this.name; - if (this.checkForCommands(message, 'clear')) { - this.clearChat(); - } + let savedUsername = localStorage.getItem('username'); + this.username = savedUsername ? savedUsername : null; - if (this.checkForCommands(message, 'weather')) { - this.checkWeather(); - } + let savedActiveTheme = localStorage.getItem('activeTheme'); + this.activeTheme = savedActiveTheme ? savedActiveTheme : 'slate'; - if (this.checkForCommands(message, 'joke')) { - this.tellJoke(this.checkForCommands(message, 'joke')); - } + let savedMessages = JSON.parse(localStorage.getItem('messages')); + this.messages = savedMessages ? savedMessages : []; - if (this.checkForCommands(message, 'meme')) { - this.getRandomMeme(this.checkForCommands(message, 'meme')); - } else if (this.checkForCommands(message, 'me ')) { - this.meMessage(this.checkForCommands(message, 'me ')); - } + let savedAnswers = JSON.parse(localStorage.getItem('answers')); + this.answers = savedAnswers ? savedAnswers : []; - if (this.checkForCommands(message, 'cat')) { - this.getRandomCat(); - } + let savedNotes = JSON.parse(localStorage.getItem('notes')); + this.notes = savedNotes ? savedNotes : []; - if (this.checkForCommands(message, 'shiba')) { - this.getRandomShiba(); - } + let savedLastMessage = JSON.parse(localStorage.getItem('lastMessage')); + this.lastMessage = savedLastMessage ? savedLastMessage : null; - if (this.checkForCommands(message, 'birb')) { - this.getRandomBirb(); - } + let savedLocation = JSON.parse(localStorage.getItem('location')); + this.location = savedLocation ? savedLocation : null; - if (this.checkForCommands(message, 'quiz')) { - this.startQuiz(); - } + this.scrollDown(); + }, + askForName() { + this.botMessage( + this.oneOf([ + 'May i ask for your name?', + 'Whats your name? :)', + 'How can i call you?', + 'How did your developers call you? :)' + ]) + ); - if (this.checkForCommands(message, 'jeopardy')) { - this.startJeopardy(); - } + this.askedForName = true; + }, + setName(message) { + this.username = message.trim(); + this.settingsModal.username = this.username; + this.askedForName = false; + this.updateStorage(); - if (this.checkForCommands(message, 'dice')) { - this.rollDice(); - } + this.botMessage( + this.oneOf([ + "That's a beautiful name!", + "Okay, i'll call you " + this.username + " from now on :)", + "Nice to meet you, " + this.username + ". :D" + ]) + ) + }, + saveNote(message) { + this.takeNote = false; - this.lastMessage = message; + this.notes.push({ + time: moment(), + body: message, + checked: false + }); + + this.updateStorage(); + + this.botMessage( + this.oneOf([ + "Saved! :)", + "You can read and check your notes in the clipboard-section. :)" + ]) + ) }, - checkForCommands(message, commands) { - if (!Array.isArray(commands)) { - commands = [commands]; - } + clearNotes() { + this.notes = []; - let commandFound = false; - let parameter = false; + this.botMessage( + this.oneOf([ + "Notes cleared. ๐Ÿšฎ" + ]) + ); - commands.forEach((command) => { - if (commandFound) { - return; - } - let commandString = '/' + command; + this.updateStorage(); + }, + saveSettings() { + this.name = this.settingsModal.name; + this.username = this.settingsModal.username; + this.location = this.settingsModal.location; + this.updateStorage(); - if (message.search(commandString) === 0) { - parameter = message.replace(commandString, '').trim(); - commandFound = true; - } - }); + // this.botMessage('Settings saved! :)'); + this.scrollDown(); + }, + clearChat() { + this.messages = []; + this.botMessage(this.oneOf([ + 'Chat cleared.', + 'Evidence destroyed.', + 'Mind cleared.', + 'Uhm.. i think forgot everything we ever talked about.. oops.', + 'A fresh start!', + "You've got something to hide? Nevermind, gotcha fam. Chat is cleared now. ๐Ÿฑโ€๐Ÿ‘ค" + ])); - return parameter ? parameter : commandFound; + this.updateStorage(); }, checkWeather() { let vue = this; @@ -496,103 +539,12 @@ let kara = new Vue({ vue.botMessage('In ' + response.data.name + ' it\'s ' + response.data.main.temp.toFixed() + 'ยฐC with ' + response.data.weather[0].description + '.'); }) .catch(function (error) { - alertify.notify(error, 'danger'); + alert(error); vue.botMessage('I couldn\'t check the weather for your location. ๐Ÿค”'); }) this.updateStorage(); }, - getRandomMeme(category = 'memes') { - let vue = this; - let url = 'https://meme-api.herokuapp.com/gimme/'; - - let categorySet = false; - - if (category !== true && category !== false) { - category = category.trim(); - categorySet = true; - } else { - category = 'memes' - } - - axios.get(url + category) - .then(function (response) { - vue.addImageMessage({ - body: response.data.title, - src: response.data.url - }, true); - - vue.lastMessageData = { - meme: true, - category: category - }; - }) - .catch(function (error) { - vue.botMessage("Hmm.. i can't think of any good memes right now, sorry.. ๐Ÿ˜ž"); - }) - - this.updateStorage(); - }, - getRandomCat() { - let vue = this; - let url = 'https://api.thecatapi.com/v1/images/search?apiKey=5666b3cc-a7a3-406f-904d-b13594780d7f'; - - axios.get(url) - .then(function (response) { - vue.addImageMessage({ - caption: '', - image: response.data[0].url - }, true); - setTimeout(() => { - vue.scrollDown(); - }, 1000) - }) - .catch(function (error) { - vue.botMessage("I can't find any images at the moment, sorry.. ๐Ÿ˜ฟ"); - }) - - this.updateStorage(); - }, - getRandomShiba() { - let vue = this; - let url = 'http://shibe.online/api/shibes'; - - axios.get(url) - .then(function (response) { - vue.addImageMessage({ - caption: '', - image: response.data[0].url - }, true); - setTimeout(() => { - vue.scrollDown(); - }, 1000) - }) - .catch(function (error) { - vue.botMessage("I can't find any images at the moment, sorry.. ๐Ÿถ"); - }) - - this.updateStorage(); - }, - getRandomBirb() { - let vue = this; - let url = '//random.birb.pw/tweet.json'; - - axios.get(url) - .then(function (response) { - vue.addImageMessage({ - caption: '', - image: 'https://random.birb.pw/img/' + response.data.file - }, true); - setTimeout(() => { - vue.scrollDown(); - }, 1000) - }) - .catch(function (error) { - vue.botMessage("I can't find any images at the moment, sorry.. ๐Ÿฆ"); - }) - - this.updateStorage(); - }, tellJoke(category) { let vue = this; let categorySet = false; @@ -608,6 +560,7 @@ let kara = new Vue({ axios.get(url) .then(function (response) { + console.log(response); let joke = response.data[0]; vue.botMessage(joke.setup); @@ -634,220 +587,14 @@ let kara = new Vue({ this.updateStorage(); }, - startQuiz() { - let vue = this; - let url = 'https://jservice.io/api/random'; - - axios.get(url) - .then(function (response) { - let clue = response.data[0]; - - vue.botMessage('Okay! Here is your question from the category "' + clue.category.title + '":'); - vue.botMessage(clue.question); - - vue.lastMessageData = { - isTrivia: true, - answer: clue.answer - }; - }) - .catch(function (error) { - vue.botMessage("It's not a good time for a quiz."); - }); - - this.updateStorage(); - }, - startJeopardy() { - let vue = this; - let url = 'https://jservice.io/api/random'; - - axios.get(url) - .then(function (response) { - let clue = response.data[0]; - - vue.botMessage('Okay! Here we go. The category is "' + clue.category.title + '":'); - vue.botMessage(clue.answer); - - vue.lastMessageData = { - isJeopardy: true, - question: clue.question - }; - }) - .catch(function (error) { - vue.botMessage("It's not a good time for a quiz."); - }); - - this.updateStorage(); - }, - rollDice() { - let dice = Math.random()*6; - - if (dice < 1) { - dice = 1; - } else { - dice = Math.floor(dice); - } - - this.addImageMessage({ - body: dice + '!', - src: 'img/' + dice + '.png' - }, true); - - this.updateStorage(); - }, - - // Notes - saveNote(message) { - this.takeNote = false; - - this.notes.push({ - time: moment(), - body: message, - checked: false - }); - - this.updateStorage(); - - this.botMessage( - this.oneOf([ - "Saved! :)", - "You can read and check your notes in the clipboard-section. :)" - ]) - ) - }, - clearNotes() { - this.notes = []; - - this.botMessage( - this.oneOf([ - "Notes cleared. ๐Ÿšฎ" - ]) - ); - - this.updateStorage(); - }, - - // LocalStorage - getSavedData() { - let savedName = localStorage.getItem('name'); - - this.name = savedName ? savedName : this.name; - - let savedUsername = localStorage.getItem('username'); - this.username = savedUsername ? savedUsername : null; - - let savedActiveTheme = localStorage.getItem('activeTheme'); - this.activeTheme = savedActiveTheme ? savedActiveTheme : 'slate'; - - let savedMessages = JSON.parse(localStorage.getItem('messages')); - this.messages = savedMessages ? savedMessages : []; - - let savedNotes = JSON.parse(localStorage.getItem('notes')); - this.notes = savedNotes ? savedNotes : []; - - let savedLastMessage = JSON.parse(localStorage.getItem('lastMessage')); - this.lastMessage = savedLastMessage ? savedLastMessage : null; - - let savedLocation = JSON.parse(localStorage.getItem('location')); - this.location = savedLocation ? savedLocation : null; - - this.scrollDown(); - }, - updateStorage() { - localStorage.setItem('name', this.name); - localStorage.setItem('username', this.username); - localStorage.setItem('activeTheme', this.activeTheme); - localStorage.setItem('messages', JSON.stringify(this.messages)); - localStorage.setItem('answers', JSON.stringify(this.reactions)); - localStorage.setItem('notes', JSON.stringify(this.notes)); - localStorage.setItem('lastMessage', JSON.stringify(this.lastMessage)); - localStorage.setItem('location', JSON.stringify(this.location)); + meMessage(message) { + this.addMessage(this.username + ' ' + message, false, true); + this.react(message, true); }, clearStorage() { localStorage.clear(); location.reload(); }, - - // Utility - scrollDown() { - $('#chat-box').stop().animate({ - scrollTop: ($('#chat-box')[0].scrollHeight * 10) - }, 800); - }, - oneOf(answers) { - let amountOfAnswers = answers.length; - let randomIndex = Math.floor(Math.random() * (amountOfAnswers)); - - return this.convertWildcards(answers[randomIndex]); - }, - convertWildcards(message) { - message = message.replace('$name$', this.username); - message = message.replace('$botname$', this.name); - - return message; - }, - cleanupMessage(message) { - message = message.toLowerCase(); - return message.replace('?', '') - .replace('!', '') - .replace('.', '') - .replace(',', '') - .replace('-', '') - .replace('_', '') - .replace('#', '') - .replace('\'', '') - .replace('"', '') - .replace('+', '') - .replace('*', '') - .replace('ยง', '') - .replace('$', '') - .replace('%', '') - .replace('&', '') - .replace('/', '') - .replace('(', '') - .replace(')', '') - .replace('=', '') - .replace('\\', '') - .replace('@', '') - .replace('~', '') - .replace('โ€ฆ', ''); - }, - clearChat() { - this.messages = []; - this.botMessage(this.oneOf([ - 'Chat cleared.', - 'Evidence destroyed.', - 'Mind cleared.', - 'Uhm.. i think forgot everything we ever talked about.. oops.', - 'A fresh start!', - "You've got something to hide? Nevermind, gotcha fam. Chat is cleared now. ๐Ÿฑโ€๐Ÿ‘ค" - ])); - - this.updateStorage(); - }, - includesOneOf(keywords, wordsToSearch) { - let includes = false; - - wordsToSearch.forEach((searchWord) => { - if (keywords.includes(searchWord)) { - includes = true; - } - }) - - return includes; - }, - includesAllOf(keywords, wordsToSearch) { - let includesAllkeywords = true; - - wordsToSearch.forEach((searchWord) => { - if (!keywords.includes(searchWord)) { - includesAllkeywords = false; - } - }) - - return includesAllkeywords; - }, - - // Ajax calls for saving/receiving reactions & themes getBootswatchThemes() { let vue = this; @@ -858,66 +605,6 @@ let kara = new Vue({ .catch(function (error) { console.log(error); }) - }, - getReactions() { - let vue = this; - - axios.get('/reactions/get').then((response) => { - response.data.forEach((reaction) => { - vue.reactions.push(JSON.parse(reaction.reaction)); - }); - }).catch((error) => { - console.log(error); - }); - }, - - // Levenshtein distance - isSimilar(message1, message2) { - let longer = message1; - let shorter = message2; - - if (message1.length < message2.length) { - longer = message2; - shorter = message1; - } - - const longerLength = longer.length; - if (longerLength === 0) { - return 1.0; - } - - let similarity = (longerLength - this.editDistance(longer, shorter)) / parseFloat(longerLength); - - return similarity > 0.65; - }, - - editDistance: function (message1, message2) { - message1 = message1.toLowerCase(); - message2 = message2.toLowerCase(); - - let costs = []; - for (let i = 0; i <= message1.length; i++) { - let lastValue = i; - for (let j = 0; j <= message2.length; j++) { - if (i === 0) - costs[j] = j; - else { - if (j > 0) { - let newValue = costs[j - 1]; - if (message1.charAt(i - 1) !== message2.charAt(j - 1)) - newValue = Math.min(Math.min(newValue, lastValue), - costs[j]) + 1; - costs[j - 1] = lastValue; - lastValue = newValue; - } - } - } - if (i > 0) { - costs[message2.length] = lastValue; - } - } - - return costs[message2.length]; } } }) \ No newline at end of file