-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaging.lua
63 lines (55 loc) · 1.56 KB
/
paging.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
local emojis = {
backArrow = '\226\172\133',
fwdArrow = '\226\158\161',
stop = '\226\143\185'
}
local paging = {
pages = {},
timeout = 60
}
function paging.init(resultsMsg, queryMsg, data, handler)
paging.pages[resultsMsg.id] = {
message = resultsMsg,
query = queryMsg,
author = queryMsg.author,
data = data,
handle = handler,
endTime = os.time() + paging.timeout
}
resultsMsg:addReaction(emojis.backArrow)
resultsMsg:addReaction(emojis.fwdArrow)
resultsMsg:addReaction(emojis.stop)
return paging.pages[queryMsg.id]
end
local function onReaction(reaction, userId)
if userId == client.user.id then return end
local page = paging.pages[reaction.message.id]
if page and page.endTime > os.time() then
-- if reaction.message.id ~= page.message.id then return end -- redundant
if userId ~= page.author.id and not config.owners[userId] then return end
local emoji = reaction.emojiName
if emoji == emojis.backArrow or emoji == emojis.fwdArrow then
local fwd = emoji == emojis.fwdArrow
page:handle(fwd)
page.endTime = os.time() + paging.timeout
elseif emoji == emojis.stop then
page.message:clearReactions()
page.endTime = 0
paging.pages[reaction.message.id] = nil
end
end
end
timer.setInterval(1000, function()
for msgId, page in next, paging.pages do
if page.endTime < os.time() then
coroutine.wrap(function()
page.message:setContent("(:hourglass:)")
page.message:clearReactions()
end)()
paging.pages[msgId] = nil
end
end
end)
client:on("reactionAdd", onReaction)
client:on("reactionRemove", onReaction)
return paging