add hearthstone plugin

in case of flood control, bot will now answer in PM if possible
This commit is contained in:
topkecleon 2015-09-07 11:19:36 -04:00
parent da0785f246
commit 2d2933c8e5
9 changed files with 166 additions and 43 deletions

View File

@ -198,4 +198,6 @@ Do not private message me for support.
For support for otouto as well as general Lua and bot assistance, please join the [CIS Bot Development](http://telegram.me/joinchat/05fe39f500f8f1b2d1548147a68acd2a) group. After you read the rules and the pastebin, I will assist you there. For support for otouto as well as general Lua and bot assistance, please join the [CIS Bot Development](http://telegram.me/joinchat/05fe39f500f8f1b2d1548147a68acd2a) group. After you read the rules and the pastebin, I will assist you there.
PS - Since there seems to be some confusion on the matter, otouto is *not* a port of yagop's telegram-bot. I am friends with yagop, and he is part of the Bot Development group, but our codebases are and always have been entirely separate. otouto was a CLI bot like telegram-bot before the new API, but they were entirely separate, non-intermingled projects. ##PS
Since there seems to be some confusion on the matter, otouto is **not** a port of yagop's telegram-bot. I am friends with yagop, and he is part of the Bot Development group, but our codebases are and always have been entirely separate. otouto was a CLI bot like telegram-bot before the new API, but they were entirely separate, non-intermingled projects.

View File

@ -11,7 +11,7 @@ local function send_request(url)
local tab = JSON.decode(dat) local tab = JSON.decode(dat)
if res ~= 200 then if res ~= 200 then
print('','Connection error.\n',url) print('Connection error.')
return false return false
end end

45
bot.lua
View File

@ -3,12 +3,15 @@ HTTPS= require('ssl.https')
URL = require('socket.url') URL = require('socket.url')
JSON = require('dkjson') JSON = require('dkjson')
VERSION = 2.9 VERSION = '2.10'
function on_msg_receive(msg) function on_msg_receive(msg)
if blacklist[tostring(msg.from.id)] then return end if blacklist[tostring(msg.from.id)] then return end
if floodcontrol[-msg.chat.id] then return end if floodcontrol[-msg.chat.id] then -- This stuff is useful for the moderation plugin to not be completely unusable when floodcontrol is activated.
msg.flood = msg.chat.id
msg.chat.id = msg.from.id
end
msg = process_msg(msg) msg = process_msg(msg)
@ -20,8 +23,11 @@ function on_msg_receive(msg)
for i,v in pairs(plugins) do for i,v in pairs(plugins) do
for j,w in pairs(v.triggers) do for j,w in pairs(v.triggers) do
if string.match(lower, w) then if string.match(lower, w) then
if not counter[msg.from.id] then counter[msg.from.id] = 0 end
counter[msg.from.id] = counter[msg.from.id] + 1
print(msg.from.first_name, msg.from.id, counter[msg.from.id])
if v.typing then if v.typing then
send_chat_action(msg.chat.id, 'typing') send_chat_action(msg.from.id, 'typing')
end end
local a,b = pcall(function() -- Janky error handling local a,b = pcall(function() -- Janky error handling
v.action(msg) v.action(msg)
@ -46,10 +52,12 @@ function bot_init()
print('Fetching bot information...') print('Fetching bot information...')
bot = get_me().result bot = get_me()
if not bot then while bot == false do
error('Failure fetching bot information.') print('Failure fetching bot information. Trying again...')
bot = get_me()
end end
bot = bot.result
print('Loading plugins...') print('Loading plugins...')
@ -72,19 +80,20 @@ function bot_init()
print('@'.. bot.username ..', AKA '.. bot.first_name ..' ('.. bot.id ..')') print('@'.. bot.username ..', AKA '.. bot.first_name ..' ('.. bot.id ..')')
is_started = true is_started = true
counter = {}
end end
function process_msg(msg) function process_msg(msg)
if msg.text and msg.reply_to_message and string.sub(msg.text, 1, 1) ~= '/' and msg.reply_to_message.from.id == bot.id then if msg.new_chat_participant then
msg.text = bot.first_name .. ', ' .. msg.text if msg.new_chat_participant.id ~= bot.id then
end if msg.from.id == 100547061 then return msg end
msg.text = 'hi '..bot.first_name
if msg.new_chat_participant and msg.new_chat_participant.id ~= bot.id then msg.from = msg.new_chat_participant
if msg.from.id == 100547061 then return msg end else
msg.text = 'hi '..bot.first_name msg.text = '/about'
msg.from = msg.new_chat_participant end
end end
if msg.left_chat_participant and msg.left_chat_participant.id ~= bot.id then if msg.left_chat_participant and msg.left_chat_participant.id ~= bot.id then
@ -93,16 +102,13 @@ function process_msg(msg)
msg.from = msg.left_chat_participant msg.from = msg.left_chat_participant
end end
if msg.new_chat_participant and msg.new_chat_participant.id == bot.id then
msg.text = '/about'
end
return msg return msg
end end
bot_init() bot_init()
last_update = 0 last_update = 0
counter = {}
while is_started do while is_started do
@ -123,7 +129,8 @@ while is_started do
if os.date('%S', os.time()) % 5 == 0 then -- Only check every five seconds. if os.date('%S', os.time()) % 5 == 0 then -- Only check every five seconds.
for k,v in pairs(plugins) do for k,v in pairs(plugins) do
if v.cron then if v.cron then
pcall(function() v.cron() end) a,b = pcall(function() v.cron() end)
if not a then print(b) end
end end
end end
end end

View File

@ -51,6 +51,11 @@ function PLUGIN.action(msg)
end end
jdat = JSON.decode(jstr) jdat = JSON.decode(jstr)
if not jdat['24h_avg'] then
return send_msg(msg, config.locale.errors.results)
end
local m = arg2 .. ' BTC = ' .. jdat['24h_avg']*arg2 ..' '.. arg1 .. '\n' local m = arg2 .. ' BTC = ' .. jdat['24h_avg']*arg2 ..' '.. arg1 .. '\n'
m = m .. arg2 ..' '.. arg1 .. ' = ' .. string.format("%.8f", arg2/jdat['24h_avg']) .. ' BTC' m = m .. arg2 ..' '.. arg1 .. ' = ' .. string.format("%.8f", arg2/jdat['24h_avg']) .. ' BTC'

View File

@ -25,7 +25,7 @@ local action = function(msg)
local s = input.groupid .. ' silenced for ' .. input.duration .. ' seconds.' local s = input.groupid .. ' silenced for ' .. input.duration .. ' seconds.'
send_message(-34496439, s) send_message(-34496439, s) -- Set this to whatever, or comment it out. I use it to send this data to my private bot group.
end end

107
plugins/hearthstone.lua Executable file
View File

@ -0,0 +1,107 @@
-- Get info for a hearthstone card.
local jstr, res = HTTP.request('http://hearthstonejson.com/json/AllSets.json')
if res ~= 200 then
return print('Error connecting to the Hearthstone database. hearthstone.lua will not be enabled.')
end
jdat = JSON.decode(jstr)
hs_dat = {}
for k,v in pairs(jdat) do
for key,val in pairs(v) do
table.insert(hs_dat, val)
end
end
local doc = [[
/hearthstone <card>
Get information about a Hearthstone card.
]]
local triggers = {
'^/hearthstone',
'^/hs'
}
local fmt_card = function(card)
local ctype = card.type
if card.race then
ctype = card.race
end
if card.rarity then
ctype = card.rarity .. ' ' .. ctype
end
if card.playerClass then
ctype = ctype .. ' (' .. card.playerClass .. ')'
elseif card.faction then
ctype = ctype .. ' (' .. card.faction .. ')'
end
local stats
if card.cost then
stats = card.cost .. 'c'
if card.attack then
stats = stats .. ' | ' .. card.attack .. 'a'
end
if card.health then
stats = stats .. ' | ' .. card.health .. 'h'
end
if card.durability then
stats = stats .. ' | ' .. card.durability .. 'd'
end
elseif card.health then
stats = card.health .. 'h'
end
local info = ''
if card.text then
info = card.text:gsub('</?.->',''):gsub('%$','')
if card.flavor then
info = info .. '\n' .. card.flavor
end
elseif card.flavor then
info = card.flavor
else
info = nil
end
local s = card.name .. '\n' .. ctype
if stats then
s = s .. '\n' .. stats
end
if info then
s = s .. '\n' .. info
end
return s
end
local action = function(msg)
local input = get_input(msg.text)
if not input then return send_msg(msg, doc) end
input = string.lower(input)
local output = ''
for k,v in pairs(hs_dat) do
if string.match(string.lower(v.name), input) then
output = output .. fmt_card(v) .. '\n\n'
end
end
output = trim_string(output)
if string.len(output) == 0 then
return send_msg(msg, config.locale.errors.results)
end
send_msg(msg, output)
end
return {
doc = doc,
triggers = triggers,
action = action
}

View File

@ -1,5 +1,7 @@
--[[ --[[
This plugin will ONLY WORK in Liberbot-administered groups.
This works using the settings in the "moderation" section of config.lua. This works using the settings in the "moderation" section of config.lua.
"realm" should be set to the group ID of the admin group. A negative number. "realm" should be set to the group ID of the admin group. A negative number.
"data" will be the file name of where the moderation 'database' will be stored. The file will be created if it does not exist. "data" will be the file name of where the moderation 'database' will be stored. The file will be created if it does not exist.
@ -47,6 +49,10 @@ ban.trigger = '^/modban'
ban.action = function(msg) ban.action = function(msg)
if msg.flood then
msg.chat.id = msg.flood
end
local data = load_data('moderation.json') local data = load_data('moderation.json')
if not data[tostring(msg.chat.id)] then return end if not data[tostring(msg.chat.id)] then return end
@ -84,6 +90,10 @@ kick.trigger = '^/modkick'
kick.action = function(msg) kick.action = function(msg)
if msg.flood then
msg.chat.id = msg.flood
end
local data = load_data('moderation.json') local data = load_data('moderation.json')
if not data[tostring(msg.chat.id)] then return end if not data[tostring(msg.chat.id)] then return end
@ -300,6 +310,10 @@ badmin.trigger = '^/hammer'
badmin.action = function(msg) badmin.action = function(msg)
if msg.flood then
msg.chat.id = msg.flood
end
if not config.moderation.admins[tostring(msg.from.id)] then return end if not config.moderation.admins[tostring(msg.from.id)] then return end
local target = get_target(msg) local target = get_target(msg)

View File

@ -1,4 +1,4 @@
reminders = load_data('reminders.json') reminders = {}
local doc = [[ local doc = [[
/remind <delay> <message> /remind <delay> <message>
@ -6,8 +6,7 @@ local doc = [[
]] ]]
local triggers = { local triggers = {
'^/remind$', '^/remind'
'^/remind '
} }
local action = function(msg) local action = function(msg)
@ -25,21 +24,20 @@ local action = function(msg)
if string.len(msg.text) <= string.len(delay) + 9 then if string.len(msg.text) <= string.len(delay) + 9 then
return send_msg(msg, 'Please include a reminder.') return send_msg(msg, 'Please include a reminder.')
end end
local text = string.sub(msg.text, string.len(delay)+10) -- this is gross local text = string.sub(msg.text, string.len(delay)+10)
if msg.from.username then if msg.from.username then
text = text .. '\n@' .. msg.from.username text = text .. '\n@' .. msg.from.username
end end
local delay = tonumber(delay) local delay = tonumber(delay)
local reminder = { local rem = {
alarm = os.time() + (delay * 60), alarm = os.time() + (delay * 60),
chat_id = msg.chat.id, chat_id = msg.chat.id,
text = text text = text
} }
table.insert(reminders, reminder) table.insert(reminders, rem)
save_data('reminders.json', reminders)
if delay <= 1 then if delay <= 1 then
delay = (delay * 60) .. ' seconds' delay = (delay * 60) .. ' seconds'
@ -55,20 +53,10 @@ end
local cron = function() local cron = function()
reminders = load_data('reminders.json') for i,v in ipairs(reminders) do
for k,v in pairs(reminders) do
if os.time() > v.alarm then if os.time() > v.alarm then
local a = send_message(v.chat_id, 'Reminder: '..v.text) send_message(v.chat_id, text)
if a then table.remove(reminders, i)
reminders[k] = nil
save_data('reminders.json', reminders)
end
end
-- If the bot is no longer in the chat, he won't be able to send reminders.
-- To prevent abuse, check for old reminders and remove them.
if v.alarm < os.time() + 3600 then
reminders[k] = nil
save_data('reminders.json', reminders)
end end
end end

View File

@ -42,7 +42,7 @@ local action = function(msg)
return send_msg(msg, config.locale.errors.results) return send_msg(msg, config.locale.errors.results)
end end
--[[ Uncomment this for more than one-paragraph summaries. --[[ Uncomment this block for more than one-paragraph summaries.
local l = text:find('<h2>') local l = text:find('<h2>')
if l then if l then
text = text:sub(1, l-2) text = text:sub(1, l-2)