aboutsummaryrefslogtreecommitdiff
path: root/bot.sh
blob: f135bd497491bb715db65d1c08c74b31a578b69a (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
#!/usr/bin/env bash



source env.sh
is_prime() {
    if (( $1 < 3 )); then
        return 0
    fi
    s=$(seq 2 $(($1-1)))
    while IFS= read -r i; do
        if (( $1 % $i == 0 )); then
            return 1
        fi
    done<<<"$s"
    return 0
}

rm -fr wsr wsw o w seq g tty
mkfifo wsr
trap 'rm -fr wsr wsw w o g seq tty' EXIT
mkfifo wsw
mkdir o
mkdir g
mkdir w
websocat wss://gateway.discord.gg <wsw >wsr &
ln -s $(tty) tty
heartbeat() {
    sendmsg '{"op":1,"d":'$(cat seq)'}'
}
dbg() {
    echo "$@" >tty
}
sendmsg() {
    fn=$(mktemp -p w)
    echo "$@" > $fn
    ln -s "$(readlink -f $fn)" o
}

(
dbg subshell started
while [[ -d o ]]; do
    for m in $(find $(readlink -f o) -type l); do
        dbg Sending message $m
        cat $m
        dbg "$(cat $m)"
        rm $m
    done
done > wsw)&

(
while [[ -d o ]]; do
    dbg handshaking
    heartbeat 
    sleep 10
done
)&


test_guild() {
    dbg Testing guild $1
    nonce=$(mktemp -p g -d)
    sendmsg '{"op":8,"d":{"guild_id":"'$1'", "query":"", "limit":0,"nonce":"'$nonce'"}}'
}


dispatch() {
    event=$(echo "$1" | jq -r .t)
    dbg Dispatching event $event
    if [[ $event = GUILD_MEMBER_ADD ]] || [[ $event = GUILD_MEMBER_REMOVE ]] ; then
        guildid=$(echo "$1" | jq -r .d.guild_id)
        dbg Someone joined $guildid
        test_guild $guildid
    elif [[ $event = MESSAGE_CREATE ]]; then
        content="$(echo "$1"|jq -r .d.content)"
        dbg Received message
        case "$content" in 
            gtest*)
                guildid=$(echo $content | tr -d '\n gtes')
                test_guild $guildid
                ;;
        esac
    elif [[ $event = GUILD_MEMBERS_CHUNK ]]; then
        chunk_size=$(echo "$1" | jq -r '[.d.members[]]|length')
        nonce=$(echo "$1" | jq -r '.d.nonce')
        chunk_index=$(echo "$1"| jq -r '.d.chunk_index')
        chunk_count=$(echo "$1"| jq -r '.d.chunk_count')
        echo $chunk_size > $nonce/$chunk_index
        total_received=$(find $nonce -type f| wc -l)
        dbg "Received chunk $chunk_index/$chunk_count (total $total_received) of size $chunk_size for $nonce"
        if [[ $total_received -eq $chunk_count ]]; then
            total_member_count=$(cat $nonce/* | paste -sd+ - | bc)
            dbg "we are done for now. total members: $total_member_count"
            if is_prime $total_member_count||true; then
                dbg "prime number woohoooo sending message to $ANNOUNCEMENT"
                curl -X POST -H "Authorization: Bot $TOKEN" https://discord.com/api/v10/channels/$ANNOUNCEMENT/messages -F payload_json='{"content": "WOOOO BABY A PRIME NUMBER!! WE ARE NOW '$total_member_count' MEMBERS!!!! WOOO THATS WHAT IVE BEEN WAITING FOR"}' >/dev/null 2>/dev/null
            fi
        fi

    fi
}

echo null > seq
while true; do
    dbg Trying to read line
    if read -r line ; then 
        echo $line | jq 'if .s then .s else '"$(cat seq)"' end'>seq
        op=$(echo $line | jq .op)
        echo $line >> rawlog
        if [[ $op -eq 10 ]]; then
            sendmsg '{"op":2,"d":{"token":"'$TOKEN'", "properties":{"os":"linux","browser":"bash","device":"bash"},"presence":{"status":"online","afk":false, "activities":[{"type":0,"name":"Being coded in Bash"}]}, "intents":33287}}' # change intents back to 7
        elif [[ $op -eq 0 ]]; then
            dispatch "$line"
        elif [[ $op -eq 1 ]] || [[ $op -eq 10 ]]; then
            dbg Received heartbeat
            dbg New sequence number = $(cat seq)
        else 
            dbg Received message of type $op
        fi
    fi
done <wsr