aboutsummaryrefslogtreecommitdiff
path: root/setup_wizard.py
blob: 8575b27702b0833ddaa9231726d4198d44ebfdfb (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
#!/usr/bin/env python3
"""
setup wizard for faggot-chan. figures out which files need writing based on
how you're deploying, and writes them. run this before bot.py / main.py.
"""
import secrets
from pathlib import Path

SCRIPT_DIR = Path(__file__).resolve().parent


def ask(prompt: str, default: str = None) -> str:
    suffix = f" [{default}]" if default else ""
    val = input(f"{prompt}{suffix}: ").strip()
    return val or default or ""


def ask_yn(prompt: str) -> bool:
    while True:
        val = input(f"{prompt} (y/n): ").strip().lower()
        if val in ("y", "yes"):
            return True
        if val in ("n", "no"):
            return False
        print("just y or n please")


def write_tokens_file(config_dir: Path):
    config_dir.mkdir(parents=True, exist_ok=True)
    print("\n-- discord bot setup --")
    token = ask("discord bot token")
    server_id = ask("discord server (guild) id")
    while not server_id.isdigit():
        print("that's not a number")
        server_id = ask("discord server (guild) id")
    (config_dir / "tokens.txt").write_text(f"{token}\n{server_id}\n", encoding="utf-8")
    print(f"wrote {config_dir / 'tokens.txt'}")


def write_connection_file(config_dir: Path, default_uri: str = "ws://0.0.0.0:8765"):
    config_dir.mkdir(parents=True, exist_ok=True)
    print("\n-- connection to main.py --")
    uri = ask("main.py address (ws:// or wss://)", default_uri)
    print("paste the auth token main.py is using (from colab_setup.py output),")
    token = ask("or leave blank to generate a new one")
    if not token:
        token = secrets.token_urlsafe(32)
        print(f"generated token: {token}")
    (config_dir / "connection.txt").write_text(f"{uri}\n{token}\n", encoding="utf-8")
    print(f"wrote {config_dir / 'connection.txt'}")
    return token


def write_auth_token_file(config_dir: Path, token: str = None):
    config_dir.mkdir(parents=True, exist_ok=True)
    if not token:
        print("\n-- websocket auth for main.py --")
        print("this is the secret bot.py has to send. leave blank to generate one.")
        token = ask("auth token")
        if not token:
            token = secrets.token_urlsafe(32)
            print(f"generated token: {token}")
    (config_dir / "auth_token.txt").write_text(f"{token}\n", encoding="utf-8")
    print(f"wrote {config_dir / 'auth_token.txt'}")
    print(f"\ngive this token (and this machine's public wss:// url) to whoever's setting up bot.py:\n  {token}")


def main():
    print("faggot-chan setup wizard\n")

    colab = ask_yn("is this for a colab-hosted main.py")

    if not colab:
        # everything lives together, plain local setup
        bot_config = SCRIPT_DIR / "files" / "config"
        main_config = SCRIPT_DIR / "config"
        write_tokens_file(bot_config)
        token = write_connection_file(bot_config, default_uri="ws://0.0.0.0:8765")
        write_auth_token_file(main_config, token=token)
        print("\nall set. run main.py, then bot.py.")
        return

    where = ask("running this on the 'vps' (bot.py host) or 'colab' box (main.py host)", "vps")

    if where.lower().startswith("colab"):
        # this box just needs the auth token main.py will check — connection details
        # (discord token, server id) live on whichever box runs bot.py
        main_config = SCRIPT_DIR / "config"
        write_auth_token_file(main_config)
        print("\nnow run colab_setup.py to bring main.py + the tunnel up.")
        return

    # vps / bot.py host: skip discord-client setup entirely (handled separately),
    # this preset only wires up the connection to the colab-hosted main.py
    bot_config = SCRIPT_DIR / "files" / "config"
    if not (bot_config / "tokens.txt").exists():
        print("\nheads up: no files/config/tokens.txt found here — the vps preset skips")
        print("discord token setup, so make sure that file gets added separately.")
    write_connection_file(bot_config, default_uri="wss://your-colab-tunnel-url-here")
    print("\nall set. run bot.py once main.py is up on the colab side.")


if __name__ == "__main__":
    main()