From 68c1ae226cd05e778c252bb3717466810f2babf6 Mon Sep 17 00:00:00 2001 From: topkecleon Date: Fri, 20 May 2016 20:47:13 -0400 Subject: [PATCH] administration.lua: kicking via tg is much faster than kicking via API control.lua: added /script command --- bindings.lua | 17 ++++++++++++----- bot.lua | 5 ++++- plugins/administration.lua | 15 +++++++-------- plugins/control.lua | 13 +++++++++++++ utilities.lua | 2 -- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/bindings.lua b/bindings.lua index d303f67..d960193 100755 --- a/bindings.lua +++ b/bindings.lua @@ -187,11 +187,18 @@ function bindings:sendPhotoID(chat_id, file_id, caption, reply_to_message_id, di end -function bindings.curlRequest(curl_command) - -- Use at your own risk. Will not check for success. - - io.popen(curl_command) - +function bindings.curlRequest(curl_command, give_output) + if give_output then + local s = io.popen(curl_command):read('*all') + local tab = JSON.encode(s) + if not tab then return false end + if not tab.ok then + return false, tab.description + end + return tab + else + io.popen(curl_command) + end end function bindings:sendPhoto(chat_id, photo, caption, reply_to_message_id, disable_notification) diff --git a/bot.lua b/bot.lua index c15cbfd..424c0c9 100755 --- a/bot.lua +++ b/bot.lua @@ -19,7 +19,10 @@ function bot:init() -- The function run when the bot is started or reloaded. end -- Fetch bot information. Try until it succeeds. - repeat self.info = bindings.getMe(self) until self.info + repeat + print('Fetching bot information...') + self.info = bindings.getMe(self) + until self.info self.info = self.info.result -- Load the "database"! ;) diff --git a/plugins/administration.lua b/plugins/administration.lua index 49f0402..4000f03 100644 --- a/plugins/administration.lua +++ b/plugins/administration.lua @@ -242,9 +242,7 @@ function administration:update_desc(chat) end function administration:kick_user(chat, target, reason) - if not bindings.kickChatMember(self, chat, target) then - drua.kick_user(chat, target) - end + drua.kick_user(chat, target) utilities.handle_exception(self, target..' kicked from '..chat, reason) end @@ -1037,11 +1035,11 @@ function administration.init_command(self_) else administration.kick_user(self, msg.chat.id, target.id, 'hammered by '..msg.from.id) self.database.blacklist[target.id_str] = true - --for k,v in pairs(self.database.administration.groups) do - --if not v.flags[6] then - --administration.kick_user(self, k, target.id) - --end - --end + for k,v in pairs(self.database.administration.groups) do + if not v.flags[6] then + drua.kick_user(k, target.id) + end + end local output = target.name .. ' has been globally banned.' if group.flags[6] == true then group.bans[target.id_str] = true @@ -1114,6 +1112,7 @@ function administration.init_command(self_) end table.insert(self.database.administration.activity, msg.chat.id_str) bindings.sendReply(self, msg, 'I am now administrating this group.') + drua.channel_set_admin(msg.chat.id, self.info.id, 2) end }, diff --git a/plugins/control.lua b/plugins/control.lua index b1c6c75..69e0186 100644 --- a/plugins/control.lua +++ b/plugins/control.lua @@ -6,6 +6,7 @@ local utilities = require('utilities') function control:init() control.triggers = utilities.triggers(self.info.username):t('reload'):t('halt').table + table.insert(control.triggers, '^/script') end function control:action(msg) @@ -30,6 +31,18 @@ function control:action(msg) elseif msg.text:match('^'..utilities.INVOCATION_PATTERN..'halt') then self.is_started = false bindings.sendReply(self, msg, 'Stopping bot!') + elseif msg.text:match('^'..utilities.INVOCATION_PATTERN..'script') then + local input = msg.text:match('^'..utilities.INVOCATION_PATTERN..'script\n(.+)') + if not input then + bindings.sendReply(self, msg, 'usage: ```\n/script\n/command \n...\n```', true) + return + end + input = input .. '\n' + for command in input:gmatch('(.-)\n') do + command = utilities.trim(command) + msg.text = command + bot.on_msg_receive(self, msg) + end end end diff --git a/utilities.lua b/utilities.lua index 326b967..2d09cbe 100755 --- a/utilities.lua +++ b/utilities.lua @@ -144,14 +144,12 @@ function utilities.build_name(first, last) end function utilities:resolve_username(input) - input = input:gsub('^@', '') for _,v in pairs(self.database.users) do if v.username and v.username:lower() == input:lower() then return v end end - end function utilities:user_from_message(msg, no_extra)