fuzzydragon / Roblox-Game-Presence-For-Discord

Uses Discord RPC to give you a rich presence on discord for the current ROBLOX game you are playing.
16 stars 4 forks source link

Add studio support! #1

Open fuzzydragon opened 3 years ago

fuzzydragon commented 3 years ago

Should not be too hard. I believe the RobloxStudio player has a reference to the place it is currently editing when it is started.

Worst case scenario I can just use the title of the window since it should be the game I am editing?

fuzzydragon commented 3 years ago

Working demo:

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/process"
    "time"
    "regexp"
    "net/http"
    "encoding/json"
    "github.com/hugolgst/rich-go/client"
)

var (
    placeId string
    reset = false
)

type MarketPlaceInfo struct {
    Name        string      `json:"Name"`
    Description string      `json:"Description"`
    Creator     struct {
        ID              int    `json:"Id"`
        Name            string `json:"Name"`
        CreatorType     string `json:"CreatorType"`
        CreatorTargetID int    `json:"CreatorTargetId"`
    } `json:"Creator"`
    IconImageAssetID       int64       `json:"IconImageAssetId"`
}

func GetProcessByName(targetProcessName string) *process.Process {
    processes, _ := process.Processes()

    for _, proc := range processes {
        name, _ := proc.Name()

        if (name == targetProcessName) {
            return proc
        }
    }

    return nil
}

func GetPlaceInfoByPlaceId(placeId string) *MarketPlaceInfo {
    url := "https://api.roblox.com/marketplace/productinfo?assetId=" + placeId
    resp, _ := http.Get(url)

    defer resp.Body.Close()

    var info *MarketPlaceInfo

    json.NewDecoder(resp.Body).Decode(&info)

    return info
}

func GetRobloxProcess() *process.Process {
    roblox := GetProcessByName("RobloxPlayerBeta.exe")
    studio := GetProcessByName("RobloxStudioBeta.exe")

    if (roblox != nil) {
        return roblox
    } else if (studio != nil) {
        return studio
    }

    return nil
}

func UpdateRobloxPresence() {
    roblox := GetRobloxProcess()

    for (roblox == nil) {
        roblox = GetRobloxProcess()

        if (reset == false) {
            reset = true

            client.Logout()
            fmt.Println("reset client activity")
        }
    }

    client.Login("823294557155754005")

    name, _ := roblox.Name()

    robloxState := ""
    placeIdPattern := ""
    robloxIcon := ""

    if (name == "RobloxPlayerBeta.exe") {
        robloxState = "Playing "
        placeIdPattern = `placeId=(\d+)`
        robloxIcon = "roblox_logo"
    } else if (name == "RobloxStudioBeta.exe") {
        robloxState = "Editing "
        placeIdPattern = `placeId (\d+)`
        robloxIcon = "studio_logo"
    }

    reset = false

    args, _ := roblox.Cmdline()

    placePattern := regexp.MustCompile(placeIdPattern)
    placeMatch := placePattern.FindStringSubmatch(args)[1]

    now := time.Now()

    if (placeMatch != placeId) {
        placeId = placeMatch
        place := GetPlaceInfoByPlaceId(placeId)

        client.SetActivity(client.Activity {
            State: "by " + place.Creator.Name,
            Details: robloxState + place.Name,
            LargeImage: robloxIcon,
            LargeText: "ROBLOX!",
            Buttons: []*client.Button {
                &client.Button {
                    Label: "Open Game Page",
                    Url: "https://www.roblox.com/games/" + placeId + "/-",
                },
            },
            Timestamps: &client.Timestamps {
                Start: &now,
            },
        })

        fmt.Println("set activity: " + place.Name)
        fmt.Println("by: " + place.Creator.Name)
        fmt.Println(`state: `+ robloxState)
    }
}

func main() {
    for (true) {
        UpdateRobloxPresence()

        time.Sleep(time.Second * 5)
    }
}
fuzzydragon commented 3 years ago

Working demo errors if they open a local file.

panic: runtime error: index out of range [1] with length 0

goroutine 1 [running]:
main.UpdateRobloxPresence()
        C:/Users/Andrew/go/src/RobloxPresence/Main_debug.go:109 +0x8d8
main.main()
        C:/Users/Andrew/go/src/RobloxPresence/Main_debug.go:141 +0x29
exit status 2