From 55e1c57b49d8450537aca7f8a6d6293da78f403b Mon Sep 17 00:00:00 2001 From: wlinator Date: Tue, 7 May 2024 05:44:31 -0400 Subject: [PATCH] keeponline init --- .env.example | 8 +++++ .gitignore | 6 ++++ Dockerfile | 11 +++++++ docker-compose.yml | 8 +++++ keeponline.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 6 files changed, 112 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 keeponline.py create mode 100644 requirements.txt diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..c375744 --- /dev/null +++ b/.env.example @@ -0,0 +1,8 @@ +# your discord token +TOKEN= + +# online, dnd or idle +MODE_STATUS=online + +# status message +CUSTOM_STATUS=Doing something exciting! diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d19b3db --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +venv/ +.idea/ +__pycache__/ + +.env +*.log diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fa8ff5c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.11 +ARG DEBIAN_FRONTEND=noninteractive + +WORKDIR /usr/src/app + +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +CMD [ "python", "./keeponline.py" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2a29dd9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +services: + core: + build: . + restart: always + environment: + ONLINE_TOKEN: ${TOKEN} + ONLINE_MODE_STATUS: ${MODE_STATUS} + ONLINE_CUSTOM_STATUS: ${CUSTOM_STATUS} diff --git a/keeponline.py b/keeponline.py new file mode 100644 index 0000000..5404189 --- /dev/null +++ b/keeponline.py @@ -0,0 +1,77 @@ +import os +import sys +import json +import time +import requests +import websocket + +usertoken = os.environ.get("ONLINE_TOKEN") +status = os.environ.get("ONLINE_MODE_STATUS") +custom_status = os.environ.get("ONLINE_CUSTOM_STATUS") + +headers = {"Authorization": usertoken, "Content-Type": "application/json"} + +validate = requests.get('https://discordapp.com/api/v9/users/@me', headers=headers) +if validate.status_code != 200: + print("[ERROR] Your token might be invalid. Please check it again.") + sys.exit() + +userinfo = requests.get('https://discordapp.com/api/v9/users/@me', headers=headers).json() +username = userinfo["username"] +discriminator = userinfo["discriminator"] +userid = userinfo["id"] + +def onliner(token, status): + ws = websocket.WebSocket() + ws.connect("wss://gateway.discord.gg/?v=9&encoding=json") + start = json.loads(ws.recv()) + heartbeat = start["d"]["heartbeat_interval"] + auth = { + "op": 2, + "d": { + "token": token, + "properties": { + "$os": "Windows 10", + "$browser": "Google Chrome", + "$device": "Windows", + }, + "presence": {"status": status, "afk": False}, + }, + "s": None, + "t": None, + } + ws.send(json.dumps(auth)) + cstatus = { + "op": 3, + "d": { + "since": 0, + "activities": [ + { + "type": 4, + "state": custom_status, + "name": "Custom Status", + "id": "custom", + #Uncomment the below lines if you want an emoji in the status + #"emoji": { + #"name": "emoji name", + #"id": "emoji id", + #"animated": False, + #}, + } + ], + "status": status, + "afk": False, + }, + } + ws.send(json.dumps(cstatus)) + online = {"op": 1, "d": "None"} + time.sleep(heartbeat / 1000) + ws.send(json.dumps(online)) + +def run_onliner(): + print(f"Logged in as {username}#{discriminator} ({userid}).") + while True: + onliner(usertoken, status) + time.sleep(50) + +run_onliner() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d1b7668 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +requests +websocket-client