2016-07-17 13:22:27 +02:00
-- original plugin by Akamaru [https://ponywave.de]
-- I added Redis and automatic online switching back in 2015
local afk = { }
function afk : init ( config )
afk.triggers = {
2016-08-15 23:14:28 +02:00
" ^/([Aa][Ff][Kk])$ " ,
2016-07-18 18:37:29 +02:00
" ^/([Aa][Ff][Kk]) (.*)$ "
2016-07-17 13:22:27 +02:00
}
afk.doc = [ [ *
] ] .. config.cmd_pat .. [[afk* _[Text]_: Setzt Status auf AFK mit optionalem Text]]
end
afk.command = ' afk [Text] '
function afk : is_offline ( hash )
local afk = redis : hget ( hash , ' afk ' )
if afk == " true " then
return true
else
return false
end
end
function afk : get_afk_text ( hash )
local afk_text = redis : hget ( hash , ' afk_text ' )
if afk_text ~= nil and afk_text ~= " " and afk_text ~= " false " then
return afk_text
else
return false
end
end
function afk : switch_afk ( user_name , user_id , chat_id , timestamp , text )
local hash = ' afk: ' .. chat_id .. ' : ' .. user_id
if afk : is_offline ( hash ) then
local afk_text = afk : get_afk_text ( hash )
if afk_text then
2016-07-18 18:37:29 +02:00
return ' Du bist bereits AFK! ( ' .. afk_text .. ' ) '
2016-07-17 13:22:27 +02:00
else
return ' Du bist bereits AFK! '
end
end
print ( ' Setting redis hash afk in ' .. hash .. ' to true ' )
redis : hset ( hash , ' afk ' , true )
print ( ' Setting redis hash timestamp in ' .. hash .. ' to ' .. timestamp )
redis : hset ( hash , ' time ' , timestamp )
if text then
print ( ' Setting redis hash afk_text in ' .. hash .. ' to ' .. text )
redis : hset ( hash , ' afk_text ' , text )
2016-07-18 18:37:29 +02:00
return user_name .. ' ist AFK! ( ' .. text .. ' ) '
2016-07-17 13:22:27 +02:00
else
2016-07-18 18:37:29 +02:00
return user_name .. ' ist AFK! '
2016-07-17 13:22:27 +02:00
end
end
function afk : pre_process ( msg , self )
if msg.chat . type == " private " then
-- Ignore
return
end
local user_name = get_name ( msg )
local user_id = msg.from . id
local chat_id = msg.chat . id
local hash = ' afk: ' .. chat_id .. ' : ' .. user_id
2016-08-15 23:14:28 +02:00
local uhash = ' user: ' .. user_id
2016-07-17 13:22:27 +02:00
if afk : is_offline ( hash ) then
local afk_text = afk : get_afk_text ( hash )
-- calculate afk time
local timestamp = redis : hget ( hash , ' time ' )
local current_timestamp = msg.date
local afk_time = current_timestamp - timestamp
2016-08-01 21:07:27 +02:00
local duration = makeHumanTime ( afk_time )
2016-07-17 13:22:27 +02:00
redis : hset ( hash , ' afk ' , false )
2016-08-15 23:14:28 +02:00
local show_afk_keyboard = redis : hget ( uhash , ' afk_keyboard ' )
2016-07-17 13:22:27 +02:00
if afk_text then
redis : hset ( hash , ' afk_text ' , false )
2016-08-15 23:14:28 +02:00
if show_afk_keyboard == ' true ' then
utilities.send_reply ( self , msg , user_name .. ' ist wieder da! (war: <b> ' .. afk_text .. ' </b> für ' .. duration .. ' ) ' , ' HTML ' , ' {"hide_keyboard":true,"selective":true} ' )
else
utilities.send_message ( self , chat_id , user_name .. ' ist wieder da! (war: <b> ' .. afk_text .. ' </b> für ' .. duration .. ' ) ' , true , nil , ' HTML ' )
end
2016-07-17 13:22:27 +02:00
else
2016-08-15 23:14:28 +02:00
if show_afk_keyboard == ' true ' then
utilities.send_reply ( self , msg , user_name .. ' ist wieder da! (war ' .. duration .. ' weg) ' , nil , ' {"hide_keyboard":true,"selective":true} ' )
else
utilities.send_message ( self , chat_id , user_name .. ' ist wieder da! (war ' .. duration .. ' weg) ' )
end
2016-07-17 13:22:27 +02:00
end
end
return msg
end
2016-08-15 23:14:28 +02:00
function afk : action ( msg , config , matches )
2016-07-17 13:22:27 +02:00
if msg.chat . type == " private " then
2016-07-18 18:37:29 +02:00
utilities.send_reply ( self , msg , " Mir ist's egal, ob du AFK bist. " )
2016-07-17 13:22:27 +02:00
return
end
local user_id = msg.from . id
local chat_id = msg.chat . id
local user_name = get_name ( msg )
local timestamp = msg.date
2016-08-03 19:05:05 +02:00
local uhash = ' user: ' .. msg.from . id
local show_afk_keyboard = redis : hget ( uhash , ' afk_keyboard ' )
if show_afk_keyboard == ' true ' then
keyboard = ' {"keyboard":[[{"text":"Wieder da."}]], "one_time_keyboard":true, "selective":true, "resize_keyboard":true} '
else
keyboard = nil
end
2016-07-17 13:22:27 +02:00
2016-08-03 19:05:05 +02:00
utilities.send_reply ( self , msg , afk : switch_afk ( user_name , user_id , chat_id , timestamp , matches [ 2 ] ) , false , keyboard )
2016-07-17 13:22:27 +02:00
end
return afk