merge upstream
Colors on Non valids. Check msg.to.id and msg.from.id function load_from_file(file) vardump uses serpent tabs on scandir Failed on Free games (no price info) Google with unescaped URLs
This commit is contained in:
parent
94fea4e9aa
commit
fe78a49367
2
.gitignore
vendored
2
.gitignore
vendored
@ -25,3 +25,5 @@ plugins/create_sticker.lua
|
||||
plugins/derpibooru_nsfw.lua
|
||||
plugins/derpibooru.lua
|
||||
plugins/rss.lua
|
||||
plugins/url_info.lua
|
||||
plugins/meme.lua
|
18
bot/bot.lua
18
bot/bot.lua
@ -38,23 +38,33 @@ end
|
||||
function msg_valid(msg)
|
||||
-- Dont process outgoing messages
|
||||
if msg.out then
|
||||
print("Not valid: msg from us")
|
||||
print('\27[36mNot valid: msg from us\27[39m')
|
||||
return false
|
||||
end
|
||||
|
||||
-- Before bot was started
|
||||
if msg.date < now then
|
||||
print("Not valid: old msg")
|
||||
print('\27[36mNot valid: old msg\27[39m')
|
||||
return false
|
||||
end
|
||||
|
||||
if msg.unread == 0 then
|
||||
print("Not valid: readed")
|
||||
print('\27[36mNot valid: readed\27[39m')
|
||||
return false
|
||||
end
|
||||
|
||||
if msg.service then
|
||||
print("Not valid: service")
|
||||
print('\27[36mNot valid: service\27[39m')
|
||||
return false
|
||||
end
|
||||
|
||||
if not msg.to.id then
|
||||
print('\27[36mNot valid: To id not provided\27[39m')
|
||||
return false
|
||||
end
|
||||
|
||||
if not msg.from.id then
|
||||
print('\27[36mNot valid: From id not provided\27[39m')
|
||||
return false
|
||||
end
|
||||
|
||||
|
@ -116,49 +116,16 @@ function download_to_file(url, file_name)
|
||||
end
|
||||
|
||||
|
||||
function vardump(value, depth, key)
|
||||
local linePrefix = ""
|
||||
local spaces = ""
|
||||
|
||||
if key ~= nil then
|
||||
linePrefix = "["..key.."] = "
|
||||
end
|
||||
|
||||
if depth == nil then
|
||||
depth = 0
|
||||
else
|
||||
depth = depth + 1
|
||||
for i=1, depth do spaces = spaces .. " " end
|
||||
end
|
||||
|
||||
if type(value) == 'table' then
|
||||
mTable = getmetatable(value)
|
||||
if mTable == nil then
|
||||
print(spaces ..linePrefix.."(table) ")
|
||||
else
|
||||
print(spaces .."(metatable) ")
|
||||
value = mTable
|
||||
end
|
||||
for tableKey, tableValue in pairs(value) do
|
||||
vardump(tableValue, depth, tableKey)
|
||||
end
|
||||
elseif type(value) == 'function' or
|
||||
type(value) == 'thread' or
|
||||
type(value) == 'userdata' or
|
||||
value == nil
|
||||
then
|
||||
print(spaces..tostring(value))
|
||||
else
|
||||
print(spaces..linePrefix.."("..type(value)..") "..tostring(value))
|
||||
end
|
||||
function vardump(value)
|
||||
print(serpent.block(value, {comment=false}))
|
||||
end
|
||||
|
||||
-- taken from http://stackoverflow.com/a/11130774/3163199
|
||||
function scandir(directory)
|
||||
local i, t, popen = 0, {}, io.popen
|
||||
for filename in popen('ls -a "'..directory..'"'):lines() do
|
||||
i = i + 1
|
||||
t[i] = filename
|
||||
i = i + 1
|
||||
t[i] = filename
|
||||
end
|
||||
return t
|
||||
end
|
||||
@ -465,3 +432,18 @@ end
|
||||
function sleep(n)
|
||||
os.execute("sleep " .. tonumber(n))
|
||||
end
|
||||
|
||||
-- Function to read data from files
|
||||
function load_from_file(file)
|
||||
local f = io.open(file, "r+")
|
||||
-- If file doesn't exists
|
||||
if f == nil then
|
||||
-- Create a new empty table
|
||||
serialize_to_file({}, file)
|
||||
print ('Created file', file)
|
||||
else
|
||||
print ('Data loaded from file', file)
|
||||
f:close()
|
||||
end
|
||||
return loadfile (file)()
|
||||
end
|
@ -1,14 +1,18 @@
|
||||
function googlethat(query)
|
||||
local api = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
|
||||
local api = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&safe=active&hl=de&"
|
||||
local parameters = "q=".. (URL.escape(query) or "")
|
||||
|
||||
-- Do the request
|
||||
local res, code = https.request(api..parameters)
|
||||
if code ~=200 then return nil end
|
||||
local data = json:decode(res)
|
||||
|
||||
local results={}
|
||||
for key,result in ipairs(data.responseData.results) do
|
||||
table.insert(results,{result.titleNoFormatting, result.url})
|
||||
table.insert(results, {
|
||||
result.titleNoFormatting,
|
||||
result.unescapedUrl or result.url
|
||||
})
|
||||
end
|
||||
return results
|
||||
end
|
||||
@ -22,9 +26,8 @@ function stringlinks(results)
|
||||
end
|
||||
|
||||
function run(msg, matches)
|
||||
vardump(matches)
|
||||
local results = googlethat(matches[1])
|
||||
return stringlinks(results)
|
||||
local results = googlethat(matches[1])
|
||||
return stringlinks(results)
|
||||
end
|
||||
|
||||
return {
|
||||
|
@ -1,12 +1,11 @@
|
||||
|
||||
-- See https://wiki.teamfortress.com/wiki/User:RJackson/StorefrontAPI
|
||||
|
||||
do
|
||||
|
||||
local BASE_URL = 'http://store.steampowered.com/api/appdetails/?appids='
|
||||
local BASE_URL = 'http://store.steampowered.com/api/appdetails/'
|
||||
local DESC_LENTH = 200
|
||||
|
||||
function unescape(str)
|
||||
local function unescape(str)
|
||||
str = string.gsub( str, '<', '<' )
|
||||
str = string.gsub( str, '>', '>' )
|
||||
str = string.gsub( str, '"', '"' )
|
||||
@ -17,28 +16,40 @@ function unescape(str)
|
||||
return str
|
||||
end
|
||||
|
||||
function get_steam_data (appid)
|
||||
local url = BASE_URL..appid
|
||||
local function get_steam_data (appid)
|
||||
local url = BASE_URL
|
||||
url = url..'?appids='..appid
|
||||
url = url..'&cc=DE'
|
||||
local res,code = http.request(url)
|
||||
if code ~= 200 then return nil end
|
||||
local data = json:decode(res)[appid].data
|
||||
return data
|
||||
end
|
||||
|
||||
local function price_info (data)
|
||||
local price = '' -- If no data is empty
|
||||
|
||||
function send_steam_data(data, receiver)
|
||||
local description = string.sub(unescape(data.about_the_game:gsub("%b<>", "")), 1, DESC_LENTH) .. '...'
|
||||
local title = data.name
|
||||
local price = "€"..(data.price_overview.initial/100)
|
||||
local sale_price = "€"..(data.price_overview.final/100)
|
||||
local percent_savings = data.price_overview.discount_percent
|
||||
local price_display = price
|
||||
|
||||
if percent_savings ~= 0 then
|
||||
price_display = price.." -> "..sale_price.." ("..percent_savings.."%)"
|
||||
if data then
|
||||
local initial = data.initial
|
||||
local final = data.final or data.initial
|
||||
local min = math.min(data.initial, data.final)
|
||||
price = tostring(min/100)
|
||||
if data.discount_percent and initial ~= final then
|
||||
price = price..data.currency..' ('..data.discount_percent..'% OFF)'
|
||||
end
|
||||
price = price..' (US)'
|
||||
end
|
||||
|
||||
local text = title..' '..price_display..'\n'..description
|
||||
return price
|
||||
end
|
||||
|
||||
|
||||
local function send_steam_data(data, receiver)
|
||||
local description = string.sub(unescape(data.about_the_game:gsub("%b<>", "")), 1, DESC_LENTH) .. '...'
|
||||
local title = data.name
|
||||
local price = price_info(data.price_overview)
|
||||
|
||||
local text = title..' '..price..'\n'..description
|
||||
local image_url = data.header_image
|
||||
local cb_extra = {
|
||||
receiver = receiver,
|
||||
@ -48,7 +59,7 @@ function send_steam_data(data, receiver)
|
||||
end
|
||||
|
||||
|
||||
function run(msg, matches)
|
||||
local function run(msg, matches)
|
||||
local appid = matches[1]
|
||||
local data = get_steam_data(appid)
|
||||
local receiver = get_receiver(msg)
|
||||
|
Reference in New Issue
Block a user