From 91a9024e21ec8b429605a036b5c9193442a580e3 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Sun, 15 Feb 2009 01:30:51 +0100 Subject: + @bot.path and datafile methods We provide two methods that make it more simple and elegant for botmodules to define paths relative to the bot's own directory (botclass) and to the BotModule's (assumed) non-registry directory. The first method is Irc::Bot#path(), which joins its arguments with the botclass. This method can be used to access datafiles in the bot directory with a much cleaner syntax; and since it uses File.join, the resulting paths are also properly formatted on each platform, which doesn't hurt. Each BotModule now also carries a dirname() method that should return the directory under botclass that holds the BotModule's datafiles. dirname() defaults to the BotModule's name(), but it can be overridden, e.g. for backwards compatibility (see the patch for the quotes plugin), or for BotModules that share their datafiles. Datafiles can be accessed using the BotModule#datafile() method that joins the botclass, the dirname() and whatever other argument is passed. --- data/rbot/plugins/games/quiz.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'data/rbot/plugins/games/quiz.rb') diff --git a/data/rbot/plugins/games/quiz.rb b/data/rbot/plugins/games/quiz.rb index 7577e7ce..9159a4c4 100644 --- a/data/rbot/plugins/games/quiz.rb +++ b/data/rbot/plugins/games/quiz.rb @@ -186,8 +186,9 @@ class QuizPlugin < Plugin def fetch_data( m ) # Read the winning messages file @win_messages = Array.new - if File.exists? "#{@bot.botclass}/quiz/win_messages" - IO.foreach("#{@bot.botclass}/quiz/win_messages") { |line| @win_messages << line.chomp } + winfile = datafile 'win_messages' + if File.exists? winfile + IO.foreach(winfile) { |line| @win_messages << line.chomp } else warning( "win_messages file not found!" ) # Fill the array with a least one message or code accessing it would fail @@ -212,13 +213,13 @@ class QuizPlugin < Plugin m.reply "Failed to download questions from #{p}, ignoring sources" end else - path = "#{@bot.botclass}/quiz/#{p}" + path = datafile p debug "Fetching from #{path}" # Local data begin - datafile = File.new( path, File::RDONLY ) - data << "\n\n" << datafile.read + file = File.new( path, File::RDONLY ) + data << "\n\n" << file.read rescue m.reply "Failed to read from local database file #{p}, skipping." end -- cgit v1.2.3