This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/plugins/dice.lua
2015-07-02 18:15:52 -04:00

72 lines
1.4 KiB
Lua

local PLUGIN = {}
PLUGIN.doc = [[
!roll [range]
Roll a die. Use any positive number for range or use D&D notation.
Example: !roll 4D100 will roll a 100-sided die four times.
]]
PLUGIN.triggers = {
'^!roll'
}
function PLUGIN.action(msg)
math.randomseed(os.time())
local input = get_input(msg.text)
if not input then
input = 6
else
input = string.upper(input)
end
if tonumber(input) then
range = tonumber(input)
rolls = 1
elseif string.find(input, 'D') then
local dloc = string.find(input, 'D')
if dloc == 1 then
rolls = 1
else
rolls = string.sub(input, 1, dloc-1)
end
range = string.sub(input, dloc+1)
if not tonumber(rolls) or not tonumber(range) then
return send_msg(msg, 'Invalid syntax.')
end
else
return send_msg(msg, 'Invalid syntax.')
end
if tonumber(rolls) == 1 then
results = 'Random (1-' .. range .. '):\t'
elseif tonumber(rolls) > 1 then
results = rolls .. 'D' .. range .. ':\n'
else
return send_msg(msg, 'Invalid syntax.')
end
if tonumber(range) < 2 then
return send_msg(msg, 'Invalid syntax.')
end
if tonumber(rolls) > 100 then
return send_msg(msg, 'Maximum dice is 100.')
end
if tonumber(range) > 100000 then
return send_msg(msg, 'Maximum range is 100000.')
end
for i = 1, tonumber(rolls) do
results = results .. math.random(range) .. '\t'
end
send_msg(msg, results)
end
return PLUGIN