This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/plugins/afk.lua

122 lines
3.0 KiB
Lua
Raw Normal View History

2015-11-12 17:42:03 +01:00
do
local function is_offline(hash)
local afk = redis:hget(hash, 'afk')
if afk == "true" then
return true
else
return false
end
end
local function 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
2016-01-11 19:38:03 +01:00
local function switch_afk(user_name, user_id, chat_id, timestamp, text)
2015-11-12 17:42:03 +01:00
local hash = 'afk:'..chat_id..':'..user_id
if is_offline(hash) then
local afk_text = get_afk_text(hash)
if afk_text then
2015-11-21 19:23:59 +01:00
return 'Du bist bereits AFK! ('..afk_text..')'
2015-11-12 17:42:03 +01:00
else
return 'Du bist bereits AFK!'
end
end
print('Setting redis hash afk in '..hash..' to true')
redis:hset(hash, 'afk', true)
2016-01-11 19:38:03 +01:00
print('Setting redis hash timestamp in '..hash..' to '..timestamp)
redis:hset(hash, 'time', timestamp)
2015-11-12 17:42:03 +01:00
if text then
print('Setting redis hash afk_text in '..hash..' to '..text)
redis:hset(hash, 'afk_text', text)
2016-07-07 21:24:02 +02:00
return user_name..' ist AFK! ('..text..')'
2015-11-12 17:42:03 +01:00
else
2016-07-07 21:24:02 +02:00
return user_name..' ist AFK!'
2015-11-12 17:42:03 +01:00
end
end
local function pre_process(msg)
if msg.to.type ~= "chat" then
-- Ignore
return msg
end
local receiver = get_receiver(msg)
2015-04-14 20:21:23 +02:00
local user_name = get_name(msg)
2015-11-12 17:42:03 +01:00
local user_id = msg.from.id
local chat_id = msg.to.id
local hash = 'afk:'..chat_id..':'..user_id
2015-06-06 22:15:05 +02:00
2015-11-12 17:42:03 +01:00
if is_offline(hash) then
local afk_text = get_afk_text(hash)
2016-01-11 19:38:03 +01:00
-- calculate afk time
local timestamp = redis:hget(hash, 'time')
local current_timestamp = msg.date
local afk_time = current_timestamp - timestamp
local seconds = afk_time % 60
local minutes = math.floor(afk_time / 60)
local minutes = minutes % 60
local hours = math.floor(afk_time / 3600)
if minutes == 00 and hours == 00 then
duration = seconds..' Sekunden'
elseif hours == 00 and minutes ~= 00 then
duration = string.format("%02d:%02d", minutes, seconds)..' Minuten'
elseif hours ~= 00 then
duration = string.format("%02d:%02d:%02d", hours, minutes, seconds)..' Stunden'
end
2015-11-12 17:42:03 +01:00
redis:hset(hash, 'afk', false)
if afk_text then
redis:hset(hash, 'afk_text', false)
2016-01-11 19:38:03 +01:00
send_msg(receiver, user_name..' ist wieder da! (war: '..afk_text..' für '..duration..')', ok_cb, false)
2015-07-19 18:58:45 +02:00
else
2016-01-11 19:38:03 +01:00
send_msg(receiver, user_name..' ist wieder da! (war '..duration..' weg)', ok_cb, false)
2015-06-06 22:15:05 +02:00
end
2015-11-12 17:42:03 +01:00
end
return msg
end
local function run(msg, matches)
if msg.to.type ~= "chat" then
2016-07-07 21:24:02 +02:00
return "Mir ist's egal, ob du AFK bist."
2015-11-12 17:42:03 +01:00
end
local user_id = msg.from.id
local chat_id = msg.to.id
local user_name = get_name(msg)
2016-01-11 19:38:03 +01:00
local timestamp = msg.date
2015-11-12 17:42:03 +01:00
if matches[2] then
2016-01-11 19:38:03 +01:00
return switch_afk(user_name, user_id, chat_id, timestamp, matches[2])
2015-11-12 17:42:03 +01:00
else
2016-01-11 19:38:03 +01:00
return switch_afk(user_name, user_id, chat_id, timestamp)
2015-11-12 17:42:03 +01:00
end
2015-04-14 20:21:23 +02:00
end
return {
2015-11-12 17:42:03 +01:00
description = 'AFK und online schalten',
usage = "#afk (Text): Setzt Status auf AFK mit optionalem Text",
2015-11-12 17:42:03 +01:00
patterns = {
"^#([Aa][Ff][Kk])$",
"^#([Aa][Ff][Kk]) (.*)$"
2015-11-12 17:42:03 +01:00
},
run = run,
pre_process = pre_process
2015-04-14 20:21:23 +02:00
}
2015-11-12 17:42:03 +01:00
-- by Akamaru [https://ponywave.de]
-- modified by iCON [http://wiidatabase.de]
end