forestbot/plugins/questbook.rb

56 lines
1.7 KiB
Ruby
Raw Normal View History

2017-10-17 20:30:42 +03:00
class QuestBook
include Cinch::Plugin
listen_to :questbook
2017-10-29 10:45:11 +02:00
def initialize(m)
@config = YAML.load_file('config.yml')
super(m)
end
2017-10-17 20:30:42 +03:00
def listen(m)
params = FormData.new
params.append('msg', '')
2017-10-17 20:49:06 +03:00
params.append('from', "#{Time.now.to_i - 5}00")
2017-10-17 20:30:42 +03:00
params.append('away', 0)
req = params.post_request("/forum/chat/message.php")
http = Net::HTTP.new('quest-book.ru', 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req.add_field 'Referer', 'https://quest-book.ru/forum/chat/'
req.add_field 'X-Requested-With', 'XMLHttpRequest'
response = http.request(req)
response = JSON.parse response.body
begin
if response['msgs'] then
for msg in response['msgs'] do
print = false
2017-10-17 20:30:42 +03:00
if msg[1] != '' then
print = true
2017-10-17 20:30:42 +03:00
end
2017-10-19 12:29:35 +03:00
color = Digest::MD5.hexdigest(msg[1]).to_i(16) % 14 + 2
who = "[\x03#{color}#{msg[1]}\x0f]"
2017-10-29 10:53:57 +02:00
message = msg[2]
a = Nokogiri::HTML.parse message
message = a.text
if message != @last and print then
Channel('#questbook').send("#{who}: #{message}")
@last = message
2017-10-29 10:45:11 +02:00
# discord webhook
http = Net::HTTP.new('discordapp.com', 443)
http.use_ssl = true
params = FormData.new
2017-10-29 10:53:57 +02:00
params.append('content', message)
2017-10-29 10:45:11 +02:00
params.append('username', msg[1])
2017-10-29 10:50:50 +02:00
req = params.post_request("/api/webhooks/#{@config['discord_webhook']}/#{@config['discord_token']}")
2017-10-29 10:45:11 +02:00
http.request(req)
end
2017-10-17 20:30:42 +03:00
end
end
rescue NoMethodError
2017-10-29 10:50:50 +02:00
puts "No method error:\n"
2017-10-17 20:30:42 +03:00
puts response['msgs'].inspect
end
end
end