This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.lua
More file actions
146 lines (113 loc) · 4.22 KB
/
index.lua
File metadata and controls
146 lines (113 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
-- loading libraries
vk = require 'vkapi'
json = require 'dkjson'
local utf8 = require 'lua-utf8'
local config = {} -- config table
local modules = { message = {}, delete = {}, edit = {} } -- modules table
--[[////////////////////////
Modules Functions
////////////////////////]]
-- function to parse module
function loadModule(name)
-- loading module file
local chunk, err = loadfile('modules/' .. name)
-- if file loaded - continue
if chunk then
local succ, ret = pcall(chunk, config)
-- if no errors - continue
if succ then
if not ret.func and not ret.delete and not ret.edit then return print(('[ERROR]\tModule %q has wrong structure'):format(name)) end
-- if modules has right structure insert module function in table
if type(ret.func) == 'function' then table.insert(modules.message, ret.func) end
if type(ret.delete) == 'function' then table.insert(modules.delete, ret.delete) end
if type(ret.edit) == 'function' then table.insert(modules.edit, ret.edit) end
print(('[LOG]\tModule %q was loaded successfully'):format(name))
else print(('[ERROR]\tRuntime error in module %q: %s'):format(name, ret)) end
else print(('[ERROR]\tError loading module %q: %s'):format(name, err)) end
end
-- function to load modules
function loadModules()
print(('[LOG]\tLoading modules...'))
-- opening modules directory
local handle = io.popen('ls modules', 'r')
-- if directory exists
if handle then
-- try load every file from 'modules' folder
for entry in handle:lines() do
-- skip system files
if entry ~= '.' and entry ~= '..' then
-- if file has extension `.lua` – try to load
if entry:sub(-4) == '.lua' then loadModule(entry)
else print(('[ERROR]\tFile %q is not .lua file'):format(entry)) end
end
end
-- closing directory
handle:close()
else print('[ERROR]\tFailed to open modules directory') end
end
--[[////////////////////////
Config Functions
////////////////////////]]
-- function to parse value to string
function toStringValue(v)
if type(v) == 'string' then return "'" .. v .. "'"
elseif type(v) == 'table' then
local str, ttype = '', 'array'
for key, val in pairs(v) do
if type(key) == 'number' then str = str .. toStringValue(val) .. ((key < #v) and ', ' or '')
elseif type(key) == 'string' then
str = str .. ' ' .. key .. ' = ' .. toStringValue(val) .. ',\n'
ttype = 'obj'
end
end
return '{' .. (ttype == 'obj' and '\n' or '') .. str .. (ttype == 'obj' and ' ' or '') .. '}'
else return tostring(v) end
end
-- function for appending value to config
function addToConfig(key, key2, value)
if key2 then config[key][key2] = value
elseif not key2 then config[key] = value end
return value
end
-- function to load config
function loadConfig()
local chunk, err = loadfile('config.lua')
if err then print(err) end
if chunk then
local succ, ret = pcall(chunk)
if succ then config = ret end
else config.accessToken = '' end
end
-- function to save config
function saveConfig()
local f = io.open('config.lua', 'w')
f:write('return {\n')
for k, v in pairs(config) do f:write(' ', k, ' = ', toStringValue(v), ',\n') end
f:write('}')
f:close()
end
-- loading config, modules and then saving config
loadConfig()
loadModules()
saveConfig()
-- if access token in config file is not exist – send error and crash script
if not config.accessToken or config.accessToken == '' then
print('[ERROR]\tPlease fill `config.lua` file in script directory')
return os.exit()
end
--[[////////////////////////
Main Functions
////////////////////////]]
-- init vk-api library
vk.init(config.accessToken)
-- callback function for new messages
vk:on('message', function(msg) for i = 1, #modules.message do modules.message[i](msg) end end)
-- callback function for deleting messages
vk:on('delete', function(delete) for i = 1, #modules.delete do modules.delete[i](delete) end end)
-- callback function for editing messages
vk:on('edit', function(edit) for i = 1, #modules.edit do modules.edit[i](edit) end end)
-- converting string function to utf-8
for name, func in pairs(utf8) do if string[name] then string['_' .. name] = string[name]; string[name] = func end end
config.thisUserId = vk.call('users.get')[1]['id']
-- starting longpoll
vk.longpollStart()