Finally working version

This commit is contained in:
rnhmjoj 2015-10-01 06:10:11 +02:00
parent 65108cffea
commit e19085bf17

View File

@ -16,6 +16,30 @@
net = require 'net'
module.exports = (robot) ->
match_all = (regex, str) ->
matches = []
str.replace regex, ->
arr = [].slice.call arguments, 0
extras = arr.splice -2
arr.index = extras[0]
arr.input = extras[1]
matches.push arr
matches
concat = (list) -> list.reduce (x, y) -> x.concat y
escape = (str) ->
str.replace(/\d|\?|#/g, '') # not supported
.replace( /\ /g, '%20') # spaces
doge_url = (text) ->
"http://dogr.io/#{escape text}.png?split=false"
shiba = (res, words) ->
word = words.filter (x) -> x.length > 4
prefix = [ 'much', 'such', 'many', 'very']
(res.random prefix) + ' ' + (res.random words)
run_command = (command, callback) ->
client = net.connect robot.adapter.port, robot.adapter.host, ->
client.write command+'\n'
@ -26,17 +50,17 @@ module.exports = (robot) ->
client.end()
get_history = (chat, size, callback) ->
regex = /^(?:\d+ )?\[(.+)\] (.+) [»«><]+ (.+)/
regex = /\[(.+)\] (.+) [>«»]+ ((?:(?!\n(\[|\d))[\s\S])+)/g
parse_line = (x) ->
date: x[1]
peer: x[2].replace chat + ' ', ''
text: x[3].trim()
text: x[3].trim().replace '\n', ' '
run_command "history #{chat} #{size}", (lines) ->
callback (parse_line i.match regex for i in lines)
callback ((match_all regex, lines.join '\n').map parse_line)
robot.respond /doge(?: (\d+))?/, (res) ->
n = res.match[1] || 10
get_history res.message.room, n, (history) ->
words = [].concat (message.text.split() for message in history)
sample = res.random words for _ in [0..5]
#do doge meme with sample
words = concat (message.text.split ' ' for message in history)
sample = (res.random ['wow', shiba res, words] for _ in [0..8])
res.send (doge_url sample.join '/')