diff --git a/otouto/bot.lua b/otouto/bot.lua index 0941f66..9319265 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -93,6 +93,10 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec end msg = pre_process_msg(self, msg, config) + if is_service_msg(msg) then + msg = service_modify_msg(msg) + end + for _, plugin in ipairs(self.plugins) do match_plugins(self, msg, config, plugin) end diff --git a/otouto/plugins/games.lua b/otouto/plugins/games.lua index e5e0222..6a8b5d4 100644 --- a/otouto/plugins/games.lua +++ b/otouto/plugins/games.lua @@ -5,9 +5,7 @@ local xml = require("xml") games.command = 'game ' function games:init(config) - games.triggers = { - "^/game (.+)$" - } + games.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('game', true).table games.doc = [[* ]]..config.cmd_pat..[[game*_ _: Sendet Infos zum Spiel]] end @@ -129,8 +127,17 @@ function games:send_game_data(game_id, self, msg) end -function games:action(msg, config, matches) - local game = URL.escape(matches[1]) +function games:action(msg, config) + local game = utilities.input(msg.text) + if not game then + if msg.reply_to_message and msg.reply_to_message.text then + game = msg.reply_to_message.text + else + utilities.send_message(self, msg.chat.id, fun.doc, true, msg.message_id, true) + return + end + end + local game_id = games:get_game_id(game) if not game_id then utilities.send_reply(self, msg, 'Spiel nicht gefunden!') diff --git a/otouto/plugins/leave_group.lua b/otouto/plugins/leave_group.lua deleted file mode 100644 index c494962..0000000 --- a/otouto/plugins/leave_group.lua +++ /dev/null @@ -1,49 +0,0 @@ -local leave_group = {} - -leave_group.triggers = { - '/nil' -} - -local report_to_admin = true -- set to false to not be notified, when Bot leaves groups without you - -function leave_group:check_for_admin(msg, self, config) - local result = bindings.request(self, 'getChatMember', { - chat_id = msg.chat.id, - user_id = config.admin - } ) - if not result.ok then - print('Konnte nicht prüfen, ob Admin in Gruppe ist! Verlasse sie sicherheitshalber...') - return false - end - if result.result.status ~= "member" and result.result.status ~= "administrator" and result.result.status ~= "creator" then - return false - else - return true - end -end - -function leave_group:pre_process(msg, self, config) - if msg.group_chat_created or msg.new_chat_member then - local admin_in_group = leave_group:check_for_admin(msg, self, config) - if not admin_in_group then - print('Admin ist nicht in der Gruppe, verlasse sie deshalb...') - utilities.send_reply(self, msg, 'Dieser Bot wurde in eine fremde Gruppe hinzugefügt. Dies wird gemeldet!\nThis bot was added to foreign group. This incident will be reported!') - local result = bindings.request(self, 'leaveChat', { - chat_id = msg.chat.id - } ) - local chat_name = msg.chat.title - local chat_id = msg.chat.id - local from = msg.from.name - local from_id = msg.from.id - if report_to_admin then - utilities.send_message(self, config.admin, '#WARNUNG: Bot wurde in fremde Gruppe hinzugefügt:\nGruppenname: '..chat_name..' ('..chat_id..')\nHinzugefügt von: '..from..' ('..from_id..')') - end - end - end - return msg -end - -function leave_group:action(msg) -end - -return leave_group diff --git a/otouto/plugins/entergroup.lua b/otouto/plugins/service_entergroup.lua similarity index 81% rename from otouto/plugins/entergroup.lua rename to otouto/plugins/service_entergroup.lua index 7423d82..5872bd0 100644 --- a/otouto/plugins/entergroup.lua +++ b/otouto/plugins/service_entergroup.lua @@ -1,7 +1,8 @@ local entergroup = {} entergroup.triggers = { - '/nil' + '^//tgservice (new_chat_member)$', + '^//tgservice (left_chat_member)$' } function entergroup:chat_new_user(msg, self) @@ -38,17 +39,14 @@ function entergroup:chat_del_user(msg, self) utilities.send_reply(self, msg, text, true) end -function entergroup:pre_process(msg, self) - if msg.new_chat_member then +function entergroup:action(msg, config, matches) + if not is_service_msg(msg) then return end -- Bad attempt at trolling! + + if matches[1] == 'new_chat_member' then entergroup:chat_new_user(msg, self) - elseif msg.left_chat_member then + elseif matches[1] == 'left_chat_member'then entergroup:chat_del_user(msg, self) end - - return msg -end - -function entergroup:action(msg) end return entergroup diff --git a/otouto/plugins/service_leave_group.lua b/otouto/plugins/service_leave_group.lua new file mode 100644 index 0000000..61b2bb0 --- /dev/null +++ b/otouto/plugins/service_leave_group.lua @@ -0,0 +1,45 @@ +local leave_group = {} + +leave_group.triggers = { + '^//tgservice group_chat_created$', + '^//tgservice supergroup_chat_created$' +} + +local report_to_admin = true -- set to false to not be notified, when Bot leaves groups without you + +function leave_group:check_for_admin(msg, self, config) + local result = bindings.request(self, 'getChatMember', { + chat_id = msg.chat.id, + user_id = config.admin + } ) + if not result.ok then + print('Konnte nicht prüfen, ob Admin in Gruppe ist! Verlasse sie sicherheitshalber...') + return false + end + if result.result.status ~= "member" and result.result.status ~= "administrator" and result.result.status ~= "creator" then + return false + else + return true + end +end + +function leave_group:action(msg) + if not is_service_msg(msg) then return end -- Bad attempt at trolling! + local admin_in_group = leave_group:check_for_admin(msg, self, config) + if not admin_in_group then + print('Admin ist nicht in der Gruppe, verlasse sie deshalb...') + utilities.send_reply(self, msg, 'Dieser Bot wurde in eine fremde Gruppe hinzugefügt. Dies wird gemeldet!\nThis bot was added to foreign group. This incident will be reported!') + local result = bindings.request(self, 'leaveChat', { + chat_id = msg.chat.id + } ) + local chat_name = msg.chat.title + local chat_id = msg.chat.id + local from = msg.from.name + local from_id = msg.from.id + if report_to_admin then + utilities.send_message(self, config.admin, '#WARNUNG: Bot wurde in fremde Gruppe hinzugefügt:\nGruppenname: '..chat_name..' ('..chat_id..')\nHinzugefügt von: '..from..' ('..from_id..')') + end + end +end + +return leave_group diff --git a/otouto/plugins/stats.lua b/otouto/plugins/stats.lua index 3ba40bd..869ad76 100644 --- a/otouto/plugins/stats.lua +++ b/otouto/plugins/stats.lua @@ -80,9 +80,9 @@ end function stats:pre_process(msg, self) -- Ignore service msg - if msg.service then -- check how Bot API handles service msgs, will update this + if is_service_msg(msg) then print('Service message') - return + return msg end if msg.left_chat_member then diff --git a/otouto/utilities.lua b/otouto/utilities.lua index 9fa0ce6..aa0b204 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -631,6 +631,62 @@ function is_sudo(msg, config) return var end +function service_modify_msg(msg) + if msg.new_chat_member then + msg.text = '//tgservice new_chat_member' + msg.text_lower = msg.text + elseif msg.left_chat_member then + msg.text = '//tgservice left_chat_member' + msg.text_lower = msg.text + elseif msg.new_chat_title then + msg.text = '//tgservice new_chat_title' + msg.text_lower = msg.text + elseif msg.new_chat_photo then + msg.text = '//tgservice new_chat_photo' + msg.text_lower = msg.text + elseif msg.group_chat_created then + msg.text = '//tgservice group_chat_created' + msg.text_lower = msg.text + elseif msg.supergroup_chat_created then + msg.text = '//tgservice supergroup_chat_created' + msg.text_lower = msg.text + elseif msg.channel_chat_created then + msg.text = '//tgservice channel_chat_created' + msg.text_lower = msg.text + elseif msg.migrate_to_chat_id then + msg.text = '//tgservice migrate_to_chat_id' + msg.text_lower = msg.text + elseif msg.migrate_from_chat_id then + msg.text = '//tgservice migrate_from_chat_id' + msg.text_lower = msg.text + end + return msg +end + +function is_service_msg(msg) + local var = false + if msg.new_chat_member then + var = true + elseif msg.left_chat_member then + var = true + elseif msg.new_chat_title then + var = true + elseif msg.new_chat_photo then + var = true + elseif msg.group_chat_created then + var = true + elseif msg.supergroup_chat_created then + var = true + elseif msg.channel_chat_created then + var = true + elseif msg.migrate_to_chat_id then + var = true + elseif msg.migrate_from_chat_id then + var = true + end + return var +end + function post_petition(url, arguments, headers) local url, h = string.gsub(url, "http://", "") local url, hs = string.gsub(url, "https://", "")