summaryrefslogtreecommitdiff
path: root/forgebot_proper.sh
blob: 947be04755f94f635936866fd1283421033613bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/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"
  exit 1
fi
if [[ x"$HYPIXEL_KEY" == x ]]; then
  echo "Please set HYPIXEL_KEY to your hypixel api key"
  exit 1
fi
source "$(dirname -- "$0")"/atrocity/load.sh
source "$(dirname -- "$0")"/hypixel_api.sh
source "$(dirname -- "$0")"/data.sh
source "$(dirname -- "$0")"/forgebot.sh

find_option() {
  # Usage: find_option <data> <name>
  printf '%s' "$1" | jq -r '.data.options[] | select(.name == "'"$2"'")| .value'
}

disallow_mentions='"allowed_mentions": {"parse":[]}'
lookup_watched_account() {
  while read username ;do
    curl "https://mowojang.matdoes.dev/$username" 2>/dev/null
  done
}

atrocity_on_INTERACTION_CREATE() {
  local type
  type="$(printf '%s' "$1" | jq -r .type)"
  #PING	1
  #APPLICATION_COMMAND	2
  #MESSAGE_COMPONENT	3
  #APPLICATION_COMMAND_AUTOCOMPLETE	4
  #MODAL_SUBMIT	5
  id=$(printf '%s' "$1" | jq -r .id)
  token=$(printf '%s' "$1" | jq -r .token)
  user=$(printf '%s' "$1" | jq -r .member.user.id)
  atrocity_debug Processing interaction with id $id and token $token of type $type from $user
  if [[ $type == 2 ]]; then
    command_name=$(printf '%s' "$1" | jq -r .data.name)
    atrocity_debug Executing command $command_name
    if [[ $command_name == register ]]; then
      username="$(find_option "$1" username)"
      atrocity_debug "Registering user with name $username"
      # TODO first ack then edit
      minecraft_uuid="$(curl "https://mowojang.matdoes.dev/$username" 2>/dev/null | jq -r .id)"
      atrocity_debug "Got uuid: $minecraft_uuid"
      if has_watched_account $user $minecraft_uuid; then
        atrocity_rest POST "interactions/$id/$token/callback" "$(cat << EOF
{
  "type": 4,
  "data": {
    "content": "You have already registered the account with name \`$username\` and uuid \`$minecraft_uuid\`.",
    $disallow_mentions
  }
}
EOF
        )"
      else
        add_watched_account $user $minecraft_uuid
        atrocity_rest POST "interactions/$id/$token/callback" "$(cat << EOF
{
  "type": 4,
  "data": {
    "content": "Trying to register with name \`$username\` and uuid \`$minecraft_uuid\`.",
    $disallow_mentions
  }
}
EOF
        )"
      fi
    fi
    if [[ $command_name == list ]]; then
      atrocity_debug "listing"
      # TODO first ack then edit

      watched_accounts="$(find_watched_accounts "$user" | lookup_watched_account | jq -s '.' | jq '("You are watching these accounts:\n"+(.|map("- [`"+.name+"`](<http://namemc.com/profile/"+.id+">)")|join("\n")))')"
      atrocity_rest POST "interactions/$id/$token/callback" "$(cat << EOF
{
  "type": 4,
  "data": {
    "content": $watched_accounts,
    $disallow_mentions
  }
}
EOF
      )"
    fi
    if [[ $command_name == unregister ]]; then
      username="$(find_option "$1" username)"
      atrocity_debug "Unregistering user with name $username"
      # TODO first ack then edit
      minecraft_uuid="$(curl "https://mowojang.matdoes.dev/$username" 2>/dev/null | jq -r .id)"
      atrocity_debug "Got uuid: $minecraft_uuid"
      if ! has_watched_account $user $minecraft_uuid; then
        atrocity_rest POST "interactions/$id/$token/callback" "$(cat << EOF
{
  "type": 4,
  "data": {
    "content": "You are not watching the account with name \`$username\` and uuid \`$minecraft_uuid\`.",
    $disallow_mentions
  }
}
EOF
        )"
      else
        remove_watched_account $user $minecraft_uuid
        atrocity_rest POST "interactions/$id/$token/callback" "$(cat << EOF
{
  "type": 4,
  "data": {
    "content": "Unregister the name \`$username\` and uuid \`$minecraft_uuid\`.",
    $disallow_mentions
  }
}
EOF
        )"
      fi
    fi
  fi
}
atrocity_on_unknown() {
  atrocity_debug "Skipping event $1"
}
atrocity_on_READY() {
  atrocity_debug "Payload: $1"
  user_id="$(printf '%s' "$1" | jq -r .user.id)"
  atrocity_debug "user id: $user_id"

  atrocity_rest POST applications/"$user_id"/${GUILD_EXTRA}commands "$(cat << 'EOF'
{
  "name": "list",
  "type": 1,
  "description": "List all users you are watching",
  "options": [
  ]
}
EOF
  )"
  atrocity_rest POST applications/"$user_id"/${GUILD_EXTRA}commands "$(cat << 'EOF'
{
  "name": "unregister",
  "type": 1,
  "description": "Unregister a profile that was watched by the forge bot",
  "options": [
    {
      "name": "username",
      "description": "The Minecraft user name you don't want to watch anymore",
      "type": 3
    }
  ]
}
EOF
  )"
  atrocity_rest POST applications/"$user_id"/${GUILD_EXTRA}commands "$(cat << 'EOF'
{
  "name": "register",
  "type": 1,
  "description": "Register a profile to be watched by the forge bot",
  "options": [
    {
      "name": "username",
      "description": "The Minecraft user name you want to watch",
      "type": 3
    }
  ]
}
EOF
  )"
  poll_forge_events &
}


atrocity_connect
atrocity_loop