-- -- [DiceBot] - v0.1 -- Written by bonki 2003 -- -- ABOUT: -- Simple dice bot dedicated to the RPG freaks among you - greets :) -- -- AVAILABLE COMMANDS: -- !db.help: Guess. -- !db.showdice: Shows all available dice. -- !db.d : Rolls a die where is of the format XdY+Z -- If no X is given, 1 is assumed. -- If Z is given it displayed the final sum, otherwise each single roll. -- -- E.g. !db.d 4d20+5 -- !db.d d4 -- !db.d 3d12 -- -- OTHER FEATURES: -- Neat description tag counting all rolls of each die. -- -- TODO: -- Any suggestions? :) -- -- BUGS/LIMITATIONS: -- Tell me! -- -- CHANGELOG: -- v0.1: General: Initial release -- sName = "DiceBot"; sVer = "0.1"; sAbout = "A simple DiceBot dedicated to the RPG freaks among you." tDiceCount = { { 3, 0 }, { 4, 0 }, { 6, 0 }, { 8, 0 }, { 10, 0 }, { 12, 0 }, { 20, 0 }, }; bot = {}; sCrLf = "\r\n"; tCommands = {}; ------------------------------------------------------------------------------- function Main() bot = { sBotName = sName, sBotVer = sVer, sBotEmail = "bonki@no-spam.net", sBotSpeed = "Sol", sBotDescr = "DiceBot v"..sVer.." written by bonki 2003", sBotTag = "<++ V:"..sVer..",P:bonki,D:[rolls]>", iBotShare = 0 }; UpdateBotInfo(); SendBotInfo(); PrepareCommands(); frmHub:RegBot(bot.sBotName); end ------------------------------------------------------------------------------- function PrepareCommands() sHelpHeader = sCrLf..sCrLf..bot.sBotDescr.. sCrLf..sAbout..sCrLf; sOpHelpOutput = sCrLf.."OP Commands:"..sCrLf..sCrLf; sHelpOutput = sCrLf.."Commands:"..sCrLf..sCrLf; tCommands = { [ "!db.help" ] = { CmdShowHelp, 0, "\t\tDisplays this help message." }, [ "!db.showdice" ] = { CmdShowDice, 0, "\tShows this hub's all time share and user record." }, [ "!db.d" ] = { CmdRollDie, 0, "\t\tRoll the dice!" }, }; for sCmd, tCmd in tCommands do if(tCmd[2] == 1) then sOpHelpOutput = sOpHelpOutput..sCmd.."\t "..tCmd[3]..sCrLf; hasOpCommands = 1; else sHelpOutput = sHelpOutput..sCmd.."\t "..tCmd[3]..sCrLf; end end end ------------------------------------------------------------------------------- function CmdShowHelp(curUser) local sMsg = sHelpHeader; if (hasOpCommands) then sMsg = sMsg..sOpHelpOutput; end sMsg = sMsg..sHelpOutput; curUser:SendData(bot.sBotName, sMsg); end ------------------------------------------------------------------------------- function CmdShowDice(curUser) local sMsg = "Available Dice: "; for i = 1, getn(tDiceCount) do sMsg = sMsg.."d"..tDiceCount[i][1]..", "; end sMsg = strsub(sMsg, 1, -3); curUser:SendData(bot.sBotName, sMsg); end ------------------------------------------------------------------------------- function CmdRollDie(curUser, args) _, _, iTimes, iDice, iAdd = strfind(args, "(%d*)d(%d+)(.*)"); if (iAdd) then _, _, iAdd = strfind(iAdd, "+(%d+)"); end iDice = tonumber(iDice); local iDiceIndex = isDice(iDice); if (iDiceIndex) then local tRnd = {}; tDiceCount[iDiceIndex][2] = tDiceCount[iDiceIndex][2] + 1; iTimes = tonumber(iTimes) or 1; if (iTimes > 500) then curUser:SendData(sData); curUser:SendData(bot.sBotName, "Number of rolls too high!"); return; end local iSum = 0; for i = 1, iTimes do local n = random(iDice); tRnd[i] = n; iSum = iSum + n; end SendToAll(sData); if (not iAdd and iTimes > 0) then local sRnds = ""; for i = 1, iTimes do sRnds = sRnds..tRnd[i]..", "; end local sMsg = curUser.sName.." rolled: "..strsub(sRnds, 1, -3); if (iTimes > 1) then sMsg = sMsg.." (total: "..iSum..")"; end SendToAll(bot.sBotName, sMsg); else iSum = iSum + iAdd; SendToAll(bot.sBotName, curUser.sName.." rolled: "..iSum); end UpdateBotInfo(); SendBotInfo(); else curUser:SendData(sData); curUser:SendData(bot.sBotName, "There's no such dice!"); return; end end ------------------------------------------------------------------------------- function DataArrival(curUser, sData) local _, _, cmd, args = strfind(sData, "%b<>%s+(%S+)%s*([^%|]*)%|$"); if (cmd == nil) then return 0; end; cmd = strlower(cmd); if (tCommands[cmd]) then curUser:SendData(sData); if (tCommands[cmd][2] == 0 or curUser.bOperator) then tCommands[cmd][1](curUser, strlower(args)); else curUser:SendData(bot.sBotName, "You do not have sufficient rights to run that command!"); end return 1; else return 0; end end ------------------------------------------------------------------------------- function isDice(iDice) if (not iDice) then return nil; end for i = 1, getn(tDiceCount) do if (tDiceCount[i][1] == iDice) then return i; end end return nil; end ------------------------------------------------------------------------------- function SendBotInfo(curUser) if (curUser) then curUser:SendData(sBotInfo); else SendToAll(sBotInfo); end end ------------------------------------------------------------------------------- function UpdateBotInfo() local sDiceCount = ""; for i = 1, getn(tDiceCount) do sDiceCount = sDiceCount..tDiceCount[i][2].."/"; end sDiceCount = strsub(sDiceCount, 1, -2); local sBotTag = gsub(bot.sBotTag, "%[rolls%]", sDiceCount); sBotInfo = "$MyINFO $ALL "..bot.sBotName.." "..bot.sBotDescr..sBotTag.."$ $"..bot.sBotSpeed..strchar(1).."$"..bot.sBotEmail.."$"..bot.iBotShare.."$" ; end ------------------------------------------------------------------------------- function NewUserConnected(curUser) SendBotInfo(curUser); end ------------------------------------------------------------------------------- function OpConnected(curUser) SendBotInfo(curUser); end -------------------------------------------------------------------------------