Files
mc-connect/mc
2025-07-02 19:20:53 -07:00

75 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
readonly base_url="https://api.my-mc.link/"
readonly headers=( -H "Accept: application/json" -H "Content-Type: application/json" -H "x-my-mc-auth: ${MY_MC_API_KEY}" )
declare -rA commands=(
[hello]='GET'
[time]='GET'
[stats]='GET'
[log]='GET'
[start]='GET'
[stop]='GET'
[restart]='GET'
[my-link]='GET'
[my-geyser-link]='GET'
[my-sftp]='GET'
[my-hash]='GET'
[my-geyser-hash]='GET'
[my-hash-sftp]='GET'
[list-players]='GET'
[website]='GET'
[map]='GET'
[ban]='POST {"username": "%s"}'
[unban]='POST {"username": "%s"}'
[say]='POST {"message": "%s"}'
[tell]='POST {"username": "%s", "message": "%s"}'
[console]='POST {"command": "%s"}'
[give]='POST {"username": "%s", "item": "%s", "amount": "%s"}'
[install]='POST {"mod": "%s"}'
[uninstall]='POST {"mod": "%s"}'
[search]='POST {"mod": "%s"}'
[mod-list]='GET'
)
function first() {
echo "$@" | cut --delimiter=" " --fields="-1"
}
function rest() {
if (( "${#@}" > 1 )); then
echo "$@" | cut --delimiter=" " --fields="2-"
fi
}
function usage() {
printf "API key for my-mc must be exported using 'export MY_MC_API_KEY=<my-mc api key>'\n\n"
printf "Positional arguments will fill the JSON objects with values from left to right.\n\n"
printf "COMMANDS:\n"
for command in "${!commands[@]}"; do
printf "\t ${command} $(rest ${commands[${command}]})\n"
done
printf "\n"
}
# remove command from arg list while preserving quoted strings.
args=("$@")
args=("${args[@]:1}")
for command in "${!commands[@]}"; do
if [[ "$1" == "${command}" ]]; then
if [[ "$(first "${commands[$1]}")" == "POST" ]]; then
curl -sS "${headers[@]}" -X "$(first "${commands[$1]}")" "${base_url}${command}" -d "$(printf "$(rest ${commands[$1]})\n" "${args[@]}")"
echo ""
else
curl -sS "${headers[@]}" -X "$(first "${commands[$1]}")" "${base_url}${command}"
echo ""
fi
exit 0
fi
done
printf "${0}: invalid option -- $1\n"
usage
exit 1