Advertisement
Terrah

the meow command

Oct 13th, 2014
2,641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.14 KB | None | 0 0
  1. --[[
  2.  
  3. Available standard functions:
  4.  
  5.     _VERSION
  6.     assert
  7.     error
  8.     ipairs
  9.     next
  10.     pairs
  11.     pcall
  12.     select
  13.     tonumber
  14.     tostring
  15.     type
  16.     unpack
  17.     xpcall
  18.     coroutine.create
  19.     coroutine.resume
  20.     coroutine.running
  21.     coroutine.status
  22.     coroutine.wrap
  23.     coroutine.yield
  24.     math.abs
  25.     math.acos
  26.     math.asin
  27.     math.atan
  28.     math.atan2
  29.     math.ceil
  30.     math.cos
  31.     math.cosh
  32.     math.deg
  33.     math.exp
  34.     math.fmod
  35.     math.floor
  36.     math.frexp
  37.     math.huge
  38.     math.ldexp
  39.     math.log
  40.     math.log10
  41.     math.max
  42.     math.min
  43.     math.modf
  44.     math.pi
  45.     math.pow
  46.     math.rad
  47.     math.random
  48.     math.sin
  49.     math.sinh
  50.     math.sqrt
  51.     math.tan
  52.     math.tanh
  53.     os.clock
  54.     os.difftime
  55.     os.time
  56.     string.byte
  57.     string.char
  58.     string.find
  59.     string.format
  60.     string.gmatch
  61.     string.gsub
  62.     string.len
  63.     string.lower
  64.     string.match
  65.     string.reverse
  66.     string.sub
  67.     string.upper
  68.     table.insert
  69.     table.maxn
  70.     table.remove
  71.     table.sort
  72.  
  73. Custom functions
  74.  
  75.     ME(); -> returns the current user
  76.     Channel(); -> returns the name of the channel we're in
  77.     print(string); -> adds string to the response
  78.     GetUsers(); -> returns an array containing all the users in the channel
  79.     UserStatus(string) -> returns 0 if the user isnt here, 1 if the user is here and 2 if the user is here and is a mod. If the user is present it returns a second result which is a string containing the title
  80.     GetCommands() -> returns an array containing the available commands
  81.     GetMsg(user) -> return the last message sent by the user, returns nil if none
  82.     JSONEncode(tbl) -> returns the table as json encoded
  83.     JSONDecode(data) -> decodes a json string into a table
  84.     Timestamp() -> timestamp in CET
  85.     Date(formatting,time) -> wrapper around os.date, if time is nil it takes the current time  
  86.     HTML(html) -> decodes html to a lua table (https://github.com/luaforge/html/tree/master/html) as html.parsestr
  87.  
  88.     (MOD is only available to moderators in the channel)
  89.  
  90.     The timer functions are defined within this script!
  91.     MOD.Timer(name,seconds,event) -> runs the event after seconds has elapsed (fire&forget), event can be a string or a function, if seconds is 0 or nil the timer is deleted. If the timer already exists its overwritten/restarted.
  92.  
  93.     he timer functions are defined within this script!
  94.     MOD.GetTimers() -> returns an array containing the names of all running timers
  95.  
  96.     Get stream is defined inside of this script!
  97.     MOD.GetStream(nameofstream) -> returns a table or nil if the stream is live or not
  98.  
  99.     MOD.GetVar(key,channel) -> returns a value from the database
  100.     MOD.SetVar(key,value,arrayid) -> Sets or updates a value in the database, arrayid is optional
  101.     MOD.DelVar(key) -> delete a value from the database
  102.     MOD.GetArray(arrayid, chan, descending,limit, offset) -> returns a table containing key:values as set by SetVar with arrayid   
  103.     MOD.DelArray(arrayid) -> deletes all records with the arrayid
  104.  
  105.     MOD.GetGlobalVar(key) -> returns a value from the database, shared across all channels
  106.     MOD.SetGlobalVar(key,value) -> Sets or updates a value in the database, shared across all channels
  107.     MOD.DelGlobalVar(key) -> delete a value from the database, shared across all channels
  108.  
  109.     MOD.Commercial(sec) -> runs a commercial for sec (30|60|90|120|150|180) default is 30
  110.     MOD.ResetCD(user) -> resets the cooldown, if a user is supplied it resets it for the user too
  111.     MOD.HTTPGet(url) -> returns content and status, content is nil on failure
  112.     MOD.HTTPSGet(url) -> Same as above but HTTPS, returns body, code, headers, status in that order
  113.  
  114.     MOD.HTTPPost(url,headers,request_body) -> sends a post request, headers should be a table, request_body should be a string
  115.     MOD.HTTPSPost(url,headers,request_body) -> same as above but https/ssl
  116.     -> POST functions returns string resource, number code, table response_headers, table response_body
  117.  
  118.     MOD.StartupScript(pastebintag*) -> Sets the channel's startup script
  119.     MOD.Save() -> Saves your commands and the saveable content in the MOD table
  120.     MOD.Reload() -> Reloads/Resets the channel environment and reruns the startup script
  121.  
  122.     MOD.AddCom(string,value); -> creates a command where string is the command without the !.
  123.     if value is a function it must be defined beforehand or within the same chunk as AddCom, IE:
  124.     AddCom("Test", function(msg,usr,channel) print("cat"); end); AddCom additionally runs MOD.Save automatically.
  125.  
  126.     AddCom passes the message, user and channel name in that order to its calling function.
  127.  
  128.     MOD.PasteBin(pastebintag*) -> runs a script off pastebin as if it was a local script, returns the result and a status boolean, if the status boolean is false the result will be an error message.
  129.  
  130.     MOD.Timeout(user,time) -> Timesout a user, if time isnt provided the timeout is 1 second (Kitsunebot must be a mod)
  131.     MOD.Ban(user) -> perma bans user (Kitsunebot must be a mod)
  132.     MOD.Unban(user) -> unbans a previously banned user (Kitsunebot must be a mod)
  133.  
  134.     MOD.TimestampToSec(stamp) -> Convert a "yyyy-mm-ddThh:mm:ssZ" timestamp to unix
  135.     MOD.SecondToSpan(unixtimestamp) -> Converts a unixtimestamp to timespan DD:HH:MM:SS
  136.  
  137.     Additional notes:
  138.  
  139.     pastebintag* = this is the last letters in a pastebin
  140.     for example the link to this script is http://pastebin.com/y68mc6pM
  141.     the tag is as thus y68mc6pM
  142.  
  143.     The MOD table is only available for moderators and its contents (strings, numbers, tables)
  144.     are saved when MOD.Save is called.
  145.  
  146.     MOD.MODONLY (default false) if true kitsunebot will only respond to mods (custom commands included).
  147.  
  148.     IE: !kitsunebot MOD.MODONLY=true (or false) to toggle it on or off.
  149.  
  150.     MOD.COOLDOWN (default 15) The cooldown of which kitsunebot will respond to commands, channel mods are exempt from this cooldown
  151.  
  152.     MOD.INDIVIDUAL_CD (default 20) same as above but for the individual user
  153. ]]
  154.  
  155. --Upvalue function
  156. local function Meow(msg,usr,chan)
  157.  
  158.     --No param supplied
  159.     if msg == "" then
  160.        
  161.         local stream = MOD.GetStream(chan:sub(2));
  162.         local streamstatus ="";
  163.        
  164.         if stream == nil then
  165.             streamstatus = "Stream not online";
  166.         else
  167.             streamstatus = "Stream is online with " .. tostring(stream.viewers) .. " viewers";
  168.         end
  169.  
  170.         print("MEOW! " .. usr .. " you are in " .. chan .. " there are " .. #GetUsers() .. " users in the chat! " .. streamstatus );
  171.     else
  172.  
  173.         --Special dumb cases
  174.         if msg:lower() == usr:lower() then
  175.             print("MEOW! " .. usr .. " 4Head");
  176.             return;
  177.         elseif msg:lower() == "kitsunebot" then
  178.             print("MEOW! " .. usr .. " Kreygasm" );
  179.             return;
  180.         end
  181.  
  182.         --Get the status 1 here, 2 mod, 0 not here
  183.         local status,title = UserStatus(msg);
  184.  
  185.         if status == 1 then
  186.             print("MEOW! " .. usr .. " " .. msg .. " is here!");
  187.         elseif status == 2 then
  188.             print("MEOW! " .. usr .. " " .. msg .. " is here as a "..title:lower().."!");
  189.         else
  190.             print("MEOW! " .. usr .. " " .. msg .. " is not here!");
  191.         end
  192.     end
  193. end
  194.  
  195. --Helper function
  196. local function IsBroadcaster(user)
  197.     local channel = Channel():sub(2):lower();
  198.     return channel  == user:lower();
  199. end
  200.  
  201. --Turn seconds into a span IE "hh:mm:ss"
  202. local function SecondToSpan(sec)
  203.  
  204.         --Break out the components from the seconds
  205.         local days = math.floor((sec/3600)/24);
  206.                 local hours = math.floor((sec/3600));
  207.         local mins = math.floor((sec-(hours*3600))/60);
  208.         local seconds = math.floor(sec%60);
  209.  
  210.         --Pad out the span with 0 when necessary
  211.         if mins < 10 then
  212.                 mins = "0"..tostring(mins);
  213.         end
  214.  
  215.         if seconds < 10 then
  216.                 seconds = "0"..tostring(seconds);
  217.         end
  218.  
  219.  
  220.         --return as "dd:hh:mm:ss"
  221.                 if days > 0 then
  222.  
  223.                         hours = hours - (days*24)
  224.  
  225.                         if hours < 10 then
  226.                 hours= "0"..tostring(hours);
  227.                         end
  228.  
  229.                         return tostring(days) .. ":" .. tostring(hours) .. ":" .. tostring(mins) .. ":" .. tostring(seconds);
  230.                 else
  231.  
  232.                         if hours < 10 then
  233.                 hours= "0"..tostring(hours);
  234.                         end
  235.                 end
  236.  
  237.         return tostring(hours) .. ":" .. tostring(mins) .. ":" .. tostring(seconds);
  238. end
  239.  
  240. MOD.SecondToSpan = SecondToSpan;
  241.  
  242. local function TimestampToSec(stamp)
  243.  
  244.     local tbl = {};
  245.  
  246.     tbl.year,tbl.month,tbl.day,tbl.hour,tbl.min,tbl.sec = string.match(stamp, "(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)");
  247.  
  248.     --+1 gmt
  249.     tbl.hour = tbl.hour + 2;
  250.  
  251.     return os.time(tbl);
  252. end
  253.  
  254. MOD.TimestampToSec = TimestampToSec;
  255.  
  256. --Helper function
  257. local function GetSub(msg)
  258.  
  259.         if(msg:find("subscribed!"))then
  260.                 local pos = msg:find(" ",0,true);
  261.                 return msg:sub(1,pos-1);
  262.         end
  263.  
  264.         return nil;
  265. end
  266. --example message function
  267. local function Message(user, msg)
  268.  
  269.     if user:lower() == "twitchnotify" and type(MOD.EventSub) == "function" then
  270.         local sub = GetSub(msg);
  271.         if sub~=nil and sub ~= "" then
  272.             return MOD.EventSub(sub);
  273.         end
  274.     end
  275.  
  276.     --Save the last message and user
  277.     MOD.LastMsg = msg;
  278.     MOD.LastUsr = user;
  279.  
  280.     if type(MOD.EventMsg) == "function" then
  281.         return MOD.EventMsg(user,msg);
  282.     end
  283. end
  284.  
  285. MOD.Ticks=0;
  286.  
  287. --timer
  288. local timerevents = {};
  289. --example tick function
  290. local function Tick(channel)
  291.    
  292.     if type(MOD.EventTick) == "function" then
  293.         local ret = MOD.EventTick(user,msg);
  294.         if ret ~= nil then
  295.             return ret;
  296.         end
  297.     end
  298.  
  299.     MOD.Ticks = MOD.Ticks + 1;
  300.  
  301.     local now = os.time();
  302.  
  303.     for k,v in pairs(timerevents)do
  304.         if v.time <= now then
  305.  
  306.             timerevents[k]=nil;
  307.  
  308.             if v.action ~= nil then
  309.            
  310.                 if v.action ~= nil then
  311.                     if type(v.action) == "function" then
  312.                         v.action();
  313.                         return;
  314.                     else
  315.                         print(tostring(v.action));
  316.                     end
  317.                 end
  318.             end
  319.         end
  320.     end
  321. end
  322.  
  323. function MOD.GetTimers()
  324.    
  325.     local list = {};
  326.     for k,v in pairs(timerevents)do
  327.         table.insert(list,k);
  328.     end
  329.  
  330.     return list;
  331. end
  332.  
  333. function MOD.Timer(name,seconds,event)
  334.  
  335.     if event == nil then
  336.         timerevents[name]=nil;
  337.         return;
  338.     end
  339.  
  340.     if type(event) ~= "function" and type(event) ~= "string" then
  341.         error("Event must be a function or a string");
  342.         return;
  343.     end
  344.  
  345.     local entry = {};
  346.  
  347.     entry.time = os.time()+seconds;
  348.     entry.action = event;
  349.  
  350.     timerevents[name] = entry;
  351. end
  352.  
  353. function MOD.GetStream(name)
  354.  
  355.     local body, code, headers, status = MOD.HTTPSGet("https://api.twitch.tv/kraken/streams/"..name);
  356.  
  357.     if code ~= 200 then
  358.         return nil,tostring(code);
  359.     elseif body == nil then
  360.         return nil,"not found";
  361.     end
  362.  
  363.     local raw = JSONDecode(body);
  364.  
  365.     if raw == nil then
  366.         return nil,"could not decode response";
  367.     elseif raw.error ~= nil then
  368.         return nil,tostring(raw.error);
  369.     elseif type(raw.stream) ~= "table" then
  370.         return nil,"offline";
  371.     end
  372.  
  373.     local ret = {};
  374.     ret.viewers = raw.stream.viewers;
  375.     ret.game = raw.stream.game;
  376.     ret.name = raw.stream.channel.display_name;
  377.     ret.partner = raw.stream.channel.partner;
  378.     ret.views = raw.stream.channel.views;
  379.     ret.language = raw.stream.channel.language;
  380.     ret.mature = raw.stream.channel.mature;
  381.     ret.status = raw.stream.channel.status;
  382.     ret.started = raw.stream.created_at;
  383.     ret.fps = raw.stream.average_fps;
  384.     ret.followers = raw.stream.channel.followers;
  385.  
  386.     return ret;
  387. end
  388.  
  389. local function SetIVCD(msg, usr, chan)
  390.  
  391.     if not msg or msg == "" or UserStatus(usr) ~= 2 then
  392.         return;
  393.     end
  394.  
  395.     msg = tonumber(msg);
  396.  
  397.     if msg == nil then
  398.        
  399.         print("Not a number");
  400.         return;
  401.     end
  402.  
  403.     MOD.INDIVIDUAL_CD = msg;
  404.     print("Individual cooldown set: " .. tostring(MOD.INDIVIDUAL_CD) );
  405.     MOD.Save();
  406. end
  407.  
  408.  
  409. local function SetCD(msg, usr, chan)
  410.  
  411.     if not msg or msg == "" or UserStatus(usr) ~= 2 then
  412.         return;
  413.     end
  414.    
  415.     msg = tonumber(msg);
  416.  
  417.     if msg == nil then
  418.        
  419.         print("Not a number");
  420.         return;
  421.     end
  422.  
  423.     MOD.COOLDOWN = msg;
  424.     print("Global cooldown set: " .. tostring(MOD.COOLDOWN) );
  425.     MOD.Save();
  426. end
  427.  
  428. --Register the function
  429. MOD.AddCom("meow",Meow);
  430.  
  431. MOD.AddCom("ivcd",SetIVCD);
  432. MOD.AddCom("gcd",SetCD);
  433.  
  434. --Set the events; these functions are only available in the startup script
  435. MOD.SetUserMsgEvent(Message);
  436. MOD.SetTickEvent(Tick);
  437.  
  438. if type(MOD.KitsuneMod)=="string" and MOD.KitsuneMod~="" then
  439.     return MOD.PasteBin(MOD.KitsuneMod);
  440. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement