#!/bin/bash -f
######################################################################
# Discord Puppy client in BASH, (C) 2015-2024 Nosey Nick Waterman,
#   <perl@noseynick.org>   https://noseynick.org/artemis/
# All wrong righted, all rights reserved. Licensed under the GNU
# Affero General Public License v3.0 https://www.gnu.org/licenses/agpl.txt
# with Commons Clause https://commonsclause.com/ v1.0
######################################################################

PROG="${0##*/}"
cd "${0%/*}" || exit # approx "dirname" - to find...
# shellcheck disable=SC1091 # don't need to parse when shellchecking
[[ -f ./creds.sh ]] && . ./creds.sh
# shellcheck disable=SC1091 # don't need to parse when shellchecking
[[ -f ./conf.sh  ]] && . ./conf.sh
# ... Which should be a short script that simply sets:
# TOKEN='94FVEoMPG0Rp2nNqqRqtpYcJ.746V/U.GWmFqAkozWxqGncXyDZMUPOptSi' or whatever
#   (See https://discord.com/developers/applications/me "App Bot User / Token")
#   (See also "Generate OAuth2 URL" for invite URLs whilst you're there)
# CHAN=295172685115110159 # probably the right-hand end of the URL when browsed to your channel
# For security reasons you should also protect this file: chmod 700 creds.sh
[[ -z "$TOKEN" ]] && echo "ERROR: $PWD/creds.sh was supposed to set \$TOKEN" >&2 && exit 9
[[ -z "$CHAN"  ]] && echo "ERROR: $PWD/creds.sh was supposed to set \$CHAN"  >&2 && exit 9

# Other vars probably don't need to be touched:
AUTH="Authorization: Bot $TOKEN"
API=https://discord.com/api/v6

ACT="$1"; shift
case "$ACT" in
--ws) #### SEE ALSO: http://www.scooterx3.net/2016-05-25/discord-bot.html
  echo '###   {"op":1,"d":null}' >&2
  {
    sleep 1
    # shellcheck disable=SC2016 # $os $browser $device are not to be expanded:
    echo '{"op":2,"d":{"token":"'"$TOKEN"'",' \
    '"properties":{"$os":"linux","$browser":"NoseyBot","$device":"NoseyBot"},' \
    '"compress":false,"large_threshold":250}}'
    cat
  } | wscat --connect wss://gateway.discord.gg/
  ;;
--fetch)
  URL=$API/channels/$CHAN/messages
  echo "# fetching $URL" >&2
  wget -t3 -T3 -qO- "$URL" --header "$AUTH" "$@" | { json_pp || cat; }
  ;;
--send)
  URL=$API/channels/$CHAN/messages
  echo "# sending to $URL - provide input and end with ctrl-D" >&2
  MSG="$(cat)"
  MID=$(wget -t3 -T3 -qO- "$URL" --header "$AUTH" --post-data "content=$MSG" "$@" | sed -E 's/.*"id": *"([0-9]*)".*/\1/')
  # AKA jq -r .id
  echo "$URL/$MID" | tee ~/.discord-msgid
  ;;
--delete)
  case "$1" in
    "")        URL=$(cat ~/.discord-msgid) ;;
    http://*)  URL="$1" ;;
    https://*) URL="$1" ;;
    [0-9]*)    URL="$API/channels/$CHAN/messages/$1" ;;
    *)         echo "USAGE: $0 --delete [url | MsgID]"; exit 9 ;;
  esac
  # NEEDS NEWER wget -t3 -T3 -qO- --method=DELETE "$URL" --header "$AUTH" "$@"
  curl -s -X DELETE "$URL" -H "$AUTH" "$@"
  echo
  ;;
--edit) # combines both of the above
  URL=$(cat ~/.discord-msgid)
  [[ "$URL" ]] || { echo "ERROR: no ~/.discord-msgid to edit!"; exit 9; }
  echo "# sending to $URL - provide input and end with ctrl-D" >&2
  MSG="$(sed -e 's|\"|\\\"|g; s|$|\\n|g;' | tr -d '\n')"
  # NEEDS NEWER wget -t3 -T3 -qO- --method=PATCH --header "$AUTH" \
  #  --header "Content-Type: application/json" \
  #  --body-data "{\"content\": \"$MSG\" }" "$URL" $@ >/dev/null
  curl -s --request PATCH -H "$AUTH" -H "Content-Type: application/json" \
    --data "{\"content\": \"$MSG\" }" "$URL" "$@" >/dev/null
  echo "Returned $?"
  ;;
--react)
  react_usage () { echo "USAGE: $0 --react [URL/msgid] REACTION EG '%F0%9F%87%A6'" >&2; exit 9; }
  case "$1" in
    "")       react_usage ;;
    http://*) URL="$1"; shift ;;
    [0-9]*)   URL="$API/channels/$CHAN/messages/$1"; shift ;;
    *)        URL=$(cat ~/.discord-msgid) ;; # NOT shift but use $1 for reaction:
  esac
  [[ "$1" ]] || react_usage
  # NEEDS NEWER wget -t3 -T3 -qO- --method=PUT "$URL/reactions/$1/@me" \
  #  --header "$AUTH" --header 'Content-Length: 0' "$@"
  curl -s -X PUT "$URL/reactions/$1/@me" -H "$AUTH" -H 'Content-Length: 0' "$@"
  ;;
*) grep ^-- "$0" | sed -e 's/).*/ | /' \
  | echo "USAGE: $PROG [ $(xargs) --help ]" >&2 ;;
esac