Merge branch 'master' into less-magic

This commit is contained in:
Brayden Banks 2016-04-12 06:47:30 -07:00
commit 80930f389c
8 changed files with 64 additions and 58 deletions

2
.gitignore vendored
View File

@ -1,5 +1,7 @@
plugins/mokubot* plugins/mokubot*
plugins/qtbot* plugins/qtbot*
plugins/mjolnir*
plugins/antisquigbot*
*.db *.db
lua-tg lua-tg
drua-tg drua-tg

View File

@ -12,10 +12,6 @@ function bindings.sendRequest(url)
local dat, res = HTTPS.request(url) local dat, res = HTTPS.request(url)
if res ~= 200 then
return false, res
end
local tab = JSON.decode(dat) local tab = JSON.decode(dat)
if not tab.ok then if not tab.ok then
@ -114,6 +110,16 @@ function bindings:forwardMessage(chat_id, from_chat_id, message_id, disable_noti
end end
function bindings:kickChatMember(chat_id, user_id)
local url = self.BASE_URL .. '/kickChatMember?chat_id=' .. chat_id .. '&user_id=' .. user_id
return bindings.sendRequest(self, url)
end
function bindings:unbanChatMember(chat_id, user_id)
local url = self.BASE_URL .. '/unbanChatMember?chat_id=' .. chat_id .. '&user_id=' .. user_id
return bindings.sendRequest(self, url)
end
-- TODO: More of this. -- TODO: More of this.
function bindings:sendPhotoID(chat_id, file_id, caption, reply_to_message_id, disable_notification) function bindings:sendPhotoID(chat_id, file_id, caption, reply_to_message_id, disable_notification)

30
bot.lua
View File

@ -5,7 +5,7 @@ local instance = {}
local bindings = require('bindings') -- Load Telegram bindings. local bindings = require('bindings') -- Load Telegram bindings.
local utilities = require('utilities') -- Load miscellaneous and cross-plugin functions. local utilities = require('utilities') -- Load miscellaneous and cross-plugin functions.
bot.version = '3.5' bot.version = '3.6'
function bot:init() -- The function run when the bot is started or reloaded. function bot:init() -- The function run when the bot is started or reloaded.
@ -44,22 +44,6 @@ function bot:init() -- The function run when the bot is started or reloaded.
self.database.users = self.database.users or {} -- Table to cache userdata. self.database.users = self.database.users or {} -- Table to cache userdata.
self.database.users[tostring(self.info.id)] = self.info self.database.users[tostring(self.info.id)] = self.info
-- Migration code. Remove in 3.6.
if self.database.lastfm then
for k,v in pairs(self.database.lastfm) do
if not self.database.users[k] then self.database.users[k] = {} end
self.database.users[k].lastfm = v
end
end
-- Migration code. Remove in 3.6.
if self.database.nicknames then
for k,v in pairs(self.database.nicknames) do
if not self.database.users[k] then self.database.users[k] = {} end
self.database.users[k].nickname = v
end
end
end end
function bot:on_msg_receive(msg) -- The fn run whenever a message is received. function bot:on_msg_receive(msg) -- The fn run whenever a message is received.
@ -77,25 +61,17 @@ function bot:on_msg_receive(msg) -- The fn run whenever a message is received.
end end
if msg.date < os.time() - 5 then return end -- Do not process old messages. if msg.date < os.time() - 5 then return end -- Do not process old messages.
if not msg.text then msg.text = msg.caption or '' end
if msg.reply_to_message and msg.reply_to_message.caption then msg = utilities.enrich_message(msg)
msg.reply_to_message.text = msg.reply_to_message.caption
end -- If the replied-to message has a caption, make that its text.
if msg.text:match('^/start .+') then if msg.text:match('^/start .+') then
msg.text = '/' .. utilities.input(msg.text) msg.text = '/' .. utilities.input(msg.text)
msg.text_lower = msg.text:lower()
end end
for _,v in ipairs(self.plugins) do for _,v in ipairs(self.plugins) do
for _,w in pairs(v.triggers) do for _,w in pairs(v.triggers) do
if string.match(msg.text:lower(), w) then if string.match(msg.text:lower(), w) then
-- a few shortcuts
msg.chat.id_str = tostring(msg.chat.id)
msg.from.id_str = tostring(msg.from.id)
msg.text_lower = msg.text:lower()
msg.from.name = utilities.build_name(msg.from.first_name, msg.from.last_name)
local success, result = pcall(function() local success, result = pcall(function()
return v.action(self, msg) return v.action(self, msg)
end) end)

View File

@ -2,12 +2,12 @@ return {
-- Your authorization token from the botfather. -- Your authorization token from the botfather.
bot_api_key = '', bot_api_key = '',
-- Your Telegram ID.
admin = 00000000,
-- Differences, in seconds, between your time and UTC. -- Differences, in seconds, between your time and UTC.
time_offset = 0, time_offset = 0,
-- Two-letter language code. -- Two-letter language code.
lang = 'en', lang = 'en',
-- Your Telegram ID.
admin = 00000000,
-- The channel, group, or user to send error reports to. -- The channel, group, or user to send error reports to.
-- If this is not set, errors will be printed to the console. -- If this is not set, errors will be printed to the console.
log_chat = nil, log_chat = nil,

View File

@ -47,15 +47,6 @@ function administration:init()
flood = {} flood = {}
} }
-- Migration code: Remove this in v1.8.
-- Most recent group activity is now cached for group listings.
if not self.database.administration.activity then
self.database.administration.activity = {}
for k,_ in pairs(self.database.administration.groups) do
table.insert(self.database.administration.activity, k)
end
end
-- Migration code: Remove this in v1.9. -- Migration code: Remove this in v1.9.
-- Groups have single governors now. -- Groups have single governors now.
for _,group in pairs(self.database.administration.groups) do for _,group in pairs(self.database.administration.groups) do
@ -1011,6 +1002,7 @@ function administration.init_command(self_)
photo = drua.get_photo(msg.chat.id), photo = drua.get_photo(msg.chat.id),
founded = os.time() founded = os.time()
} }
administration:update_desc(self, msg.chat.id)
for i,_ in ipairs(administration.flags) do for i,_ in ipairs(administration.flags) do
self.database.administration.groups[msg.chat.id_str].flags[i] = false self.database.administration.groups[msg.chat.id_str].flags[i] = false
end end

View File

@ -43,10 +43,6 @@ local yesno_answers = {
function eightball:action(msg) function eightball:action(msg)
if msg.reply_to_message then
msg = msg.reply_to_message
end
local message local message
if msg.text_lower:match('y/n%p?$') then if msg.text_lower:match('y/n%p?$') then

View File

@ -12,23 +12,26 @@ function hearthstone:init()
print('Downloading Hearthstone database...') print('Downloading Hearthstone database...')
self.database.hearthstone = { -- This stuff doesn't play well with lua-sec. Disable it for now; hack in curl.
expiration = os.time() + 600000 --local jstr, res = HTTPS.request('https://api.hearthstonejson.com/v1/latest/enUS/cards.json')
} --if res ~= 200 then
-- print('Error connecting to hearthstonejson.com.')
-- print('hearthstone.lua will not be enabled.')
-- return
--end
--local jdat = JSON.decode(jstr)
local jstr, res = HTTPS.request('http://hearthstonejson.com/json/AllSets.json') local s = io.popen('curl -s https://api.hearthstonejson.com/v1/latest/enUS/cards.json'):read('*all')
if res ~= 200 then local d = JSON.decode(s)
if not d then
print('Error connecting to hearthstonejson.com.') print('Error connecting to hearthstonejson.com.')
print('hearthstone.lua will not be enabled.') print('hearthstone.lua will not be enabled.')
return return
end end
local jdat = JSON.decode(jstr)
for _,v in pairs(jdat) do self.database.hearthstone = d
for _,val in pairs(v) do self.database.hearthstone.expiration = os.time() + 600000
table.insert(self.database.hearthstone, val)
end
end
print('Download complete! It will be stored for a week.') print('Download complete! It will be stored for a week.')

View File

@ -171,7 +171,7 @@ function utilities:user_from_message(msg)
if msg.reply_to_message then if msg.reply_to_message then
target = msg.reply_to_message.from target = msg.reply_to_message.from
elseif input and tonumber(input) then elseif input and tonumber(input) then
target.id = input target.id = tonumber(input)
if self.database.users[input] then if self.database.users[input] then
for k,v in pairs(self.database.users[input]) do for k,v in pairs(self.database.users[input]) do
target[k] = v target[k] = v
@ -300,3 +300,34 @@ function utilities.with_http_timeout(timeout, fun)
fun() fun()
HTTP.TIMEOUT = original HTTP.TIMEOUT = original
end end
function utilities.enrich_user(user)
user.id_str = tostring(user.id)
user.name = utilities.build_name(user.first_name, user.last_name)
return user
end
function utilities.enrich_message(msg)
if not msg.text then msg.text = msg.caption or '' end
msg.text_lower = msg.text:lower()
msg.from = utilities.enrich_user(msg.from)
msg.chat.id_str = tostring(msg.chat.id)
if msg.reply_to_message then
if not msg.reply_to_message.text then
msg.reply_to_message.text = msg.reply_to_message.caption or ''
end
msg.reply_to_message.text_lower = msg.reply_to_message.text:lower()
msg.reply_to_message.from = utilities.enrich_user(msg.reply_to_message.from)
msg.reply_to_message.chat.id_str = tostring(msg.reply_to_message.chat.id)
end
if msg.forward_from then
msg.forward_from = utilities.enrich_user(msg.forward_from)
end
if msg.new_chat_participant then
msg.new_chat_participant = utilities.enrich_user(msg.new_chat_participant)
end
if msg.left_chat_participant then
msg.left_chat_participant = utilities.enrich_user(msg.left_chat_participant)
end
return msg
end