Skip to content

Instantly share code, notes, and snippets.

@lukeledet
Created January 5, 2011 04:32
Show Gist options
  • Select an option

  • Save lukeledet/765931 to your computer and use it in GitHub Desktop.

Select an option

Save lukeledet/765931 to your computer and use it in GitHub Desktop.
Simple Minecraft jabber bot
###
# Simple jabber bot to let me talk to users of my minecraft server from gtalk
require 'xmpp4r-simple'
BOT_USERNAME = '...'
BOT_PASSWORD = '...'
ADMIN_USERNAME = '...'
SCREEN_NAME = 'minecraft'
# The idea for this method came from here but I blockified it:
# http://stackoverflow.com/questions/1293695/watch-read-a-growing-log-file-with-ruby
def watch_file(file, &block)
timeout = 1
f = File.open(file, "r")
f.seek(0, IO::SEEK_END)
while true do
select [f]
yield f.gets
sleep timeout
end
end
def say(message)
%x{screen -S #{SCREEN_NAME} -p 0 -X stuff "`printf "say #{message}\r"`"}
end
jabber = Jabber::Simple.new(BOT_USERNAME, BOT_PASSWORD, nil, 'Minecraft Bot')
admin_online = false
# Trap CTRL-C to logoff cleanly.
trap("INT") do
jabber.disconnect
puts "\nLogging off."
exit
end
watch_file('server.log') do |line|
case line
when /\[INFO\] (.*?) \[[0-9\/\.:]+\] logged in/
jabber.deliver(ADMIN_USERNAME, "#{$1} logged in")
when /\[INFO\] (.*?) lost connection: (.*)/
jabber.deliver(ADMIN_USERNAME, "#{$1} lost connection: #{$2}")
when /\[INFO\] (?:\[.*?\] )?(.*?): (.*)/
jabber.deliver(ADMIN_USERNAME, "#{$1}: #{$2}")
end
# Crudely determine if the admin is online
jabber.presence_updates do |update|
admin_online = true if update[0] == ADMIN_USERNAME && update[1] == :online
admin_online = false if update[0] == ADMIN_USERNAME && update[1] == :unavailable
end
jabber.received_messages {|msg| say msg.body if admin_online }
end
@zQueal

zQueal commented Apr 23, 2012

Copy link
Copy Markdown

Yup! That was it. :P For whatever reason a single 'ruby jabber.rb' command was running it twice! I restarted my server and it's working 100% now! Thank you so much for taking the time to help, and to create this little gem!

@lukeledet

Copy link
Copy Markdown
Author

No problem at all, I'm glad it's useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment