add EVENT_TIMER

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-05-29 13:49:05 +02:00
parent 6651cf4f0a
commit 74cb204d1a
2 changed files with 42 additions and 5 deletions

View File

@ -27,6 +27,9 @@
# BASHBOT_EVENT_LOCATION location or venue received
# BASHBOT_EVENT_FILE file received
#
# BAHSBOT_EVENT_TIMER this event is a bit special as it fires every Minute
# and has 3 meanings: oneshot, everytime, every X minutes.
#
# all global variables and functions can be used in registered functions.
#
# parameters when loaded
@ -39,7 +42,7 @@
#
# export used events
export BASHBOT_EVENT_INLINE BASHBOT_EVENT_CMD BASHBOT_EVENT_REPLY
export BASHBOT_EVENT_INLINE BASHBOT_EVENT_CMD BASHBOT_EVENT_REPLY BASHBOT_EVENT_TIMER
# any global variable defined by addons MUST be prefixed by addon name
EXAMPLE_ME="example"
@ -59,7 +62,7 @@ if [[ "$1" = "start"* ]]; then
# function local variables can have any name, but must be LOCAL
example_reply(){
local msg="message"
send_markdown_message "${CHAT[ID]}" "User *${USER[USERNAME]}* replied to ${msg} from *${REPLYTO[USERNAME]}*"
send_markdown_message "${CHAT[ID]}" "User *${USER[USERNAME]}* replied to ${msg} from *${REPLYTO[USERNAME]}*" &
}
# register to inline and command
@ -73,6 +76,22 @@ if [[ "$1" = "start"* ]]; then
local msg="${MESSAGE[0]}"
# shellcheck disable=SC2154
[ "${type}" = "inline" ] && msg="${iQUERY[0]}"
send_normal_message "${CHAT[ID]}" "${type} received: ${msg}"
send_normal_message "${CHAT[ID]}" "${type} received: ${msg}" &
}
BASHBOT_EVENT_TIMER["${EXAMPLE_ME}after5min","-5"]="${EXAMPLE_ME}_after5min"
# any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL
example_after5min(){
send_markdown_message "$(< "${BOTADMIN}")" "This is a one time event after 5 Minutes!" &
}
BASHBOT_EVENT_TIMER["${EXAMPLE_ME}every2min","2"]="${EXAMPLE_ME}_every2min"
# any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL
example_every2min(){
send_markdown_message "$(< "${BOTADMIN}")" "This a a every 2 minute event ..." &
}
fi

View File

@ -11,7 +11,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.90-dev2-14-gafc669c
#### $$VERSION$$ v0.90-dev2-15-g6651cf4
#
# Exit Codes:
# - 0 sucess (hopefully)
@ -370,7 +370,25 @@ process_client() {
}
declare -Ax BASBOT_EVENT_INLINE BASBOT_EVENT_MESSAGE BASHBOT_EVENT_CMD BASBOT_EVENT_REPLY BASBOT_EVENT_FORWARD
declare -Ax BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE BASHBOT_EVENT_TEXT
declare -Ax BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE BASHBOT_EVENT_TEXT BASHBOT_EVENT_TIMER
EVENT_TIMER="0"
event_timer() {
local event timer debug="$1"
(( EVENT_TIMER += 1 ))
# shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_TIMER[@]}"
do
timer="${BASHBOT_EVENT_TIMER[${event}]##*,}"
[[ ! "$timer" =~ ^-*[0-9]+$ ]] && continue
[[ "$timer" =~ ^-*0+$ ]] && continue
if [ "$(( EVENT_TIMER % timer ))" = "0" ]; then
_exec_if_function "${BASHBOT_EVENT_TIMER[${event}]}" "timer" "${debug}"
[ "$(( EVENT_TIMER % timer ))" -lt "0" ] && \
unset BASHBOT_EVENT_TIMER["${event}"]
fi
done
}
event_inline() {
local event debug="$1"