From cdec693d39ca7de649c7d46d4e103000bedba002 Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Sun, 24 Nov 2024 17:07:26 +0100 Subject: [PATCH] switch to typescript --- .gitignore | 2 +- config.json | 10 ------ status.txt => data/status.txt | 0 emotes.txt | 1 - run | 20 ----------- run-loop | 20 +++++++++++ run-rotate | 15 ++++++++ src/discord-status-set.ts | 68 +++++++++++++++++++++++++++++++++++ 8 files changed, 104 insertions(+), 32 deletions(-) delete mode 100644 config.json rename status.txt => data/status.txt (100%) delete mode 100644 emotes.txt delete mode 100755 run create mode 100755 run-loop create mode 100755 run-rotate create mode 100755 src/discord-status-set.ts diff --git a/.gitignore b/.gitignore index 1f4296a..9cabdb4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -/token +/data/token /repo /venv \ No newline at end of file diff --git a/config.json b/config.json deleted file mode 100644 index 1095667..0000000 --- a/config.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "token": "discord-account-token", - "status_sequence": [ - "online" - ], - "clear_enabled": false, - "clear_interval": 0, - "// 12 * 60 * 60 = 43200": "", - "speed_rotator": 43200 -} \ No newline at end of file diff --git a/status.txt b/data/status.txt similarity index 100% rename from status.txt rename to data/status.txt diff --git a/emotes.txt b/emotes.txt deleted file mode 100644 index 7fdd95d..0000000 --- a/emotes.txt +++ /dev/null @@ -1 +0,0 @@ -emoji2:1243572228868673566 diff --git a/run b/run deleted file mode 100755 index 1acbfd6..0000000 --- a/run +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/bash -set -e -cd "$(dirname "$(realpath "$0")")" - - -[ -d repo ] || git clone https://github.com/RELIHR/Discord-Status-Changer.git repo -[ -d venv ] || python3 -m venv venv -[ -f token ] || echo "token goes here" > token -. ./venv/bin/activate - - -pip install requests colorama -cp emotes.txt repo/emojis.txt -cp status.txt repo/text.txt -cp config.json repo/config.json -sed -i "s/discord-account-token/$(cat token)/g" repo/config.json - - -cd repo -python3 main.py diff --git a/run-loop b/run-loop new file mode 100755 index 0000000..c2b2a9d --- /dev/null +++ b/run-loop @@ -0,0 +1,20 @@ +#!/usr/bin/bash +set -e +cd "$(dirname "$(realpath "$0")")" + + +interval="6h" + + +while true +do + until ping -c 1 '0.0.0.0' > /dev/null + do echo "[run-loop] waiting for internet connection." + done + + ./run-rotate + + echo "[run-loop] Waiting $interval from" + echo " $(date)" + sleep "$interval" +done diff --git a/run-rotate b/run-rotate new file mode 100755 index 0000000..960fe3d --- /dev/null +++ b/run-rotate @@ -0,0 +1,15 @@ +#!/usr/bin/bash +set -e +cd "$(dirname "$(realpath "$0")")" + + +[ -f data/token ] || ( echo "[run-rotate] Must place a discord token in '$PWD/data/token'" && exit 1 ) + + +token="$( cat data/token)" +status="$(cat data/status.txt | shuf -n 1)" +emote='feur:1243572228868673566' + + +echo "[run-rotate] Setting status to '$status'" +./src/discord-status-set.ts "$token" "$status" --emote "$emote" diff --git a/src/discord-status-set.ts b/src/discord-status-set.ts new file mode 100755 index 0000000..e678a18 --- /dev/null +++ b/src/discord-status-set.ts @@ -0,0 +1,68 @@ +#!/usr/bin/env -S deno run --allow-net=discord.com --allow-import=deno.land + +import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts"; + +async function main() { + const args = await parse_args(); + const r = await set_status(args.token, args.status, args.emote); + if (!r.ok) { + console.error("[discord-status-set.ts] API Response failure", r.status); + console.error(await r.text()); + Deno.exit(1); + } +} + +async function parse_args() { + const { args, options } = await new Command() + .name("discord-status-set") + .description("Set the status of your discord profile.") + .arguments(" ") + .option( + "-e, --emote ", + "Emote to set the status to (format is ':').", + ) + .parse(); + const [token, status] = args; + let emote = undefined as Emote | undefined; + if (options.emote) { + const [name, id] = options.emote.split(":"); + if (id === undefined) { + throw new Error( + `Incorrect emote format: '${options.emote}' instead of ':'`, + ); + } + emote = { name, id }; + } + return { token, status, emote }; +} + +async function set_status( + token: string, + status: string, + emote: Emote | undefined = undefined, +) { + return await fetch("https://discord.com/api/v10/users/@me/settings", { + method: "PATCH", + headers: { + "Content-Type": "application/json", + "authorization": token, + }, + body: JSON.stringify({ + "custom_status": { + "text": status, + ...emote + ? { + "emoji_name": emote.name, + "emoji_id": emote.id, + } + : {}, + }, + "activities": [], + "status": "online", + }), + }); +} + +type Emote = { name: string; id: string }; + +if (import.meta.main) await main();