provide event key to function

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-06-03 20:34:43 +02:00
parent db03e23be8
commit 1e851bd296
4 changed files with 35 additions and 34 deletions

View File

@ -4,7 +4,7 @@
# this addon counts how many files, e.g. stickers, are sent to # this addon counts how many files, e.g. stickers, are sent to
# a chat and takes actions if threshold is reached # a chat and takes actions if threshold is reached
# #
#### $$VERSION$$ v0.91-0-g31808a9 #### $$VERSION$$ v0.91-1-gdb03e23
# used events: # used events:
# #

View File

@ -4,7 +4,7 @@
# Addons can register to bashbot events at statup # Addons can register to bashbot events at statup
# by providing their name and a callback per event # by providing their name and a callback per event
# #
#### $$VERSION$$ v0.91-0-g31808a9 #### $$VERSION$$ v0.91-1-gdb03e23
# #
# If an event occours each registered event function is called. # If an event occours each registered event function is called.
# #
@ -40,7 +40,8 @@
# #
# prameters on events # prameters on events
# $1 event: inline, message, ..., file # $1 event: inline, message, ..., file
# $2 debug: use "[[ "$2" = *"debug"* ]]" if you want to output extra diagnostic # $2 key: key of array BASHBOT_EVENT_xxx
# $3 debug: use "[[ "$2" = *"debug"* ]]" if you want to output extra diagnostic
# #
# export used events # export used events
@ -63,8 +64,8 @@ if [[ "$1" = "start"* ]]; then
# any function defined by addons MUST be prefixed by addon name # any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL # function local variables can have any name, but must be LOCAL
example_reply(){ example_reply(){
local msg="message" local msg="message" event="$1" key="$2"
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]}* (Event: ${event} Key:{${key})" &
} }
# register to inline and command # register to inline and command
@ -74,11 +75,11 @@ if [[ "$1" = "start"* ]]; then
# any function defined by addons MUST be prefixed by addon name # any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL # function local variables can have any name, but must be LOCAL
example_multievent(){ example_multievent(){
local type="$1" local event="$1" key="$2"
local msg="${MESSAGE[0]}" local msg="${MESSAGE[0]}"
# shellcheck disable=SC2154 # shellcheck disable=SC2154
[ "${type}" = "inline" ] && msg="${iQUERY[0]}" [ "${type}" = "inline" ] && msg="${iQUERY[0]}"
send_normal_message "${CHAT[ID]}" "${type} received: ${msg}" & send_normal_message "${CHAT[ID]}" "${event} from ${key} received: ${msg}" &
} }
BASHBOT_EVENT_TIMER["${EXAMPLE_ME}after5min","-5"]="${EXAMPLE_ME}_after5min" BASHBOT_EVENT_TIMER["${EXAMPLE_ME}after5min","-5"]="${EXAMPLE_ME}_after5min"

View File

@ -11,7 +11,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.91-0-g31808a9 #### $$VERSION$$ v0.91-1-gdb03e23
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -385,80 +385,80 @@ start_timer(){
EVENT_TIMER="0" EVENT_TIMER="0"
event_timer() { event_timer() {
local event timer debug="$1" local key timer debug="$1"
(( EVENT_TIMER++ )) (( EVENT_TIMER++ ))
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_TIMER[@]}" for key in "${!BASHBOT_EVENT_TIMER[@]}"
do do
timer="${event##*,}" timer="${key##*,}"
[[ ! "$timer" =~ ^-*[1-9][0-9]*$ ]] && continue [[ ! "$timer" =~ ^-*[1-9][0-9]*$ ]] && continue
if [ "$(( EVENT_TIMER % timer ))" = "0" ]; then if [ "$(( EVENT_TIMER % timer ))" = "0" ]; then
_exec_if_function "${BASHBOT_EVENT_TIMER[${event}]}" "timer" "${debug}" _exec_if_function "${BASHBOT_EVENT_TIMER[${key}]}" "timer" "${key}" "${debug}"
[ "$(( EVENT_TIMER % timer ))" -lt "0" ] && \ [ "$(( EVENT_TIMER % timer ))" -lt "0" ] && \
unset BASHBOT_EVENT_TIMER["${event}"] unset BASHBOT_EVENT_TIMER["${key}"]
fi fi
done done
} }
event_inline() { event_inline() {
local event debug="$1" local key debug="$1"
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_INLINE[@]}" for key in "${!BASHBOT_EVENT_INLINE[@]}"
do do
_exec_if_function "${BASHBOT_EVENT_INLINE[${event}]}" "inline" "${debug}" _exec_if_function "${BASHBOT_EVENT_INLINE[${key}]}" "inline" "${key}" "${debug}"
done done
} }
event_message() { event_message() {
echo "${MESSAGE[0]}" echo "${MESSAGE[0]}"
local event debug="$1" local key debug="$1"
# ${MESSAEG[*]} event_message # ${MESSAEG[*]} event_message
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_MESSAGE[@]}" for key in "${!BASHBOT_EVENT_MESSAGE[@]}"
do do
_exec_if_function "${BASHBOT_EVENT_MESSAGE[${event}]}" "messsage" "${debug}" _exec_if_function "${BASHBOT_EVENT_MESSAGE[${key}]}" "messsage" "${key}" "${debug}"
done done
# ${TEXT[*]} event_text # ${TEXT[*]} event_text
if [ "${MESSAGE[0]}" != "" ]; then if [ "${MESSAGE[0]}" != "" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_TEXT[@]}" for key in "${!BASHBOT_EVENT_TEXT[@]}"
do do
_exec_if_function "${BASHBOT_EVENT_TEXT[${event}]}" "text" "${debug}" _exec_if_function "${BASHBOT_EVENT_TEXT[${key}]}" "text" "${key}" "${debug}"
done done
# ${CMD[*]} event_cmd # ${CMD[*]} event_cmd
if [ "${CMD[0]}" != "" ]; then if [ "${CMD[0]}" != "" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_CMD[@]}" for key in "${!BASHBOT_EVENT_CMD[@]}"
do do
_exec_if_function "${BASHBOT_EVENT_CMD[${event}]}" "command" "${debug}" _exec_if_function "${BASHBOT_EVENT_CMD[${key}]}" "command" "${key}" "${debug}"
done done
fi fi
fi fi
# ${REPLYTO[*]} event_replyto # ${REPLYTO[*]} event_replyto
if [ "${REPLYTO[UID]}" != "" ]; then if [ "${REPLYTO[UID]}" != "" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_REPLYTO[@]}" for key in "${!BASHBOT_EVENT_REPLYTO[@]}"
do do
_exec_if_function "${BASHBOT_EVENT_REPLYTO[${event}]}" "replyto" "${debug}" _exec_if_function "${BASHBOT_EVENT_REPLYTO[${key}]}" "replyto" "${key}" "${debug}"
done done
fi fi
# ${FORWARD[*]} event_forward # ${FORWARD[*]} event_forward
if [ "${FORWARD[UID]}" != "" ]; then if [ "${FORWARD[UID]}" != "" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_FORWARD[@]}" for key in "${!BASHBOT_EVENT_FORWARD[@]}"
do do
_exec_if_function && "${BASHBOT_EVENT_FORWARD[${event}]}" "forward" "${debug}" _exec_if_function && "${BASHBOT_EVENT_FORWARD[${key}]}" "forward" "${key}" "${debug}"
done done
fi fi
# ${CONTACT[*]} event_contact # ${CONTACT[*]} event_contact
if [ "${CONTACT[FIRST_NAME]}" != "" ]; then if [ "${CONTACT[FIRST_NAME]}" != "" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_CONTACT[@]}" for key in "${!BASHBOT_EVENT_CONTACT[@]}"
do do
_exec_if_function "${BASHBOT_EVENT_CONTACT[${event}]}" "contact" "${debug}" _exec_if_function "${BASHBOT_EVENT_CONTACT[${key}]}" "contact" "${key}" "${debug}"
done done
fi fi
@ -466,9 +466,9 @@ echo "${MESSAGE[0]}"
# ${LOCALTION[*]} event_location # ${LOCALTION[*]} event_location
if [ "${LOCATION[LONGITUDE]}" != "" ] || [ "${VENUE[TITLE]}" != "" ]; then if [ "${LOCATION[LONGITUDE]}" != "" ] || [ "${VENUE[TITLE]}" != "" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_LOCATION[@]}" for key in "${!BASHBOT_EVENT_LOCATION[@]}"
do do
_exec_if_function "${BASHBOT_EVENT_LOCATION[${event}]}" "location" "${debug}" _exec_if_function "${BASHBOT_EVENT_LOCATION[${key}]}" "location" "${key}" "${debug}"
done done
fi fi
@ -476,9 +476,9 @@ echo "${MESSAGE[0]}"
# NOTE: compare again #URLS -1 blanks! # NOTE: compare again #URLS -1 blanks!
if [[ "${URLS[*]}" != " " ]]; then if [[ "${URLS[*]}" != " " ]]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_FILE[@]}" for key in "${!BASHBOT_EVENT_FILE[@]}"
do do
_exec_if_function "${BASHBOT_EVENT_FILE[${event}]}" "file" "${debug}" _exec_if_function "${BASHBOT_EVENT_FILE[${key}]}" "file" "${key}" "${debug}"
done done
fi fi

View File

@ -275,5 +275,5 @@ fi
#### [Prev Function Reference](6_reference.md) #### [Prev Function Reference](6_reference.md)
#### $$VERSION$$ v0.91-0-g31808a9 #### $$VERSION$$ v0.91-1-gdb03e23