-
Notifications
You must be signed in to change notification settings - Fork 2
/
whattoeat.lua
255 lines (217 loc) · 7.25 KB
/
whattoeat.lua
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
local log = require("log")
local Api = require("coreApi")
local json = require("json")
local http = require("http")
--########菜单路径#########
local menupath = "/root/opqbot/Plugins/whattoeat.json"
function ReceiveFriendMsg(CurrentQQ, data)
if data.FromUin ==2986807981 then--防止自我复读
return 1 end
if string.find(data.Content, ".今天吃啥菜单") then
menu = ""
foodarray = readfile()
for i=1,#foodarray.arry,1 do
menu = menu.."No"..i..foodarray.arry[i].."\n"
end
luaMsg =
Api.Api_SendMsg(--调用发消息的接口
CurrentQQ,
{
toUser = data.FromUin, --回复当前消息的来源群ID
sendToType = 1, --2发送给群1发送给好友3私聊
sendMsgType = "TextMsg", --进行文本复读回复
groupid = 0, --不是私聊自然就为0咯
content = menu, --回复内容
atUser = 0 --是否 填上data.FromUserId就可以复读给他并@了
}
)
menu = nil
end
if string.find(data.Content, ".添加菜单") then
if data.FromUin ==779091224 then --屏蔽某些捣乱的
result = "你说你🐎呢"
else
local newrecipe = data.Content:gsub(".添加菜单", "")
result = addrecipe(newrecipe)
end
luaMsg =
Api.Api_SendMsg(--调用发消息的接口
CurrentQQ,
{
toUser = data.FromUin, --回复当前消息的来源群ID
sendToType = 1, --2发送给群1发送给好友3私聊
sendMsgType = "TextMsg", --进行文本复读回复
groupid = 0, --不是私聊自然就为0咯
content = result, --回复内容
atUser = 0 --是否 填上data.FromUserId就可以复读给他并@了
}
)
end
return 1
end
function ReceiveGroupMsg(CurrentQQ, data)
if data.FromUserId ==2986807981 then--防止自我复读
return 1 end
if string.find(data.Content, ".今天吃啥") and data.Content:gsub(".今天吃啥", "")=="" or (string.find(data.Content, ".今天吃什么") and data.Content:gsub(".今天吃什么", "")=="") or string.find(data.Content, ".今晚吃啥") or string.find(data.Content, ".今晚吃什么") then
--foodarray = {"汉堡","螺蛳粉","凉皮","火锅","过桥米线","饺子","铁锅饭","卤肉饭","炸酱面","麻辣烫","炒粉","云吞","炸鸡","手抓饼","拉面","泡面","牛排","寿司","木桶饭","冒菜","羊肉粉","馒头","皮蛋瘦肉粥","奶茶","黄焖鸡米饭","海鲜"}
foodarray = readfile()
--print(#foodarray.arry)
--math.randomseed(tonumber(tostring(os.time()):reverse():sub(1, 7))) --https://blog.csdn.net/goodai007/article/details/59579515
math.randomseed(os.time()+assert(tonumber(tostring({}):sub(7)))) --试试这个?匿名空表取地址
--result = math.random(1 , #foodarray.arry)
result = getrandomint(#foodarray.arry)
menu = foodarray.arry[result]
luaMsg =
Api.Api_SendMsg(--调用发消息的接口
CurrentQQ,
{
toUser = data.FromGroupId, --回复当前消息的来源群ID
sendToType = 2, --2发送给群1发送给好友3私聊
sendMsgType = "TextMsg", --进行文本复读回复
groupid = 0, --不是私聊自然就为0咯
content =" [ATUSER("..data.FromUserId..")] 今天吃"..menu.."\n*"..result.."*可选命令->.添加菜单 ,完整菜单-> .今天吃啥菜单", --回复内容
atUser = 0 --是否 填上data.FromUserId就可以复读给他并@了
}
)
foodarray = nil
menu = nil
result = nil
return 1
end
if string.find(data.Content, ".添加菜单") then
local newrecipe = data.Content:gsub(".添加菜单", "")
if data.FromUserId ==779091224 then --屏蔽某些捣乱的
result = "你说你🐎呢"
else
result = addrecipe(newrecipe)
end
luaMsg =
Api.Api_SendMsg(--调用发消息的接口
CurrentQQ,
{
toUser = data.FromGroupId, --回复当前消息的来源群ID
sendToType = 2, --2发送给群1发送给好友3私聊
sendMsgType = "TextMsg", --进行文本复读回复
groupid = 0, --不是私聊自然就为0咯
content = result.."\n完整菜单请使用命令 .今天吃啥菜单", --回复内容
atUser = 0 --是否 填上data.FromUserId就可以复读给他并@了
}
)
result = nil
return 1
end
if string.find(data.Content, ".今天吃啥菜单") then
menu = " "
foodarray = readfile()
for i=1,#foodarray.arry,1 do
menu = menu.."No"..i..foodarray.arry[i].."\n"
end
Api.Api_SendMsgV2(
CurrentQQ,
{
ToUserUid = data.FromGroupId,
GroupID = 0,
SendToType = 2, --2发送给群1发送给好友3私聊
SendMsgType = "TextMsg" , --进行文本复读回复
Content = "避免刷屏, 已发送至私聊信息"
}
)
Api.Api_SendMsgV2(
CurrentQQ,
{
ToUserUid = data.FromUserId,
GroupID = 0,
SendToType = 1, --2发送给群1发送给好友3私聊
SendMsgType = "TextMsg" , --进行文本复读回复
Content = menu
}
)
Api.Api_SendMsgV2(
CurrentQQ,
{
ToUserUid = data.FromUserId,
GroupID = data.FromGroupId,
SendToType = 3, --2发送给群1发送给好友3私聊
SendMsgType = "TextMsg" , --进行文本复读回复
Content = menu
}
)
menu = nil
end
return 1
end
function ReceiveEvents(CurrentQQ, data, extData)
return 1
end
function readfile()--读json
local tbl ={}
local f = io.input( menupath, "r" )--路径可能需要修改
local t = f:read( "*a" )
f:close()
if nil ~= t and "" ~= t then
tbl =json.decode( t )
--local s = dump_tostring(tbl);--测试打印表格
--print(s);--测试打印表格
--print(jsonData)
--print(t)
if tbl == nil then
print("Json error")
end
end
return tbl
end
function writefile(content)
local file = io.open(menupath, "w")
if file then
if file:write(content) == nil then return false end
io.close(file)
return true
else
return false
end
end
function addrecipe (newrecipe)
local result = ""
newrecipe = newrecipe:gsub(" ", "")
if newrecipe == "" then --消息为空返回错误信息
result = "您是不是要喝西北风?" --回复内容
else
local foodarray = readfile()
table.insert(foodarray.arry, newrecipe) --插入新菜
local str = json.encode(foodarray) --编码
if writefile(str) == true then --覆盖写入
result = "写入成功"
else
result = "写入失败"
end
end
return result
end
function getrandomint(n)
local bodytable = {
jsonrpc= "2.0",
method= "generateIntegers",
params= {
apiKey= "aaabbbcccddd",--此处填写apikey 需要到random.org申请
n= 1,
min= 1,
max= n,
replacement= true,
base= 10
},
id=math.random(1 , n)
}
response, error_message = http.request
(
"POST",
"https://api.random.org/json-rpc/2/invoke",
{
query = "",
headers = {["Content-Type"] = "application/json"},
body = json.encode( bodytable)
}
)
local html = response.body
local res=json.decode(html)
return res.result.random.data[1]
end