Skip to content

Instantly share code, notes, and snippets.

@CoolnsX
Created October 28, 2023 09:04
Show Gist options
  • Save CoolnsX/e7bf120953703abd4e0d4507606000e9 to your computer and use it in GitHub Desktop.
Save CoolnsX/e7bf120953703abd4e0d4507606000e9 to your computer and use it in GitHub Desktop.
Implementation of DIscord Rich presence in posix shell script, not depenedent upon python or any other programming language
#!/bin/sh
### Posix compliant script for Discord Rich presence (no need to have python and pypresence) ###
#encode opcode and payload length in little-endian format and prepend it with payload
encode_data() {
op=$1
datalen=${#2}
for i in $(seq 0 3); do
byte=$(((op >> (i * 8)) & 255))
#shellcheck disable=SC2059
printf "\\$(printf "%03o" "$byte")"
done
for i in $(seq 0 3); do
byte=$(((datalen >> (i * 8)) & 255))
#shellcheck disable=SC2059
printf "\\$(printf "%03o" "$byte")"
done
printf "%s" "$2"
}
#this do the handshake as well as set_activity
set_activity() {
#handshake
encode_data "0" '{"v":1,"client_id":"'$presence_client_id'"}'
#send activity
encode_data "1" "$1"
}
#main
presence_client_id="<your_client_id>"
discord_ipc="${XDG_RUNTIME_DIR}/discord-ipc-0"
# - need to write both inside a function since I want to send them at same time to netcat
# - activity will be shown as long as this running, if you do ctrl-c it "activity" will be disappeard
# - netcat should be bsd version, not gnu for this to work
# - $1 is actually payload json, atleast figure that out from internet
set_activity "$1" | nc -U "$discord_ipc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment