Add argument checks

This commit is contained in:
Michael De Roover 2021-03-01 11:16:26 +01:00
parent 80bb3deceb
commit 072e2b51a0
Signed by: vim
GPG Key ID: 075496E232CE04CB

View File

@ -31,28 +31,56 @@ sendmsg(){
fmt=$(sed 's/[>#+-=|{}.!]/\\&/g' <<< $1 | sed 's/\t//g')
sendraw "$fmt"
}
escape(){
sed "s/[$1]/\\\&/g" <<< $2
}
sendraw(){
curl -sX POST "https://api.telegram.org/bot$token/sendMessage" \
-d "chat_id=$chat" -d "parse_mode=markdownv2" \
-d "disable_web_page_preview=true" \
--data-urlencode "text=$1"
}
sendlog(){
chat=$logchat
sendmsg "$1"
}
sendfile(){
curl -sF document=@"$1" "https://api.telegram.org/bot$token/sendDocument?chat_id=$chat"
}
sendpic(){
curl -sX POST "https://api.telegram.org/bot$token/sendPhoto" \
-d "chat_id=$chat" -d "parse_mode=markdownv2" \
-d "photo=$1" -d "caption=$2"
}
# Internal checks
args(){
if [ -z ${args[*]} ]
then
sendmsg "Don't just click me like that. See /help@$bot for more info on how to use this bot."
exit 1
fi
}
admin(){
case $user in
ghnou) true ;;
*) fail ;;
esac
debug "Admin @$user tried to execute $cmd and was approved."
}
fail(){
debug "User @$user tried to execute $cmd and was rejected."
sendmsg "Sorry, you are not allowed to use this command!"
exit 1
}
# Debugging commands
debug(){
[ ! -z $debug ] && echo "[DEBUG] $1" >&2
@ -86,6 +114,7 @@ alive(){
}
cv(){
args
stats=$(curl -s "https://corona.lmao.ninja/v2/countries/${args[*]}")
mapfile -t cv <<< $(jq -cr '.[]' <<< $stats)
if [ "${cv[4]}" == "0" ]
@ -114,6 +143,7 @@ id(){
}
ud(){
args
enc=$(echo "${args[*]}" | sed "s/\ /%20/g")
res=$(curl -s "https://api.urbandictionary.com/v0/define?term=$enc")
if jq -re '.list[0].definition' <<< $res
@ -135,6 +165,7 @@ konata(){
}
konachan(){
args
query=$(sed "s/\ /_/g" <<< ${args[*]})
data=$(curl -s "https://konachan.net/post.json?limit=1&page=1&tags=order:random%20$query")
jpeg=$(jq -r '.[0].jpeg_url' <<< $data)
@ -152,6 +183,7 @@ konachan(){
}
4c(){
args
tmp="/tmp/4chan"
json="$tmp/${args[0]}.json"
[ ! -d $tmp ] && mkdir $tmp
@ -166,13 +198,13 @@ konachan(){
}
lfy(){
args
query=$(sed "s/\ /+/g" <<< ${args[*]})
sendmsg "[Let me look that up for you](https://lmgtfy.app/?s=d&iee=1&q=$query)"
}
ip(){
if [ ! -z ${args[0]} ]
then
args
ip=${args[0]}
res=$(curl -s http://ip-api.com/json/$ip)
# See https://0x0.st/Nlc0 for reference.
@ -184,7 +216,6 @@ ip(){
*ISP:* ${data[10]}
*ASN:* ${data[12]}
*Time:* $(TZ=${data[9]} date +'%H:%M %Z')"
fi
}
repo(){
@ -194,6 +225,7 @@ repo(){
# Administrative
mp3(){
admin
args
tmp="/tmp/mp3"
[ ! -d $tmp ] && mkdir $tmp
sendmsg "Downloading, this might take a while..."
@ -224,20 +256,6 @@ term(){
\`$(echo ${args[*]} | bash - 2>&1)\`"
}
fail(){
debug "User @$user tried to execute $cmd and was rejected."
sendmsg "Sorry, you are not allowed to use this command!"
exit 1
}
admin(){
case $user in
ghnou) true ;;
*) fail ;;
esac
debug "Admin @$user tried to execute $cmd and was approved."
}
# Notes and such
anyone(){
sendmsg "Quite possibly. [Why do you ask?](https://dontasktoask.com)"