lots of changes I never bothered to structure...

This commit is contained in:
Jinks 2018-02-01 15:44:30 +01:00
parent 7581f32fa4
commit 6fffe876e8
7 changed files with 356 additions and 16 deletions

View file

@ -4,21 +4,76 @@ local json = require("json")
local https = require("ssl.https")
local ltn12 = require("ltn12")
local msc = require("misc")
--local curl = require("cURL")
local function fetch_data (endpoint)
local function fetch_data(endpoint)
local response = {}
hdrs = { Accept = "application/vnd.twitchtv.v3+json", ["Client-ID"] = cid }
local r,c,h = https.request{url = "https://api.twitch.tv/kraken/"..endpoint,
headers = { accept = "application/vnd.twitchtv.v3+json"},
headers = hdrs,
sink = ltn12.sink.table(response)}
return json.decode(table.concat(response))
end
--[[
local function fetch_data_curl (url)
t = {}
curl.easy()
:setopt_url("https://api.twitch.tv/kraken/"..url)
:setopt_httpheader{
"Accept: application/vnd.twitchtv.v3+json",
"Client-ID:".. cid
}
:setopt_writefunction(function(buf)
table.insert(t,buf)
return #buf
end)
:perform()
return json.decode(table.concat(t))
end
]]
local COLOR = true
local ON = true
local outfile = io.output()
local function _message(msg_type, msg, color)
if ON then
local endcolor = ""
if COLOR and outfile == io.stdout then
color = color or "\027[1;30m"
endcolor = "\027[0m"
else
color = ""
endcolor = ""
end
outfile:write(color .. msg_type .. ": " .. msg .. endcolor .. "\n")
end
end
-- }}}
function red(msg)
_message("ERR", msg, "\027[0;31m")
end
function yellow(msg)
_message("WARN", msg, "\027[0;33m")
end
local function match_any( str, pattern_list )
for _, pattern in ipairs( pattern_list ) do
local w = string.match( str, pattern )
if w then return w end
end
end
plugin.uptime = function (target, from, arg)
local channel = string.sub(target, 2)
if arg and arg ~= "" then
channel = arg
end
local j = fetch_data("streams/"..channel)
--print_r(j)
if j.stream then
if j.stream ~= json.decode("null") then
irc.say(target, j.stream.channel.display_name.." is streaming ["..j.stream.game.."] for "..elapsed(j.stream.created_at)..".")
@ -29,3 +84,56 @@ plugin.uptime = function (target, from, arg)
irc.say(target, "Stream "..channel.." not found.")
end
end
plugin.followage = function (target, from, arg)
local channel = string.sub(target, 2)
if arg and arg ~= "" then
user = arg
else
user = from
end
local j = fetch_data("users/"..user.."/follows/channels/"..channel)
--print_r(j)
if j.channel and j.created_at then
if j.created_at ~= json.decode("null") then
irc.say(target, user.." is following "..j.channel.display_name.." for "..elapsed(j.created_at)..".")
end
elseif j.message then
irc.say(target, "Error: " .. j.message)
else
irc.say(target, "Error: Could not find any data.")
end
end
plugin.dilfa = function (target, from, arg)
irc.say(target, "!img https://i.imgur.com/2rUOXMT.png")
end
plugin.owl = function (target, from, arg)
irc.say(target, "!img https://i.imgur.com/C5Uz6Pv.gif")
end
plugin.bus = function (target, from, arg)
irc.say(target, "!img https://memeguy.com/photos/images/short-bus-243553.jpg")
end
--[[
callback.fospam = function (target, from, message)
local patterns = {
".*son%s.*%svillain.*",
".*son%s.*%sbadguy.*",
".*son%s.*%sbad%sguy.*",
"have.*to.*bomb%s.*boston.*",
"have.*to.*nuke%s.*boston.*",
"have.*to.*bomb%s.*commonwealth.*",
"have.*to.*nuke%s.*commonwealth.*",
}
local spam = match_any(message:lower(), patterns)
if spam then
irc.say(target, ".ban "..from)
yellow("Banned "..from.." for spoliers ["..spam.."]!")
end
end
]]

View file

@ -1,7 +1,90 @@
-- Small utilty commands that return static text and don't fit anywhere else.
booms = 0
last_boom = os.time()
last_unboom = os.time()
-- Bot's code repository
plugin.code = function (target, from, arg)
irc.say(target, "My source code can be found at https://github.com/jinks/LuaMeat")
end
plugin.source = plugin.code
plugin.ping = function (target, from, arg)
irc.say(target, "PONG")
end
plugin.say = function(target, from, arg)
if from == "jinks___" then
local sp = arg:find("%s") or 1
local ch = arg:sub(1,sp-1)
for channel in irc.channels() do
if (channel.name == ch) then
print(arg)
target = channel
arg = arg:sub(sp+1)
print(arg)
break
end
end
irc.say(target, arg)
end
end
plugin.join = function (target, from, arg)
if (from == "jinks___") then
irc.join(arg)
end
end
plugin.part = function (target, from, arg)
if (from == "jinks___") then
if arg:len() > 0 then
irc.part(arg)
else
irc.part(target)
end
end
end
plugin.hail = function (target, from, arg)
irc.say(target, "HAIL HYDRATION! And our lord and savior Crop Rotation. But really: http://bit.ly/1ipyhGd")
end
plugin.score = function (target, from, arg)
irc.say(target, "BOOM counter is " .. booms)
end
plugin.unboom = function (target, from, arg)
now = os.time()
if now-last_unboom > 20 then
last_unboom = now
booms = booms - 1
irc.say(target, "BOOM counter is now " .. booms)
end
end
plugin.boom = function (target, from, arg)
now = os.time()
if now-last_boom > 20 then
last_boom = now
booms = booms + 1
irc.say(target, "BOOM counter is now " .. booms)
end
end
plugin.resetboom = function (target, from, arg)
if (from == "jinks___" or from == "4kbshort") then
booms = 0
irc.say(target, "BOOM counter reset.")
end
end
plugin.setboom = function (target, from, arg)
if (from == "jinks___" or from == "4kbshort") then
num = tonumber(arg)
if num ~= nil then
booms = num
end
irc.say(target, "BOOM counter is " .. booms)
end
end

View file

@ -6,17 +6,23 @@ local function fetch_title (videoid)
--local response = {}
local r,c,h = https.request("https://www.googleapis.com/youtube/v3/videos?id="..videoid.."&key="..yt_api_key.."&part=snippet&fields=items(snippet(title))")
local j = json.decode(r)
if j and next(j.items) then
if j and j.items and next(j.items) then
return j.items[1].snippet.title
end
end
callback.youtube = function (target, from, message)
local ytid = message:match(".*https?://w?w?w?%.?youtube.com/watch%?v=(%g*)%s*.*")
if ytid then
local t = fetch_title(ytid)
if t then
irc.say(target, "Youtube video title: "..t)
end
local ytid = message:match(".*https?://w?w?w?%.?youtube.com/watch%?v=(%g*)(#?.*)%s*.*")
local ybid = message:match(".*https?://w?w?w?%.?youtu.be/(%g*)%s*.*")
if ybid then
ytid = ybid
end
if not ytid then return end
if ytid:find("#") then
ytid = ytid:match("(.*)#")
end
local t = fetch_title(ytid)
if t then
irc.say(target, "Youtube video title: "..t)
end
end