Last active
September 14, 2017 17:57
-
-
Save EricRabil/76e01e1750915b92756e998ff4f61077 to your computer and use it in GitHub Desktop.
A Markov SelfBot for Discord.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const discord = require('discord.js'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const client = new discord.Client(); | |
| global.triggerWord = 'Beep boop, boop boop beep???' | |
| const readline = require(`readline`) | |
| if (!process.argv[2]) { | |
| console.log(`Usage: node client.js <token>`) | |
| process.exit(-69) | |
| } | |
| const token = process.argv[2]; | |
| const logger = { | |
| __print (prefix, data) { | |
| console.log(data) | |
| }, | |
| log (...word) { | |
| word = word.join(' ') | |
| logger.__print(`[INFO]`, word) | |
| }, | |
| warn (...word) { | |
| word = word.join(' ') | |
| logger.__print(`[WARN]`, word) | |
| }, | |
| err (...word) { | |
| word = word.join(' ') | |
| logger.__print(`[ERR ]`, word) | |
| } | |
| } | |
| global.recordWords = true; | |
| var titles = []; | |
| if (fs.existsSync(path.join(__dirname, 'words.json'))) { | |
| titles = require("./words"); | |
| } | |
| global.writeWords = function() { | |
| var wordtyWords = JSON.stringify(titles); | |
| fs.writeFileSync(path.join(__dirname, 'words.json'), wordtyWords); | |
| } | |
| global.shutdown = function() { | |
| writeWords(); | |
| client.destroy().then(() => { | |
| logger.log(`Goodbye!`) | |
| process.exit(0); | |
| }) | |
| } | |
| var terminals = {}; | |
| var startwords = []; | |
| var wordstats = {}; | |
| var make_title = function (min_length) { | |
| var choice = function (a) { | |
| var i = Math.floor(a.length * Math.random()); | |
| return a[i]; | |
| }; | |
| word = choice(startwords); | |
| var title = [word]; | |
| while (wordstats.hasOwnProperty(word)) { | |
| var next_words = wordstats[word]; | |
| word = choice(next_words); | |
| title.push(word); | |
| if (title.length > min_length && terminals.hasOwnProperty(word)) break; | |
| } | |
| if (title.length < min_length) return make_title(min_length); | |
| return title.join(' '); | |
| }; | |
| const handleSpeak = message => { | |
| // we are no getin hakd m8 | |
| if (titles.length <= 15) { | |
| message.react('❌') | |
| return; | |
| } | |
| message.edit(make_title(5 + Math.floor(3 * Math.random()))); | |
| } | |
| const insertWordIntoWords = message => { | |
| if (recordWords) { | |
| var sentence = message.content | |
| logger.log(`[${titles.length+1}] [${message.guild.name}] (#${message.channel.name}) <${message.author.username}>: ${sentence}`) | |
| var words = sentence.split(' '); | |
| terminals[words[words.length-1]] = true; | |
| startwords.push(words[0]); | |
| for (var j = 0; j < words.length - 1; j++) { | |
| if (wordstats.hasOwnProperty(words[j])) { | |
| wordstats[words[j]].push(words[j+1]); | |
| } else { | |
| wordstats[words[j]] = [words[j+1]]; | |
| } | |
| } | |
| titles.push(sentence); | |
| } | |
| } | |
| logger.log('Logging into Discord...') | |
| client.login(token).then(() => { | |
| logger.log(`Done! Please wait a few minutes, then type \`${triggerWord}\``) | |
| client.on('message', message => { | |
| if (message.cleanContent === triggerWord && message.author.id === client.user.id) { | |
| return handleSpeak(message); | |
| } | |
| if (message.author.id === client.user.id) return; | |
| if (message.author.bot) return; | |
| if (1 >= message.cleanContent) return; | |
| insertWordIntoWords(message); | |
| }) | |
| readline.createInterface({input: process.stdin, output: process.stdout}).on('line', i => { | |
| try { | |
| var output = eval(i) | |
| output instanceof Promise | |
| ? output.then(a => { | |
| console.log('Promise Resolved') | |
| console.log(util.inspect(a, {depth: 0})) | |
| }).catch(e => { | |
| console.log('Promise Rejected') | |
| console.log(e.stack) | |
| }) | |
| : output instanceof Object | |
| ? console.log(util.inspect(output, {depth: 0})) | |
| : console.log(output) | |
| } catch (err) { | |
| console.log(err.stack) | |
| } | |
| }) | |
| }).catch(logger.err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment