Skip to content

Instantly share code, notes, and snippets.

@SwadicalRag
Last active March 26, 2017 05:26
Show Gist options
  • Save SwadicalRag/13191f4a847531fb20b5eff230031389 to your computer and use it in GitHub Desktop.
Save SwadicalRag/13191f4a847531fb20b5eff230031389 to your computer and use it in GitHub Desktop.
Example code that uses nana2's rich message api
function cookie.poll(question,options,time)
assert(#options > 1,"Need more than 1 option!")
assert(#options <= 9,"No more than 9 options!")
time = time or 15
local emoteMap = {
["1"] = "one",
["2"] = "two",
["3"] = "three",
["4"] = "four",
["5"] = "five",
["6"] = "six",
["7"] = "seven",
["8"] = "eight",
["9"] = "nine",
}
for k,v in pairs(emoteMap) do emoteMap[v] = k end
local function optionToEmote(num)
num = tostring(num)
return emoteMap[num]
end
local msg = {
title = question,
fields = {},
description = "",
}
local results = {}
for i,optionData in ipairs(options) do
msg.fields[#msg.fields + 1] = {
name = tostring(i),
value = tostring(optionData),
}
results[i] = 0
end
local m
local mFuture = message.sendRich(msg):thence(function(mClass)
m = mClass
for i,optionData in ipairs(options) do
m:React(optionToEmote(i)):await()
end
m:OnReact(function(emote,_,created)
if not emoteMap[emote] then return end
local i = tonumber(emoteMap[emote])
if created then
results[i] = results[i] + 1
else
results[i] = results[i] - 1
end
end)
end):thence(function()
local function tick()
time = time - 1
if not m then return timer.Simple(1,tick) end
if time >= 0 then
timer.Simple(1,tick)
msg.description = string.format("%d seconds left to vote",time)
m:EditRich(msg)
else
m:Delete()
local amt,winner = 0,""
for i,option in ipairs(options) do
if results[i] > amt then
winner = option.." ("..results[i]..")"
amt = results[i]
elseif results[i] == amt then
if winner ~= "" then
winner = winner.." or "
end
winner = winner..option.." ("..results[i]..")"
end
end
message.sendRich({
title = question,
description = winner,
})
end
end
timer.Simple(1,tick)
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment