upd readme, some small changes
This commit is contained in:
parent
f15c139502
commit
e8fc2791e0
8 changed files with 95 additions and 81 deletions
|
@ -13,13 +13,21 @@ and C is faster.
|
||||||
|
|
||||||
[Alpine Linux](https://alpinelinux.org), root:
|
[Alpine Linux](https://alpinelinux.org), root:
|
||||||
* Enable community repo (in wiki)
|
* Enable community repo (in wiki)
|
||||||
|
|
||||||
* Install: `apk add sudo git lua5.3 luarocks openssl-dev`
|
* Install: `apk add sudo git lua5.3 luarocks openssl-dev`
|
||||||
|
|
||||||
* Install dependencies: `luarocks-5.3 install luasec`
|
* Install dependencies: `luarocks-5.3 install luasec`
|
||||||
|
|
||||||
* Create user: `adduser user`
|
* Create user: `adduser user`
|
||||||
|
|
||||||
setup sudo and login to user
|
setup sudo and login to user
|
||||||
|
|
||||||
* Get repo: `git clone https://github.com/Er2ch/comp-tg`
|
* Get repo: `git clone https://github.com/Er2ch/comp-tg`
|
||||||
|
|
||||||
and `cd comp-tg`
|
and `cd comp-tg`
|
||||||
|
|
||||||
* Change token and owner in `config.lua`
|
* Change token and owner in `config.lua`
|
||||||
|
|
||||||
TODO: Use env instaed of config
|
TODO: Use env instaed of config
|
||||||
|
|
||||||
* Run: `lua5.3 init.lua`
|
* Run: `lua5.3 init.lua`
|
||||||
|
|
|
@ -6,13 +6,13 @@
|
||||||
local tools =require 'etc.api.tools'
|
local tools =require 'etc.api.tools'
|
||||||
local json = require 'etc.json'
|
local json = require 'etc.json'
|
||||||
local events=require 'etc.events'
|
local events=require 'etc.events'
|
||||||
local api = { _ev_ = {} }
|
local api = {
|
||||||
|
request = function(s, ...) return tools.request(s.token, ...) end
|
||||||
|
}
|
||||||
api.__index = api -- Make class
|
api.__index = api -- Make class
|
||||||
|
|
||||||
events(api) -- inheritance
|
events(api) -- inheritance
|
||||||
|
|
||||||
function api:request(...) return tools.request(self.token, ...) end
|
|
||||||
|
|
||||||
-- Getters without params
|
-- Getters without params
|
||||||
|
|
||||||
function api:getMe() return self:request 'getMe' end
|
function api:getMe() return self:request 'getMe' end
|
||||||
|
|
|
@ -37,4 +37,7 @@ function events:emit(name, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return function(t) return setmetatable(t, events) end
|
return function(t)
|
||||||
|
t._ev_ = {}
|
||||||
|
return setmetatable(t, events)
|
||||||
|
end
|
||||||
|
|
2
init.lua
2
init.lua
|
@ -1 +1 @@
|
||||||
require 'src.core.core'
|
require 'src.parts.core'
|
||||||
|
|
|
@ -1,76 +0,0 @@
|
||||||
local config = require 'config'
|
|
||||||
|
|
||||||
local Core = {
|
|
||||||
tg = require 'etc.api',
|
|
||||||
config = config,
|
|
||||||
cmds = {},
|
|
||||||
|
|
||||||
_ev_ = {}, -- :evil:
|
|
||||||
}
|
|
||||||
local tg = Core.tg
|
|
||||||
|
|
||||||
Core = (require 'etc.events')(Core) -- add events
|
|
||||||
|
|
||||||
function Core:load(what)
|
|
||||||
local c = config[what]
|
|
||||||
local s = #c
|
|
||||||
for i = 1, s do
|
|
||||||
local v = c[i]
|
|
||||||
|
|
||||||
print(('Loading %s (%d / %d) %s...'):format(what:sub(0, -2), i, s, v))
|
|
||||||
-- Lint
|
|
||||||
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
|
|
||||||
elseif what == 'core' then a(self)
|
|
||||||
end
|
|
||||||
else print 'fail' end
|
|
||||||
end
|
|
||||||
print(('Loaded %d %s'):format(s, what))
|
|
||||||
end
|
|
||||||
|
|
||||||
function Core:ev(t, i, name, ...)
|
|
||||||
local v = t[i]
|
|
||||||
if v.name == name then
|
|
||||||
local succ = pcall(v.fn, self, ...)
|
|
||||||
if not succ then print('event "' .. name .. '" was failed') end
|
|
||||||
if v.type == 'once' then table.remove(t, i) end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function Core:init()
|
|
||||||
self.api = tg {norun = true}
|
|
||||||
|
|
||||||
print 'Client initialization...'
|
|
||||||
|
|
||||||
function Core._ev(ev, ...) self:ev(...) end
|
|
||||||
function self.api._ev(ev, t, i, n, ...)
|
|
||||||
self._ev(ev, t, i, n, self.api, ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
self:load 'events'
|
|
||||||
self:load 'core'
|
|
||||||
|
|
||||||
self.api:login(config.token, function()
|
|
||||||
print('Logged on as @' .. self.api.info.username)
|
|
||||||
self.config.token = nil
|
|
||||||
self.api:emit 'ready'
|
|
||||||
end)
|
|
||||||
|
|
||||||
self:emit 'init'
|
|
||||||
print 'Done!'
|
|
||||||
|
|
||||||
local offs, o = 0
|
|
||||||
self.t = os.time()
|
|
||||||
self.api.runs = true
|
|
||||||
while self.api.runs do
|
|
||||||
self:emit 'tick'
|
|
||||||
|
|
||||||
o = self.api:_getUpd(1, offs, 0)
|
|
||||||
offs = o and o or offs
|
|
||||||
end
|
|
||||||
self.api:getUpdates(1, offs, 0)
|
|
||||||
end
|
|
||||||
|
|
||||||
Core:init()
|
|
35
src/parts/client.lua
Normal file
35
src/parts/client.lua
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
local tg = require 'etc.api'
|
||||||
|
|
||||||
|
return function(Core)
|
||||||
|
local self = Core
|
||||||
|
|
||||||
|
self.api = tg { norun = true }
|
||||||
|
self.cmds = {}
|
||||||
|
|
||||||
|
print 'Client initialization...'
|
||||||
|
|
||||||
|
function Core._ev(ev, ...) self:ev(...) end
|
||||||
|
function self.api._ev(_, t, i, n, ...)
|
||||||
|
self._ev(_, t, i, n, self.api, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
self:load 'events'
|
||||||
|
|
||||||
|
self.api:login(self.config.token, function()
|
||||||
|
print('Logged on as @' .. self.api.info.username)
|
||||||
|
self.config.token = nil
|
||||||
|
self.api:emit 'ready'
|
||||||
|
end)
|
||||||
|
|
||||||
|
local offs, o = 0
|
||||||
|
self.api.runs = true
|
||||||
|
self:on('ready', function()
|
||||||
|
while self.api.runs do
|
||||||
|
self:emit 'tick'
|
||||||
|
|
||||||
|
o = self.api:_getUpd(1, offs, 0)
|
||||||
|
offs = o and o or offs
|
||||||
|
end
|
||||||
|
self.api:getUpdates(1, offs, 0)
|
||||||
|
end)
|
||||||
|
end
|
44
src/parts/core.lua
Normal file
44
src/parts/core.lua
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
local config = require 'config'
|
||||||
|
|
||||||
|
local Core = {
|
||||||
|
config = config,
|
||||||
|
}
|
||||||
|
(require 'etc.events')(Core) -- add events
|
||||||
|
|
||||||
|
function Core:load(what)
|
||||||
|
local c = config[what]
|
||||||
|
local s = #c
|
||||||
|
for i = 1, s do
|
||||||
|
local v = c[i]
|
||||||
|
|
||||||
|
print(('Loading %s (%d / %d) %s...'):format(what:sub(0, -2), i, s, v))
|
||||||
|
-- Lint
|
||||||
|
if true then
|
||||||
|
--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
|
||||||
|
elseif what == 'parts' then a(self)
|
||||||
|
end
|
||||||
|
else print 'fail' end
|
||||||
|
end
|
||||||
|
print(('Loaded %d %s'):format(s, what))
|
||||||
|
end
|
||||||
|
|
||||||
|
function Core:ev(t, i, name, ...)
|
||||||
|
local v = t[i]
|
||||||
|
if v.name == name then
|
||||||
|
local succ = pcall(v.fn, self, ...)
|
||||||
|
if not succ then print('event "' .. name .. '" was failed') end
|
||||||
|
if v.type == 'once' then table.remove(t, i) end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Core:init()
|
||||||
|
self:load 'parts'
|
||||||
|
|
||||||
|
print 'Done!'
|
||||||
|
self:emit 'ready'
|
||||||
|
end
|
||||||
|
|
||||||
|
Core:init()
|
Loading…
Reference in a new issue