forked from group-butler/GroupButler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.lua
More file actions
executable file
·173 lines (139 loc) · 3.23 KB
/
utilities.lua
File metadata and controls
executable file
·173 lines (139 loc) · 3.23 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
-- utilities.lua
-- Functions shared among plugins.
function get_word(s, i) -- get the indexed word in a string
s = s or ''
i = i or 1
local t = {}
for w in s:gmatch('%g+') do
table.insert(t, w)
end
return t[i] or false
end
function string:input() -- Returns the string after the first space.
if not self:find(' ') then
return false
end
return self:sub(self:find(' ')+1)
end
function string:sec2() -- Returns the string after the first space.
if not self:find(' ') then
return false
end
t = self:sub(self:find(' ')+1)
if not t:find(' ') then
return nil
end
return t:sub(t:find(' ')+1)
end
function is_owner(msg)
local var = false
local groups = load_data('groups.json')
if groups[tostring(msg.chat.id)]['owner'] == tostring(msg.from.id) then
var = true
end
if msg.from.id == config.admin then
var = true
end
return var
end
function is_mod(msg)
local var = false
local groups = load_data('groups.json')
if groups[tostring(msg.chat.id)]['owner'] == tostring(msg.from.id) then
var = true
end
if groups[tostring(msg.chat.id)]['mods'][tostring(msg.from.id)] then
var = true
end
if msg.from.id == config.admin then
var = true
end
return var
end
function is_locked(msg, cmd)
local var = false
local data = load_data('groups.json')
if data[tostring(msg.chat.id)]['settings'][tostring(cmd)] == 'yes' then
var = true
end
return var
end
function mystat(cmd)
stat = load_data('statsbot.json')
n = stat[tostring(cmd)]
n = n+1
stat[tostring(cmd)] = tonumber(n)
save_data('statsbot.json', stat)
print('Stats saved', cmd)
end
-- I swear, I copied this from PIL, not yago! :)
function string:trim() -- Trims whitespace from a string.
local s = self:gsub('^%s*(.-)%s*$', '%1')
return s
end
local lc_list = {
-- Latin = 'Cyrillic'
['A'] = 'А',
['B'] = 'В',
['C'] = 'С',
['E'] = 'Е',
['I'] = 'І',
['J'] = 'Ј',
['K'] = 'К',
['M'] = 'М',
['H'] = 'Н',
['O'] = 'О',
['P'] = 'Р',
['S'] = 'Ѕ',
['T'] = 'Т',
['X'] = 'Х',
['Y'] = 'Ү',
['a'] = 'а',
['c'] = 'с',
['e'] = 'е',
['i'] = 'і',
['j'] = 'ј',
['o'] = 'о',
['s'] = 'ѕ',
['x'] = 'х',
['y'] = 'у',
['!'] = 'ǃ'
}
function latcyr(str) -- Replaces letters with corresponding Cyrillic characters.
for k,v in pairs(lc_list) do
str = string.gsub(str, k, v)
end
return str
end
function load_data(filename) -- Loads a JSON file as a table.
local f = io.open(filename)
if not f then
return {}
end
local s = f:read('*all')
f:close()
local data = JSON.decode(s)
return data
end
function save_data(filename, data) -- Saves a table to a JSON file.
local s = JSON.encode(data)
local f = io.open(filename, 'w')
f:write(s)
f:close()
end
-- Gets coordinates for a location. Used by gMaps.lua, time.lua, weather.lua.
function get_coords(input)
local url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input)
local jstr, res = HTTP.request(url)
if res ~= 200 then
return config.errors.connection
end
local jdat = JSON.decode(jstr)
if jdat.status == 'ZERO_RESULTS' then
return config.errors.results
end
return {
lat = jdat.results[1].geometry.location.lat,
lon = jdat.results[1].geometry.location.lng
}
end