Advertisement
Terrah

emote

Jan 3rd, 2015
1,533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.40 KB | None | 0 0
  1. local cache = {};
  2.  
  3. local function GetAllEmotes(chan)
  4.  
  5.     local list = cache[chan:lower()];
  6.  
  7.     if list ~= nil then
  8.  
  9.         if type(list) == "table" then
  10.             return list;
  11.         else
  12.             return nil;
  13.         end
  14.     end
  15.  
  16.     local body, code, headers, status = MOD.HTTPSGet("https://api.twitch.tv/kraken/chat/"..chan.."/emoticons");
  17.  
  18.     local tbl = JSONDecode(body);
  19.  
  20.     if tbl.error ~= nil then
  21.         cache[chan:lower()] = 0;
  22.         return nil;
  23.     end
  24.  
  25.     local lst = {};
  26.  
  27.     for n=1,#tbl.emoticons do
  28.  
  29.         if tbl.emoticons[n].subscriber_only and tbl.emoticons[n].state then
  30.             lst[tbl.emoticons[n].regex] = tbl.emoticons[n].url
  31.         end
  32.     end
  33.  
  34.     cache[chan:lower()] = lst;
  35.  
  36.     return lst;
  37. end
  38.  
  39. local cmd = function(msg,usr,chan)
  40.  
  41.     local split = {};
  42.     for match in (msg.." "):gmatch("(.-) ") do
  43.         table.insert(split, match);
  44.     end
  45.  
  46.     local channel = split[1];
  47.     local emote = split[2];
  48.  
  49.     if channel == nil or channel == "" then
  50.         channel = chan:lower();
  51.     end
  52.  
  53.     local lst = GetAllEmotes(channel);
  54.  
  55.     if lst == nil then
  56.  
  57.         print("No such channel exists");
  58.         return;
  59.     end
  60.  
  61.     if emote == nil or emote == "" then
  62.         for k,v in pairs(lst) do
  63.             print(k);
  64.         end
  65.     else
  66.  
  67.         emote = emote:lower();
  68.  
  69.         local url = "";
  70.         for k,v in pairs(lst) do
  71.             if k:lower() == emote then
  72.                 url = v;
  73.                 break;
  74.             end
  75.         end
  76.  
  77.         if url == "" then
  78.             print("No such emote exists");
  79.         else
  80.             print(url);
  81.         end
  82.     end
  83. end
  84.  
  85. return cmd;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement