windows discord note when message is empty, linux ddns and discord new quiet options.

This commit is contained in:
2023-11-15 14:25:42 +00:00
parent 161ca4c691
commit 3a56b99e3d
4 changed files with 52 additions and 20 deletions
+2
View File
@@ -105,6 +105,8 @@ end {
if ($distinct) { if ($distinct) {
Out-File -FilePath $distinctFile -InputObject $message Out-File -FilePath $distinctFile -InputObject $message
} }
} elseif ($distinct) {
echo "duplicate message"
} }
} }
+18 -13
View File
@@ -16,7 +16,7 @@ function SendDiscordMessage
# convert string into json, escaping the given message # convert string into json, escaping the given message
$title = ConvertTo-Json -InputObject $title $title = ConvertTo-Json -InputObject $title
$message = ConvertTo-Json -InputObject $message $message = ConvertTo-Json -InputObject $message.trim()
$botName = ConvertTo-Json -InputObject $botName $botName = ConvertTo-Json -InputObject $botName
# rewrite json string, because "\u001b[m" needs to "\u001b[0m" instead # rewrite json string, because "\u001b[m" needs to "\u001b[0m" instead
@@ -24,29 +24,34 @@ function SendDiscordMessage
$message = $message -Replace "\\u001b\[m", "\u001b[0m" $message = $message -Replace "\\u001b\[m", "\u001b[0m"
# only enable colors if needed # only enable colors if needed
$prefix = ''; $prefix = ''
if ($message -contains "\u001b") { if ($message -contains "\u001b") {
$prefix = 'ansi\n'; $prefix = 'ansi\n'
} }
# remove wrapping quotes around json string, replace with color-enabled block quotes if ($message -eq '""') {
$message = '"```' + $prefix + $message.SubString(1, $message.length - 2) + '\n```"' # message empty, so denote as such
$message = '"\ud83d\udea9 MESSAGE EMPTY \ud83d\udea9"'
} else {
# remove wrapping quotes around json string, replace with color-enabled block quotes
$message = '"```' + $prefix + $message.SubString(1, $message.length - 2) + '\n```"'
}
# build post data: {"username": "$botName", "content": "$message"} # build post data: {"username": "$botName", "content": "$message"}
$content = "{"; $content = "{"
if ($botName) { if ($botName -ne '""') {
$content += '"username": ' + $botName + ', '; $content += '"username": ' + $botName + ', '
} }
$content += '"embeds": [{"description": ' + $message; $content += '"embeds": [{"description": ' + $message
if ($color -ge 0) { if ($color -ge 0) {
$content += ', "color": ' + $color; $content += ', "color": ' + $color
} }
if ($title) { if ($title -ne '""') {
$content += ', "title": ' + $title; $content += ', "title": ' + $title
} }
$content += '}]}'; $content += '}]}'
# post data is complex, so we have to pipe it into curl # post data is complex, so we have to pipe it into curl
# display the resulting id/message returned from Discord # display the resulting id/message returned from Discord
+14 -4
View File
@@ -9,6 +9,7 @@
# cyan(#00ffff), blue(#0343df), purple(#7e1e9c), magenta(#c20078), grey(#929591), # cyan(#00ffff), blue(#0343df), purple(#7e1e9c), magenta(#c20078), grey(#929591),
# LIGHT: pink(#ff81c0), apricot(#ffb16d), beige(#e6daa6), mint(#9ffeb0), lavender(#c79fef), white(#ffffff). # LIGHT: pink(#ff81c0), apricot(#ffb16d), beige(#e6daa6), mint(#9ffeb0), lavender(#c79fef), white(#ffffff).
quiet=false
skipMsg=false skipMsg=false
declare -A colors declare -A colors
colors=( colors=(
@@ -23,7 +24,7 @@ print_usage() {
} }
# handle all arguments provided # handle all arguments provided
while getopts 'h:c:d:t:z' flag; do while getopts 'h:c:d:t:zq' flag; do
case "$flag" in case "$flag" in
h) h)
if [[ "$OPTARG" == "http"*"?wait=true" ]]; then if [[ "$OPTARG" == "http"*"?wait=true" ]]; then
@@ -67,6 +68,9 @@ while getopts 'h:c:d:t:z' flag; do
z) z)
skipMsg=true skipMsg=true
;; ;;
q)
quiet=true
;;
*) *)
echo "unknown option provided '$flag'" echo "unknown option provided '$flag'"
print_usage print_usage
@@ -103,7 +107,8 @@ if [[ -z "$message" ]] && [[ "$skipMsg" = false ]]; then
fi fi
# sed removes blank lines from the end of the message # sed removes blank lines from the end of the message
fullMsg=$(echo "$message" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}') message=$(echo "$message" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}')
fullMsg="$message"
# if message is too long, split it, and after curl, recursively call with remainder # if message is too long, split it, and after curl, recursively call with remainder
# 4k max, but we must account for escaping text in json, so limit to 90% capacity # 4k max, but we must account for escaping text in json, so limit to 90% capacity
@@ -127,8 +132,13 @@ fi
message=$(echo "$message" | jq -aRs . | sed 's/\\u001b\[m/\\u001b[0m/g') message=$(echo "$message" | jq -aRs . | sed 's/\\u001b\[m/\\u001b[0m/g')
if [[ "$message" = '"\n"' ]]; then if [[ "$message" = '"\n"' ]]; then
# message empty, so denote as such if [[ "$quiet" = true ]]; then
message='"\ud83d\udea9 MESSAGE EMPTY \ud83d\udea9"' # quiet was specified, and there was no message, so stop now
exit 0
else
# message empty, so denote as such
message='"\ud83d\udea9 MESSAGE EMPTY \ud83d\udea9"'
fi
else else
if [[ "$message" = *"\\u001b"* ]]; then if [[ "$message" = *"\\u001b"* ]]; then
prefix="ansi\n" prefix="ansi\n"
+18 -3
View File
@@ -1,5 +1,15 @@
#!/bin/sh #!/bin/sh
quiet=false
while getopts 'q' flag; do
case "$flag" in
q)
quiet=true
;;
esac
done
shift "$((OPTIND-1))"
# we need zone and token, we also accept, netmask, and overrides # we need zone and token, we also accept, netmask, and overrides
zone=$1 zone=$1
device=$2 device=$2
@@ -7,7 +17,8 @@ scope=$3
# stop if we do not have the required information # stop if we do not have the required information
if [ -z "$zone" -o -z "$token" ]; then if [ -z "$zone" -o -z "$token" ]; then
echo "Usage: token=<your-authentication-token> [netmask=64] [ipv4=<override>] [ipv6=<override>] $0 your-name.dynv6.net [device] [scope]" echo '"-q": quiet flag, to suppress "address unchanged" messages'
echo "Usage: token=<your-authentication-token> [netmask=64] [ipv4=<override>] [ipv6=<override>] $0 your-name.dynv6.net [-q] [device] [scope]"
exit 1 exit 1
fi fi
@@ -73,7 +84,9 @@ ipv6="$ipv6/$netmask"
# update IPv4, if changed # update IPv4, if changed
if [ "$old4" = "$ipv4" ]; then if [ "$old4" = "$ipv4" ]; then
echo "IPv4 address unchanged" if [ "$quiet" = false ]; then
echo "IPv4 address unchanged"
fi
else else
# send IPv4 address to dynv6 # send IPv4 address to dynv6
echo -n "Updating IPv4: " echo -n "Updating IPv4: "
@@ -86,7 +99,9 @@ fi
# update IPv6, if changed # update IPv6, if changed
if [ "$old6" = "$ipv6" ]; then if [ "$old6" = "$ipv6" ]; then
echo "IPv6 address unchanged" if [ "$quiet" = false ]; then
echo "IPv6 address unchanged"
fi
else else
# send IPv6 address to dynv6 # send IPv6 address to dynv6
echo -n "Updating IPv6: " echo -n "Updating IPv6: "