Skip to content

Instantly share code, notes, and snippets.

@b1nary
Created February 21, 2014 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b1nary/9135150 to your computer and use it in GitHub Desktop.
Save b1nary/9135150 to your computer and use it in GitHub Desktop.
#Anycoin Bot
#!/usr/bin/env ruby
require 'socket'
require 'timeout'
require 'json'
require 'open-uri'
def try
begin
Timeout::timeout(10) {
yield
}
rescue Exception => e
p e.message
p e.backtrace
"Sorry your request did fail/timeout..."
end
end
def get_diff
try {
data = JSON.parse(`mintcoind getdifficulty`)
"Mintcoin difficulty: PoW #{data['proof-of-work']} ~ PoS #{data['proof-of-stake']}"
}
end
def get_moneysupply
try {
data = JSON.parse(`mintcoind getinfo`)
"Mintcoin moneysupply: #{data['moneysupply']}"
}
end
def get_blockcount
try {
data = JSON.parse(`mintcoind getinfo`)
"Mintcoin blockcount: #{data['blocks']}"
}
end
def get_received adr
try {
data = open("http://mintcoin-explorer.info/chain/Mintcoin/q/getreceivedbyaddress/#{adr.chomp()}").read
"Received by #{adr.chomp()} = #{data}"
}
end
def get_sent adr
try {
data = open("http://mintcoin-explorer.info/chain/Mintcoin/q/getsentbyaddress/#{adr.chomp()}").read
"Received by #{adr.chomp()} = #{data}"
}
end
def get_mintpal
try {
data = JSON.parse(open("https://api.mintpal.com/market/stats/MINT/BTC").read)[0]
"MintPal: #{data['change']}% LAST #{data['last_price']} HIGH #{data['24hhigh']} LOW #{data['24hlow']} VOLUME #{data['24hvol']} BTC"
}
end
class SimpleIrcBot
def initialize(server, port, channel)
@channel = channel
@socket = TCPSocket.open(server, port)
say "NICK Freshh"
say "USER Freshh 0 * Freshh"
say "JOIN ##{@channel}"
#say_to_chan "#{1.chr}ACTION is here to help#{1.chr}"
end
def say(msg)
puts msg
@socket.puts msg
end
def say_to_chan(msg)
say "PRIVMSG ##{@channel} :#{msg}"
end
def run
until @socket.eof? do
msg = @socket.gets
puts msg
if msg.match(/^PING :(.*)$/)
say "PONG #{$~[1]}"
next
end
if msg.match(/PRIVMSG ##{@channel} :(.*)$/)
content = $~[1]
#put matchers here
p content
if content.match(/^!diff/)
say_to_chan( get_diff )
elsif content.match(/^!money/)
say_to_chan( get_moneysupply )
elsif content.match(/^!mintpal/) or content.match(/^!market/)
say_to_chan( get_mintpal )
elsif content.match(/^!block/)
say_to_chan( get_blockcount )
elsif content.match(/^!received /)
say_to_chan( get_received(content.split("!received ")[1]) )
elsif content.match(/^!sent /)
say_to_chan( get_sent(content.split("!sent ")[1]) )
elsif content.match(/^!help/)
say_to_chan( "!diff | !money | !block | !received ADDR | !sent ADDR" )
end
end
end
end
def quit
say "PART ##{@channel} :Ooops. Keep fresh"
say 'QUIT'
end
end
bot = SimpleIrcBot.new("irc.freenode.net", 6667, 'mintcoin')
trap("INT"){ bot.quit }
bot.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment