Skip to content
This repository has been archived by the owner on May 15, 2021. It is now read-only.

Commit

Permalink
Core bug Fixes + New Custom Alive + Upstreams (#10)
Browse files Browse the repository at this point in the history
* some cherry-pick

fixing merge conflicts

* fix some more typos (#151)

* add broad Exception to ytdl stuff

* Update config.py

* Will use it later

* some alive tweaks

* alive tweaks

* some random bugs

https://nekobin.com/riqagareso.txt

* Update alive.py

* Check for remote 'heroku'

* Update alive.py

* custon alive support

* Update help.py

* Update api.sh

* Deleted and readded whole init/

- improved stability

* Testing updater

Co-authored-by: rking32 <[email protected]>
Co-authored-by: TheWeaver786 <[email protected]>
  • Loading branch information
3 people authored Sep 17, 2020
1 parent 935e691 commit 45995bd
Show file tree
Hide file tree
Showing 29 changed files with 343 additions and 293 deletions.
40 changes: 27 additions & 13 deletions init/checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,36 @@ _checkDefaultVars() {
set +a
done
DOWN_PATH=${DOWN_PATH%/}/
[[ -n $HEROKU_API_KEY && -n $HEROKU_APP_NAME ]] \
&& declare -gx HEROKU_GIT_URL="https://api:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git"
if [[ -n $HEROKU_API_KEY && -n $HEROKU_APP_NAME ]]; then
local herokuErr=$(runPythonCode '
import heroku3
try:
if "'$HEROKU_APP_NAME'" not in heroku3.from_key("'$HEROKU_API_KEY'").apps():
raise Exception("Invalid HEROKU_APP_NAME \"'$HEROKU_APP_NAME'\"")
except Exception as e:
print(e)')
[[ $herokuErr ]] && quit "heroku response > $herokuErr"
declare -g HEROKU_GIT_URL="https://api:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git"
fi
for var in G_DRIVE_IS_TD LOAD_UNOFFICIAL_PLUGINS; do
eval $var=$(tr "[:upper:]" "[:lower:]" <<< ${!var})
done
local nameAndUName=$(grep -oP "(?<=\/\/)(.+)(?=\@)" <<< $DATABASE_URL)
DATABASE_URL=$(sed 's/$nameAndUName/$(printf "%q\n" $nameAndUName)/' <<< $DATABASE_URL)
local uNameAndPass=$(grep -oP "(?<=\/\/)(.+)(?=\@)" <<< $DATABASE_URL)
local parsedUNameAndPass=$(runPythonCode '
from urllib.parse import quote_plus
print(quote_plus("'$uNameAndPass'"))')
DATABASE_URL=$(sed 's/$uNameAndPass/$parsedUNameAndPass/' <<< $DATABASE_URL)
}

_checkDatabase() {
editLastMessage "Checking DATABASE_URL ..."
local err=$(runPythonCode '
local mongoErr=$(runPythonCode '
import pymongo
try:
pymongo.MongoClient("'$DATABASE_URL'").list_database_names()
except Exception as e:
print(e)')
[[ $err ]] && quit "pymongo response > $err" || log "\tpymongo response > {status : 200}"
[[ $mongoErr ]] && quit "pymongo response > $mongoErr" || log "\tpymongo response > {status : 200}"
}

_checkTriggers() {
Expand Down Expand Up @@ -122,6 +134,8 @@ _checkGit() {
gitClone $HEROKU_GIT_URL tmp_git || quit "Invalid HEROKU_API_KEY or HEROKU_APP_NAME var !"
mv tmp_git/.git .
rm -rf tmp_git
editLastMessage "\tChecking Heroku Remote ..."
remoteIsExist heroku || addHeroku
else
replyLastMessage "\tInitializing Empty Git ..."
gitInit
Expand All @@ -132,16 +146,17 @@ _checkGit() {

_checkUpstreamRepo() {
editLastMessage "Checking UPSTREAM_REPO ..."
grep -q $UPSTREAM_REMOTE < <(git remote) || addUpstream
remoteIsExist $UPSTREAM_REMOTE || addUpstream
replyLastMessage "\tFetching Data From UPSTREAM_REPO ..."
fetchUpstream || updateUpstream && fetchUpstream || quit "Invalid UPSTREAM_REPO var !"
fetchBranches
deleteLastMessage
}

_checkUnoffPlugins() {
editLastMessage "Checking Userge-X [Extra] Plugins ..."
editLastMessage "Checking USERGE-X [Extra] Plugins ..."
if test $LOAD_UNOFFICIAL_PLUGINS = true; then
editLastMessage "\tLoading Userge-X [Extra] Plugins ..."
editLastMessage "\tLoading USERGE-X [Extra] Plugins ..."
replyLastMessage "\t\tClonning ..."
gitClone --depth=1 https://github.com/code-rgb/Userge-Plugins.git
editLastMessage "\t\tUpgrading PIP ..."
Expand All @@ -154,11 +169,10 @@ _checkUnoffPlugins() {
cp -r Userge-Plugins/resources/* resources/
rm -rf Userge-Plugins/
deleteLastMessage
editLastMessage "\tUserge-X [Extra] Plugins Loaded Successfully !"
editLastMessage "\tUSERGE-X [Extra] Plugins Loaded Successfully !"
else
editLastMessage "\tUserge-X [Extra] Plugins Disabled !"
editLastMessage "\tUSERGE-X [Extra] Plugins Disabled !"
fi
sleep 1
deleteLastMessage
}

Expand All @@ -178,4 +192,4 @@ assertEnvironment() {
_checkGit
_checkUpstreamRepo
_checkUnoffPlugins
}
}
2 changes: 1 addition & 1 deletion init/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ runUserge() {
startLogBotPolling
startUserge "$@"
stopUserge
}
}
61 changes: 1 addition & 60 deletions init/logbot/core/api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ declare -a _allMessages=()

_getResponse() {
if [[ -n $BOT_TOKEN && -n $LOG_CHANNEL_ID ]]; then
local reqType=$1 parse=false; shift
local reqType=${1#api.} parse=false; shift
test ${reqType::4} = "send" && parse=true
local params=$(sed 's/ /\&/g' <<< $*)
test -n $params && params="?$params"
Expand All @@ -39,62 +39,3 @@ _getResponse() {
sleep 0.6
fi
}

_urlEncode() {
echo "<code>$(echo "${1#\~}" | sed -E 's/(\\t)|(\\n)/ /g' |
curl -Gso /dev/null -w %{url_effective} --data-urlencode @- "" | cut -c 3-)</code>"
}

getMessageCount() {
return ${#_allMessages[@]}
}

getAllMessages() {
echo ${_allMessages[@]}
}

getLastMessage() {
if test ${#_allMessages[@]} -gt 0; then
${_allMessages[-1]}.$1 "$2"
elif [[ -n $BOT_TOKEN && -n $LOG_CHANNEL_ID ]]; then
log "first sendMessage ! (caused by \"core.api.$FUNCNAME\")\n"$2""
else
log "$2"
fi
}

getUpdates() {
local params=($*)
_getResponse $FUNCNAME ${params[*]}
}

rawsendMessage() {
local params=(
chat_id=$1
text=$(_urlEncode "$2")
parse_mode=HTML
)
test -n $3 && params+=(reply_to_message_id=$3)
log "$2"
_getResponse ${FUNCNAME#raw} ${params[*]}
}

raweditMessageText() {
local params=(
chat_id=$1
message_id=$2
text=$(_urlEncode "$3")
parse_mode=HTML
)
log "$3"
_getResponse ${FUNCNAME#raw} ${params[*]}
}

rawdeleteMessage() {
local params=(
chat_id=$1
message_id=$2
)
unset _allMessages[$3]
_getResponse ${FUNCNAME#raw} ${params[*]}
}
3 changes: 2 additions & 1 deletion init/logbot/core/init.sh → init/logbot/core/core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
# All rights reserved.

. init/logbot/core/api.sh
. init/logbot/core/message.sh
. init/logbot/core/types/types.sh
. init/logbot/core/methods/methods.sh
47 changes: 47 additions & 0 deletions init/logbot/core/methods/apiMethods.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
#
# Copyright (C) 2020 by UsergeTeam@Github, < https://github.com/UsergeTeam >.
#
# This file is part of < https://github.com/UsergeTeam/Userge > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/uaudith/Userge/blob/master/LICENSE >
#
# All rights reserved.

. init/logbot/core/utils.sh

api.getUpdates() {
local params=($*)
_getResponse $FUNCNAME ${params[*]}
}

api.sendMessage() {
local params=(
chat_id=$1
text=$(urlEncode "$2")
parse_mode=HTML
)
test -n $3 && params+=(reply_to_message_id=$3)
log "$2"
_getResponse $FUNCNAME ${params[*]}
}

api.editMessageText() {
local params=(
chat_id=$1
message_id=$2
text=$(urlEncode "$3")
parse_mode=HTML
)
log "$3"
_getResponse $FUNCNAME ${params[*]}
}

api.deleteMessage() {
local params=(
chat_id=$1
message_id=$2
)
unset _allMessages[$3]
_getResponse $FUNCNAME ${params[*]}
}
12 changes: 12 additions & 0 deletions init/logbot/core/methods/methods.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
#
# Copyright (C) 2020 by UsergeTeam@Github, < https://github.com/UsergeTeam >.
#
# This file is part of < https://github.com/UsergeTeam/Userge > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/uaudith/Userge/blob/master/LICENSE >
#
# All rights reserved.

. init/logbot/core/methods/apiMethods.sh
. init/logbot/core/methods/rawMethods.sh
27 changes: 27 additions & 0 deletions init/logbot/core/methods/rawMethods.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
#
# Copyright (C) 2020 by UsergeTeam@Github, < https://github.com/UsergeTeam >.
#
# This file is part of < https://github.com/UsergeTeam/Userge > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/uaudith/Userge/blob/master/LICENSE >
#
# All rights reserved.

raw.getMessageCount() {
return ${#_allMessages[@]}
}

raw.getAllMessages() {
echo ${_allMessages[@]}
}

raw.getLastMessage() {
if test ${#_allMessages[@]} -gt 0; then
${_allMessages[-1]}.$1 "$2"
elif [[ -n $BOT_TOKEN && -n $LOG_CHANNEL_ID ]]; then
log "first sendMessage ! (caused by \"core.methods.$FUNCNAME\")\n"$2""
else
log "$2"
fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
# All rights reserved.

Message() {
. <(sed "s/_Message/$1/g" init/logbot/core/messageClass.sh)
. <(sed "s/_Message/$1/g" init/logbot/core/types/messageClass.sh)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ _Message.id() {
}

_Message.chat_id() {
test "$1" == "=" && _Message.property chat_id = $2 || _Message.property chat_id
test "$1" = "=" && _Message.property chat_id = $2 || _Message.property chat_id
}

_Message.message_id() {
Expand All @@ -48,14 +48,14 @@ $(_Message.id) $(_Message.chat_id) $(_Message.message_id) "$(_Message.text)"
}

_Message.edit() {
raweditMessageText $(_Message.chat_id) $(_Message.message_id) "$1"
api.editMessageText $(_Message.chat_id) $(_Message.message_id) "$1"
_Message.text = "$1"
}

_Message.reply() {
rawsendMessage $(_Message.chat_id) "$1" $(_Message.message_id)
api.sendMessage $(_Message.chat_id) "$1" $(_Message.message_id)
}

_Message.delete() {
rawdeleteMessage $(_Message.chat_id) $(_Message.message_id) $(_Message.id)
api.deleteMessage $(_Message.chat_id) $(_Message.message_id) $(_Message.id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
#
# All rights reserved.

. init/logbot/methods/methods.sh
. init/logbot/core/types/message.sh
14 changes: 14 additions & 0 deletions init/logbot/core/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
#
# Copyright (C) 2020 by UsergeTeam@Github, < https://github.com/UsergeTeam >.
#
# This file is part of < https://github.com/UsergeTeam/Userge > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/uaudith/Userge/blob/master/LICENSE >
#
# All rights reserved.

urlEncode() {
echo "<code>$(echo "${1#\~}" | sed -E 's/(\\t)|(\\n)/ /g' |
curl -Gso /dev/null -w %{url_effective} --data-urlencode @- "" | cut -c 3-)</code>"
}
4 changes: 2 additions & 2 deletions init/logbot/logbot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
#
# All rights reserved.

. init/logbot/core/init.sh
. init/logbot/methods/init.sh
. init/logbot/core/core.sh
. init/logbot/methods/methods.sh
2 changes: 1 addition & 1 deletion init/logbot/methods/basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ printMessages() {
for msg in $(raw.getAllMessages); do
printf "{%s: %s}\n" $msg "$($msg.print)"
done
}
}
Loading

0 comments on commit 45995bd

Please sign in to comment.