Created
January 5, 2011 04:32
-
-
Save lukeledet/765931 to your computer and use it in GitHub Desktop.
Simple Minecraft jabber bot
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
### | |
# 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 |
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!
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
I don't think I'll be able to help you with this one. Are you getting any errors from the ruby script when that happens? I can't reproduce it. I remember it staying connected for hours when I used it. Are you running it twice or something? One version might kick another one off.