From 00b15b847b57b331fb934bfa76176d4eee194622 Mon Sep 17 00:00:00 2001 From: Er2 Date: Tue, 18 Jan 2022 18:50:00 +0000 Subject: [PATCH 1/5] add uptime --- src/cmds/ping.lua | 10 ++++++++-- src/locales/en.json | 2 +- src/locales/ru.json | 2 +- src/parts/core.lua | 11 +++++++++-- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/cmds/ping.lua b/src/cmds/ping.lua index 926cef7..78e0726 100644 --- a/src/cmds/ping.lua +++ b/src/cmds/ping.lua @@ -1,5 +1,11 @@ return { run = function(C, msg) - C.api:send(msg, msg.loc.pat:format(os.time() - msg.date)) + local t = os.time() + local ps, ls, lm, lh, ld + ps, ls = t - msg.date, t - C.loaded + lm = ls / 60 + lh = lm / 60 + ld = lh / 24 + C.api:send(msg, msg.loc.pat:format(ps, ld, lh, lm, ls)) end -} \ No newline at end of file +} diff --git a/src/locales/en.json b/src/locales/en.json index 55a04fc..7aec5cd 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -17,7 +17,7 @@ "ping": { "desc": "ping pong", - "pat": "Pong! %ds" + "pat": "Pong! %ds. Uptime: %d days (%dh, %dm.)" }, "rub": { "args": "[valute]...", diff --git a/src/locales/ru.json b/src/locales/ru.json index 5efe927..96bc655 100644 --- a/src/locales/ru.json +++ b/src/locales/ru.json @@ -17,7 +17,7 @@ "ping": { "desc": "пинг-понг", - "pat": "Понг! %d секунд" + "pat": "Понг! %dс. В сети: %d дней (%dч, %dм.)" }, "rub": { "args": "[валюта]...", diff --git a/src/parts/core.lua b/src/parts/core.lua index b152c57..8e072a8 100644 --- a/src/parts/core.lua +++ b/src/parts/core.lua @@ -2,6 +2,7 @@ local Core = { config = config, + loaded = 0, } (require 'etc.events')(Core) -- add events @@ -13,8 +14,7 @@ function Core:load(what) print(('Loading %s (%d / %d) %s...'):format(what:sub(0, -2), i, s, v)) -- Lint - if true then - --pcall(require, 'src.'.. what ..'.'.. v) then + if pcall(require, 'src.'.. what ..'.'.. v) then local a=require('src.'.. what ..'.'.. v) if what == 'events' then self.api:on(v, a) elseif what == 'cmds' then self.cmds[v] = a @@ -23,6 +23,7 @@ function Core:load(what) else print 'fail' end end print(('Loaded %d %s'):format(s, what)) + self.loaded = os.time() end function Core:ev(t, i, name, ...) @@ -44,4 +45,10 @@ function Core:init() self:emit 'ready' end +function Core:stop() + self.api:destroy() + print 'Stopped' + print('Uptime: '.. os.time() - self.loaded.. ' seconds') +end + Core:init() From 9a9bd56b7dfffaab8aa8e399b7c3f4e2fa984e18 Mon Sep 17 00:00:00 2001 From: Er2 Date: Tue, 18 Jan 2022 19:15:03 +0300 Subject: [PATCH 2/5] use floats --- src/locales/en.json | 2 +- src/locales/ru.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/locales/en.json b/src/locales/en.json index 7aec5cd..5b58d54 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -17,7 +17,7 @@ "ping": { "desc": "ping pong", - "pat": "Pong! %ds. Uptime: %d days (%dh, %dm.)" + "pat": "Pong! %ds. Uptime: %.1f days (%.1fh, %.1fm.)" }, "rub": { "args": "[valute]...", diff --git a/src/locales/ru.json b/src/locales/ru.json index 96bc655..1f72af5 100644 --- a/src/locales/ru.json +++ b/src/locales/ru.json @@ -17,7 +17,7 @@ "ping": { "desc": "пинг-понг", - "pat": "Понг! %dс. В сети: %d дней (%dч, %dм.)" + "pat": "Понг! %dс. В сети: %.1f дней (%.1fч, %.1fм.)" }, "rub": { "args": "[валюта]...", From 5dee01b07b9571a5ddda919b82ae17c3520a0f1a Mon Sep 17 00:00:00 2001 From: Er2 Date: Tue, 18 Jan 2022 19:34:23 +0000 Subject: [PATCH 3/5] reload --- config.lua | 2 ++ src/cmds/reload.lua | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 src/cmds/reload.lua diff --git a/config.lua b/config.lua index 6acd80f..eead837 100644 --- a/config.lua +++ b/config.lua @@ -3,6 +3,7 @@ return { owner = 935626920 , -- hehe cmds = { 'eval', + 'reload', 'ping', 'rub', 'start', @@ -10,6 +11,7 @@ return { events = { 'command', 'ready', + 'inlineQuery', }, parts = { 'locale', diff --git a/src/cmds/reload.lua b/src/cmds/reload.lua new file mode 100644 index 0000000..1f87b03 --- /dev/null +++ b/src/cmds/reload.lua @@ -0,0 +1,17 @@ +return { + private = true, + run = function(C, msg) + local cat, sub = table.unpack(msg.args) + if not (cat and sub) then + return C.api:reply(msg, '/reload cmds ping') + end + + local path = 'src.'..cat..'.'..sub + package.loaded[path] = nil + local err, m = pcall(require, path) + C.api:reply(msg, ('Reloaded. %s (%s)'):format( + err and 'Error:' or 'No errors', + m + )) + end +} From 8421b61a82510307d7c3a7b8d9a0fa292abb4bf4 Mon Sep 17 00:00:00 2001 From: Er2 Date: Tue, 18 Jan 2022 19:37:23 +0000 Subject: [PATCH 4/5] aww --- src/cmds/reload.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmds/reload.lua b/src/cmds/reload.lua index 1f87b03..52a1393 100644 --- a/src/cmds/reload.lua +++ b/src/cmds/reload.lua @@ -10,7 +10,7 @@ return { package.loaded[path] = nil local err, m = pcall(require, path) C.api:reply(msg, ('Reloaded. %s (%s)'):format( - err and 'Error:' or 'No errors', + not err and 'Error:' or 'Result:', m )) end From 1b498be1d6fd234e93a4a6d8173f499dd4f6f75a Mon Sep 17 00:00:00 2001 From: Er2 Date: Tue, 18 Jan 2022 19:48:06 +0000 Subject: [PATCH 5/5] now must reload --- etc/events.lua | 8 ++++++++ src/cmds/reload.lua | 12 ++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/etc/events.lua b/etc/events.lua index ee1876a..28de8d0 100644 --- a/etc/events.lua +++ b/etc/events.lua @@ -17,6 +17,14 @@ end function events:on(n,f) self:_add('on', n,f) end function events:once(n,f) self:_add('once', n,f) end +function events:off(f) + for k, v in pairs(self._ev_) do + if v.fn == f then + table.remove(self._ev_, k) + end + end +end + function events:_ev(t, i, name, ...) local v = t[i] if v.name == name then diff --git a/src/cmds/reload.lua b/src/cmds/reload.lua index 52a1393..ee00df8 100644 --- a/src/cmds/reload.lua +++ b/src/cmds/reload.lua @@ -9,9 +9,13 @@ return { local path = 'src.'..cat..'.'..sub package.loaded[path] = nil local err, m = pcall(require, path) - C.api:reply(msg, ('Reloaded. %s (%s)'):format( - not err and 'Error:' or 'Result:', - m - )) + + if not err then return C.api:reply(msg, 'Reload failed. ' .. m) + elseif cat == 'events' then C.api:off(m); C.api:on(sub, m) + elseif cat == 'cmds' then C.cmds[sub] = m + elseif cat == 'parts' then m(C) + end + + C.api:reply(msg, 'Reloaded. ' .. tostring(m)) end }