Save Tox state
This commit is contained in:
parent
ddd903a96f
commit
2f08914397
1 changed files with 21 additions and 1 deletions
|
@ -12,10 +12,20 @@ module Lita
|
||||||
# Tox adapter for the Lita chat bot.
|
# Tox adapter for the Lita chat bot.
|
||||||
#
|
#
|
||||||
class Tox < Adapter
|
class Tox < Adapter
|
||||||
|
config :savedata_filename, type: String
|
||||||
|
|
||||||
def initialize(robot)
|
def initialize(robot)
|
||||||
super
|
super
|
||||||
|
|
||||||
@tox = ::Tox.new
|
options = ::Tox::Options.new
|
||||||
|
|
||||||
|
if config.savedata_filename && File.exist?(config.savedata_filename)
|
||||||
|
savedata_file = open(config.savedata_filename)
|
||||||
|
options.data = savedata_file.read
|
||||||
|
savedata_file.close
|
||||||
|
end
|
||||||
|
|
||||||
|
@tox = ::Tox.new(options)
|
||||||
|
|
||||||
log.info("ID: #{@tox.id}")
|
log.info("ID: #{@tox.id}")
|
||||||
|
|
||||||
|
@ -37,6 +47,16 @@ module Lita
|
||||||
@tox.loop
|
@tox.loop
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def shut_down
|
||||||
|
if config.savedata_filename
|
||||||
|
savedata_file = open(config.savedata_filename, 'w')
|
||||||
|
savedata_file.write(@tox.savedata)
|
||||||
|
savedata_file.close
|
||||||
|
end
|
||||||
|
|
||||||
|
@tox.kill
|
||||||
|
end
|
||||||
|
|
||||||
def send_messages(target, messages)
|
def send_messages(target, messages)
|
||||||
messages.reject(&:empty?).each do |message|
|
messages.reject(&:empty?).each do |message|
|
||||||
@tox.friend_send_message(target.user.id.to_i, message)
|
@tox.friend_send_message(target.user.id.to_i, message)
|
||||||
|
|
Reference in a new issue