mirror of
https://github.com/sapphiregaze/discord-gorp.git
synced 2025-12-05 20:40:02 -08:00
Finished Basic RPC Interface:
- Added configuration loading - Added RPC client creation & update - Added example configuration file
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1 +1,5 @@
|
||||
.direnv
|
||||
.DS_Store
|
||||
.vscode
|
||||
|
||||
config.yaml
|
||||
@@ -1,5 +1,7 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
import "github.com/sapphiregaze/discord-gorp/internal/updater"
|
||||
|
||||
func main() {
|
||||
updater.Start()
|
||||
}
|
||||
|
||||
25
configs/config.example.yaml
Normal file
25
configs/config.example.yaml
Normal file
@@ -0,0 +1,25 @@
|
||||
activity:
|
||||
application_id: "<DISCORD_APPLICATION_ID>"
|
||||
name: "<CUSTOM_NAME>"
|
||||
type: 0
|
||||
state: "<CUSTOM_STATE>"
|
||||
details: "<CUSTOM_DETAILS>"
|
||||
timestamp:
|
||||
start: 0000000001
|
||||
end: 2147483647
|
||||
assets:
|
||||
large_image: ""
|
||||
large_text: ""
|
||||
small_image: ""
|
||||
small_text: ""
|
||||
party:
|
||||
id: ""
|
||||
size: [1, 4]
|
||||
secrets:
|
||||
join: ""
|
||||
spectate: ""
|
||||
match: ""
|
||||
buttons:
|
||||
label: "<CUSTOM_BOTTON_LABEL>"
|
||||
url: "<CUSTOM_BOTTON_URL>"
|
||||
instance: true
|
||||
24
go.mod
24
go.mod
@@ -1,3 +1,27 @@
|
||||
module github.com/sapphiregaze/discord-gorp
|
||||
|
||||
go 1.22.7
|
||||
|
||||
require (
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||
github.com/sagikazarmark/locafero v0.4.0 // indirect
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||
github.com/spf13/afero v1.11.0 // indirect
|
||||
github.com/spf13/cast v1.6.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/spf13/viper v1.19.0 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||
golang.org/x/sys v0.18.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
56
go.sum
Normal file
56
go.sum
Normal file
@@ -0,0 +1,56 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
|
||||
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
|
||||
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
|
||||
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
|
||||
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
|
||||
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
|
||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -1 +0,0 @@
|
||||
package daemon
|
||||
47
internal/updater/updater.go
Normal file
47
internal/updater/updater.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/sapphiregaze/discord-gorp/pkg/config"
|
||||
"github.com/sapphiregaze/discord-gorp/pkg/rpc"
|
||||
)
|
||||
|
||||
func Start() {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintf("Failed to load config: %v", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
client, err := rpc.NewClient()
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintf("Failed to connect to Discord: %v", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
activity := &cfg.Activity
|
||||
client.SetActivity(activity)
|
||||
|
||||
ticker := time.NewTicker(5 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
client.SetActivity(activity)
|
||||
case <-sigs:
|
||||
slog.Info("Shutting down...")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +1,73 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Timestamp struct {
|
||||
Start int64 `mapstructure:"start" json:"start,omitempty"`
|
||||
End int64 `mapstructure:"end" json:"end,omitempty"`
|
||||
}
|
||||
|
||||
type Assets struct {
|
||||
LargeImage string `mapstructure:"large_image" json:"large_image,omitempty"`
|
||||
LargeText string `mapstructure:"large_text" json:"large_text,omitempty"`
|
||||
SmallImage string `mapstructure:"small_image" json:"small_image,omitempty"`
|
||||
SmallText string `mapstructure:"small_text" json:"small_text,omitempty"`
|
||||
}
|
||||
|
||||
type Party struct {
|
||||
ID string `mapstructure:"id" json:"id,omitempty"`
|
||||
Size [2]int `mapstructure:"size" json:"size,omitempty"`
|
||||
}
|
||||
|
||||
type Secrets struct {
|
||||
Join string `mapstructure:"join" json:"join,omitempty"`
|
||||
Spectate string `mapstructure:"spectate" json:"spectate,omitempty"`
|
||||
Match string `mapstructure:"match" json:"match,omitempty"`
|
||||
}
|
||||
|
||||
type Button struct {
|
||||
Label string `mapstructure:"label" json:"label,omitempty"`
|
||||
Url string `mapstructure:"url" json:"url,omitempty"`
|
||||
}
|
||||
|
||||
type Activity struct {
|
||||
ApplicationID string `mapstructure:"application_id" json:"application_id,omitempty"`
|
||||
Name string `mapstructure:"name" json:"name,omitempty"`
|
||||
Type int `mapstructure:"type" json:"type,omitempty"`
|
||||
State string `mapstructure:"state" json:"state,omitempty"`
|
||||
Details string `mapstructure:"details" json:"details,omitempty"`
|
||||
Timestamp *Timestamp `mapstructure:"timestamp" json:"timestamps,omitempty"`
|
||||
Assets *Assets `mapstructure:"assets" json:"assets,omitempty"`
|
||||
Party *Party `mapstructure:"party" json:"party,omitempty"`
|
||||
Secrets *Secrets `mapstructure:"secrets" json:"secrets,omitempty"`
|
||||
Buttons []Button `mapstructure:"buttons" json:"buttons,omitempty"`
|
||||
Instance bool `mapstructure:"instance" json:"instance"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Activity Activity `mapstructure:"activity"`
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
path, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
viper.SetConfigFile(filepath.Join(path, ".config/discord-gorp/config.yaml"))
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var config Config
|
||||
if err := viper.Unmarshal(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
@@ -1 +1,62 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"github.com/sapphiregaze/discord-gorp/pkg/config"
|
||||
)
|
||||
|
||||
type RPCClient struct {
|
||||
conn *websocket.Conn
|
||||
pid int
|
||||
}
|
||||
|
||||
func NewClient() (*RPCClient, error) {
|
||||
conn, _, err := websocket.DefaultDialer.Dial("ws://127.0.0.1:6463", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &RPCClient{
|
||||
conn: conn,
|
||||
pid: os.Getpid(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *RPCClient) Close() {
|
||||
r.conn.Close()
|
||||
slog.Info("Disconnected from Discord RPC")
|
||||
}
|
||||
|
||||
func (r *RPCClient) SetActivity(activity *config.Activity) error {
|
||||
payload := map[string]interface{}{
|
||||
"cmd": "SET_ACTIVITY",
|
||||
"args": map[string]interface{}{
|
||||
"pid": r.pid,
|
||||
"activity": activity,
|
||||
},
|
||||
"nonce": generateNonce(),
|
||||
}
|
||||
|
||||
message, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.conn.WriteMessage(websocket.TextMessage, message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info("Sent activity update to Discord RPC")
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateNonce() string {
|
||||
return time.Now().Format("20060102150405")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user