From b7bf0368732edbc9858035432ba86b6b9b23545c Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Thu, 17 Oct 2024 12:44:39 +0200 Subject: Add docker file --- .dockerignore | 1 + Dockerfile | 8 ++++++++ data.sh | 2 +- docker-compose.yml | 11 +++++++++++ forgebot.sh | 16 ++++++++-------- forgebot_proper.sh | 20 ++++++++++++++++++++ 6 files changed, 49 insertions(+), 9 deletions(-) create mode 120000 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 120000 index 0000000..3e4e48b --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.gitignore \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a8b68c8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM alpine +RUN apk add --no-cache websocat bash jq grep curl +COPY ./ /app/forgebot/ +WORKDIR "/app/forgebot" +ENTRYPOINT ["/app/forgebot/forgebot_proper.sh"] + + + diff --git a/data.sh b/data.sh index a8cd51b..adf034e 100644 --- a/data.sh +++ b/data.sh @@ -1,4 +1,4 @@ -export FORGEBOT_DATA_DIR="forgebotdata" +export FORGEBOT_DATA_DIR="${DATA_DIR:-.}/forgebotdata" mkdir -p "$FORGEBOT_DATA_DIR" list_all_watched_accounts() { diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..de6de98 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ + +services: + forgebot: + build: . + environment: + ATROCITY_TOKEN: 'DISCORD_TOKEN' + HYPIXEL_KEY: 'HYPIXEL_API_KEY' + CHANNEL_ID: 'NOTIFICATION_CHANNEL_ID' + DATA_DIR: '/app/data' + volumes: + - ./data:/app/data diff --git a/forgebot.sh b/forgebot.sh index bf07e1c..98a246a 100755 --- a/forgebot.sh +++ b/forgebot.sh @@ -24,23 +24,23 @@ while true; do processes="$(curl -H "Api-Key: $HYPIXEL_KEY" https://api.hypixel.net/v2/skyblock/profiles\?uuid=$uuid 2>/dev/null | tee api_response.json| jq '[.profiles[] | select(.members["'$uuid'"].forge.forge_processes.forge_1) | {"qf": .members["'$uuid'"].mining_core.nodes.forge_time,"fp": [.members["'$uuid'"].forge.forge_processes.forge_1[]], cutename: .cute_name}]')" atrocity_debug $processes - mkdir -p notificationdata - touch notificationdata/$uuid + mkdir -p "${DATA_DIR:-.}/"notificationdata + touch "${DATA_DIR:-.}/"notificationdata/$uuid now=$(date +%s000) - rm -f messagedata + rm -f "${DATA_DIR:-.}/"messagedata echo "$processes"| jq -r '.[] | {qf: (.qf // 0), fp: .fp[], cutename: .cutename} | (if (.qf == 20) then "0.7" elif (.qf == 0) then "1" else (1 - (.qf * .5 + 10) / 100|tostring) end) + " " + .fp.id + " " + (.fp.startTime|tostring) + " " + (.fp.slot|tostring) + " " + (.cutename)' | while read qf id starttime slot cutename; do atrocity_debug ID: $id STARTTIME: $starttime SLOT: $slot doneat=$(($starttime + $(get_duration $id))) atrocity_debug DONEAT WITHOUT QF: $doneat doneat=$(bc <<<"$starttime + $qf*$(get_duration $id)"|sed s'/\..*//') atrocity_debug DONE: $doneat - already_notified=$(grep $starttime notificationdata/$uuid >/dev/null; echo $?) + already_notified=$(grep $starttime "${DATA_DIR:-.}/"notificationdata/$uuid >/dev/null; echo $?) atrocity_debug NOTIFIED: $already_notified isready=$(($now > $doneat)) atrocity_debug "$now > $doneat: $isready" if [[ $isready -eq 1 ]]; then atrocity_debug READY - echo $starttime >> notificationdata/$uuid.new + echo $starttime >> "${DATA_DIR:-.}/"notificationdata/$uuid.new if [[ already_notified -eq 1 ]]; then echo Your $id in $slot on $cutename is ready since " (started )" >>messagedata atrocity_debug ADDED @@ -50,10 +50,10 @@ while true; do done if [[ -f messagedata ]]; then echo Sending messagedata: - atrocity_debug "$(cat messagedata)" - jsondata="$(jq -n --arg body "$(list_watchers "$uuid"|sed -E 's|(.*)|<@\1>|';echo;cat messagedata)" '{"content": $body}')" + atrocity_debug "$(cat "${DATA_DIR:-.}/"messagedata)" + jsondata="$(jq -n --arg body "$(list_watchers "$uuid"|sed -E 's|(.*)|<@\1>|';echo;cat "${DATA_DIR:-.}/"messagedata)" '{"content": $body}')" atrocity_rest POST "channels/$CHANNEL_ID/messages" "$jsondata" - mv notificationdata/$uuid.new notificationdata/$uuid + mv "${DATA_DIR:-.}/"notificationdata/$uuid.new "${DATA_DIR:-.}/"notificationdata/$uuid fi sleep 10 done diff --git a/forgebot_proper.sh b/forgebot_proper.sh index e1a8971..947be04 100755 --- a/forgebot_proper.sh +++ b/forgebot_proper.sh @@ -1,4 +1,24 @@ #!/bin/bash + +program_exists() { + which "$1" >/dev/null + return $? +} +require_program() { + for prog in "$@"; do + if ! program_exists "$prog"; then + echo "Missing program: $prog" + exit 1 + fi + done +} +require_program curl jq websocat printf grep find sed mkdir touch rm mv mkfifo basename tty +if ! [[ "$(grep --version)" = *GNU* ]]; then + echo Using non-GNU grep. This can cause issues since we use some non standard grep options. + exit 1 +fi + + [[ -f "$(dirname -- "$0")"/env.sh ]] && source "$(dirname -- "$0")"/env.sh if [[ x"$ATROCITY_TOKEN" == x ]]; then echo "Please set ATROCITY_TOKEN to your discord token" -- cgit