From 307363a4cc6f68f1b20cb36a16092af4d1110bc1 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 11 Apr 2019 20:33:39 +0200 Subject: [PATCH] functions user_is_creator user_is_admin --- bashbot.sh | 21 +++++++++++++++++++-- commands.sh | 10 ++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index f45adfb..752f4a0 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -10,7 +10,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.6-dev2-8-g649fe44 +#### $$VERSION$$ v0.6-dev2-10-gdb64978 # # Exit Codes: # - 0 sucess (hopefully) @@ -99,7 +99,8 @@ FORWARD_URL=$URL'/forwardMessage' INLINE_QUERY=$URL'/answerInlineQuery' ME_URL=$URL'/getMe' DELETE_URL=$URL'/deleteMessage' -ME=$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | grep '\["result","username"\]' | cut -f 2 | cut -d '"' -f 2) +GETMEMBER_URL=$URL'/getChatMember' +ME="$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | grep '\["result","username"\]' | cut -f 2 | cut -d '"' -f 2)" FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/' @@ -205,6 +206,11 @@ delete_message() { res="$(curl -s "$DELETE_URL" -F "chat_id=$1" -F "message_id=$2")" } +# usage: status="$(get_chat_member_status "chat" "user")" +get_chat_member_status() { + curl -s "$GETMEMBER_URL" -F "chat_id=$1" -F "user_id=$2" | ./JSON.sh/JSON.sh -s | sed -n -e '/\["result","status"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' +} + kick_chat_member() { res="$(curl -s "$KICK_URL" -F "chat_id=$1" -F "user_id=$2")" } @@ -217,6 +223,17 @@ leave_chat() { res="$(curl -s "$LEAVE_URL" -F "chat_id=$1")" } +user_is_creator() { + if [ "$1" == "$2" ] || [ "$(get_chat_member_status "$1" "$2")" == "creator" ]; then return 0; fi + return 1 +} + +user_is_admin() { + local me; me="$(get_chat_member_status "$1" "$2")" + if [ "${me}" == "creator" ] || [ "${me}" == "administrator" ]; then return 0; fi + return 1 +} + answer_inline_query() { case "$2" in "article") diff --git a/commands.sh b/commands.sh index 9cc6db7..c89369d 100755 --- a/commands.sh +++ b/commands.sh @@ -4,7 +4,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.6-dev2-9-gf4e277b +#### $$VERSION$$ v0.6-dev2-11-gaeb9a6a # # shellcheck disable=SC2154 # shellcheck disable=SC2034 @@ -121,9 +121,11 @@ else bashbot_help "${CHAT[ID]}" ;; - '/leavechat') - send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*" - leave_chat "${CHAT[ID]}" + '/leavechat') # bot leave chat if user is admin in chat + if user_is_admin "${CHAT[ID]}" "${USER[ID]}"; then + send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*" + leave_chat "${CHAT[ID]}" + fi ;; '/kickme')