aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-05-12 16:07:15 +0200
committerLinnea Gräf <nea@nea.moe>2024-05-12 16:07:15 +0200
commit1083db296869fdf6cbb9b4ac1739b62f21428920 (patch)
treec65b2597734aa3cf963f546f0ec90c890c4203bb
parent65cbc9069e15f7f77a0497395a7796906b8ade9a (diff)
downloadatrocity-1083db296869fdf6cbb9b4ac1739b62f21428920.tar.gz
atrocity-1083db296869fdf6cbb9b4ac1739b62f21428920.tar.bz2
atrocity-1083db296869fdf6cbb9b4ac1739b62f21428920.zip
More WIP
-rw-r--r--.gitignore1
-rw-r--r--connect.sh10
-rw-r--r--events.sh22
-rw-r--r--example/.gitignore1
-rw-r--r--example/env-example.sh3
-rwxr-xr-xexample/simple.sh24
-rw-r--r--load.sh2
-rw-r--r--rest.sh3
8 files changed, 57 insertions, 9 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..397b4a7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.log
diff --git a/connect.sh b/connect.sh
index 73d45ba..40b66da 100644
--- a/connect.sh
+++ b/connect.sh
@@ -73,14 +73,12 @@ atrocity_loop() {
atrocity_debug "Atrocity loop started before connection has been set up"
exit 1
fi
- atrocity_debug "Loop started"
+ atrocity_debug "Read loop started"
while true; do
local line
local operator
- atrocity_debug "Trying to read line"
if read -r line; then
- atrocity_debug "Reading message from discord: $line"
-
+ echo "$line" >>raw_receive.log
printf %s "$line" | jq 'if .s then .s else '"$(atrocity_sequence_get)"' end' >"$ATROCITY_SESSION"/sequence
operator="$(printf %s "$line" | jq .op)"
case "$operator" in
@@ -91,10 +89,10 @@ atrocity_loop() {
10) # Hello
atrocity_debug "Sending hello"
echo "$(($(printf "%s" "$line" | jq -r .d.heartbeat_interval) / 1000))" >"$ATROCITY_SESSION"/heartbeat-interval
- atrocity_gateway_send_raw '{"op":2,"d":{"token":"'$ATROCITY_TOKEN'", "properties":{"os":"linux","browser":"bash","device":"bash"},"presence":{"status":"online","afk":false, "activities":[{"type":0,"name":"Being coded in Bash"}]}, "intents":33287}}'
+ atrocity_gateway_send_raw '{"op":2,"d":{"token":"'$ATROCITY_TOKEN'", "properties":{"os":"linux","browser":"bash","device":"bash"},"presence":{"status":"online","afk":false, "activities":[{"type":0,"name":"Being coded in Bash"}]}, "intents":4609}}'
;;
0) # Dispatch
- atrocity_dispatch "$line"
+ atrocity_on_dispatch "$line"
;;
1) # Heartbeat
atrocity_heartbeat
diff --git a/events.sh b/events.sh
new file mode 100644
index 0000000..6704def
--- /dev/null
+++ b/events.sh
@@ -0,0 +1,22 @@
+atrocity_on_dispatch() {
+ local event
+ event="$(printf '%s' "$1" | jq -r .t)"
+ atrocity_on_event "$event" "$(printf '%s' "$1" | jq .d)"
+}
+
+atrocity_on_event() {
+ atrocity_on_default_event "$1" "$2"
+}
+atrocity_on_default_event() {
+ local handler
+ handler="atrocity_on_$1"
+ if declare -F "$handler" >/dev/null; then
+ "$handler" "$2"
+ else
+ atrocity_on_unknown "$1" "$2"
+ fi
+}
+
+atrocity_on_unknown() {
+ noop
+}
diff --git a/example/.gitignore b/example/.gitignore
new file mode 100644
index 0000000..137e678
--- /dev/null
+++ b/example/.gitignore
@@ -0,0 +1 @@
+env.sh
diff --git a/example/env-example.sh b/example/env-example.sh
new file mode 100644
index 0000000..be6e59c
--- /dev/null
+++ b/example/env-example.sh
@@ -0,0 +1,3 @@
+# Copy this file to env.sh and place your configuration in here
+
+export ATROCITY_TOKEN="MzEy.............................................................."
diff --git a/example/simple.sh b/example/simple.sh
index c90c451..a756ee4 100755
--- a/example/simple.sh
+++ b/example/simple.sh
@@ -1,12 +1,30 @@
#!/usr/bin/env bash
+source "$(dirname -- "$0")"/env.sh
source "$(dirname -- "$0")"/../load.sh
atrocity_debug Simple example loaded
+atrocity_on_GUILD_CREATE() {
+ atrocity_debug "Guild: $(echo "$1" | jq -r .name)"
+}
+atrocity_on_unknown() {
+ atrocity_debug "Unknown event $1"
+}
+atrocity_on_MESSAGE_CREATE() {
+ local content
+ local channel
+ content="$(echo "$1" | jq -r .content)"
+ channel="$(echo "$1" | jq -r .channel_id)"
-export ATROCITY_TOKEN="MzEyMjU2ODcxMTc0MTExMjMz.GPe_v6.4Acwm7tje3sMEvUu05NPyQrZalh8knTIHPgLmk"
+ if [[ "$(echo "$1" | jq -r .author.bot)" = "true" ]]; then
+ return
+ fi
+ atrocity_debug "Found message with content $content"
-atrocity_dispatch() {
- echo "Dispatching $1"
+ case "$content" in
+ \!ping)
+ atrocity_rest POST /channels/"$channel"/messages '{"content": "Pong", "embeds": [{"title": "Pong", "description": "You have been ponginated"}]}'
+ ;;
+ esac
}
atrocity_connect
diff --git a/load.sh b/load.sh
index 7e7f0af..3deaab6 100644
--- a/load.sh
+++ b/load.sh
@@ -6,7 +6,9 @@ fi
_atrocity_base="$(dirname -- "$(readlink -f -- _atrocity_init_file)")"
source "$_atrocity_base"/logger.sh
+source "$_atrocity_base"/events.sh
source "$_atrocity_base"/connect.sh
+source "$_atrocity_base"/rest.sh
atrocity_debug "Loaded atrocity from $_atrocity_base"
_atrocity_prepare
diff --git a/rest.sh b/rest.sh
new file mode 100644
index 0000000..e4824d8
--- /dev/null
+++ b/rest.sh
@@ -0,0 +1,3 @@
+atrocity_rest() {
+ curl -X "$1" "https://discord.com/api/v10/$2" -H "authorization: Bot $ATROCITY_TOKEN" -H "content-type: application/json" --data "$3" 2>/dev/null
+}