Skip to content

Discord Rich Presence Client written in PowerShell

License

Notifications You must be signed in to change notification settings

JaytheSpazz/discordrpc

 
 

Repository files navigation

discordrpc logo

 

discordrpc is a Discord Rich Presence Client written in PowerShell that allows you to change your presence to let people know that you're playing with PowerShell.

discordrpc works on PowerShell Core. This means that you can run all commands on Windows, Linux and macOS.

 

After seeing the Discord Presence plugin for VS Code, I was inspired to do something similar with PowerShell.

I made discordrpc as flexible as possible, so you can make your presence all your own, even changing the icons that show up. A wrapper command, Start-DSClient, has also been included to make getting started easy.

Install

Ensure that Discord is running and that "Game Activity" enabled in your settings.

Install from PowerShell Gallery

Run the following to install discordrpc from the PowerShell Gallery:

Install-Module discordrpc -Scope CurrentUser

Add to profile

discordrpc loads fast enough to add to your profile so everyone can always see when you're hanging out in PowerShell. Type notepad $profile then add these params and call the command.

$params = @{
    Details      = "Version $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)"
    State        = (Split-Path -Path $pwd -Leaf)
    Start        = "Now"
    UpdateScript = {
        # show the directory you are in. you can do anything here.
        Update-DSRichPresence -State (Split-Path -Path $pwd -Leaf)
    }
}

Start-DSClient @params

That will show results similar to the image above.

Image Options

The following images are available to use within your configuration. If you'd like me to add any more, let me know.

Image Key
avatar
icon

Highlights

  • You don't just have to show up as Playing PowerShell, you can also select from a variety of apps within Start-DSClient. You can see a list using Get-DSTemplate. Use the Template parameter within Start-DSClient to select the app you'd like to appear as.
  • If you don't find an application that matches your needs, you can create your own application and use your own title and "assets" or icons/images
  • You can use the wrapper command for ease or use the underlying commands individually for granular control

Integrate with other apps

Twitch

Here's a sample for Twitch using my other module tvbot.

$stream = Get-TvStream
$username = $stream.UserName
$params = @{
    Template     = "Twitch"
    Details      = $stream.Title
    State        = "$($stream.ViewerCount) Viewers"
    Start        = $stream.StartedAt
    Label        = "Watch Stream"
    Url          = "https://twitch.tv/$username"
    TimerRefresh = 30
    UpdateScript = {
        if (-not (Get-TvStream)) {
            Stop-DSClient
        }
    }
}

Start-DSClient @params

If I'm livestreaming, it'll appear like this..

Plex

Here's a sample for Plex using the community module PSPlex.

This script will update your presence every 60 seconds, updating as you watch new media on Plex.

# needs to be global for the timer update
function global:Get-CurrentlyWatching {
    Import-PlexConfiguration
    $session = Get-PlexSession | Where-Object { $psitem.player.state -in "playing", "paused" } | Select -First 1
    $xml = [xml]($session).OuterXml
    $media = $xml.Video

    switch ($media.type) {
        "episode" {
            $title = $media.grandparentTitle
            $state = $media.title
        }
        default {
            $title = $media.title
            $state = $media.year
        }
    }
    [pscustomobject]@{
        Title  = $title
        State  = $state
        End    = [int]$media.duration - [int]$media.viewOffset
        Label  = "Watch $title"
        Url    = $media.guid
        Player = $media.player
    }
}
$watching = Get-CurrentlyWatching
$params = @{
    Template       = "Plex"
    Details        = $watching.Title
    State          = $watching.State
    End            = $watching.End
    Label          = $watching.Label
    Url            = $watching.Url
    TimerRefresh   = 60
    UpdateScript   = {
        $watching = global:Get-CurrentlyWatching
        if (-not $watching) {
            Stop-DSClient
        }

        if ($watching.Player.state -eq "paused") {
            $timestamp = New-DSTimestamp
        } else {
            $timestamp = New-DSTimestamp -End $watching.End
        }
        $button = New-DSButton -Label $watching.Label -Url $watching.Url
        $params = @{
            Buttons   = $button
            Details   = $watching.Title
            State     = $watching.State
            Timestamp = $timestamp
        }
        Update-DSRichPresence @params
    }
}

Start-DSClient @params

If I'm playing Bob's Burgers, it'll appear like this..

Advanced commands

If you'd like to control all of the exact outcome, you can use the sample code below.

In the command below, you can use a custom application ID, customize your timer, see info messages and more.

$params = @{
    ApplicationID  = "824593663883214948"
    LargeImageKey  = "avatar"
    LargeImageText = "Summoners Rift"
    SmallImageKey  = "icon"
    SmallImageText = "Lvl 7"
    Label          = "Potato 🥔"
    Url            = "https://github.com/potatoqualitee/discordrpc"
    Details        = "Version $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)"
    State          = (Split-Path -Path $pwd -Leaf)
    LoggerType     = "ConsoleLogger"
    LoggerLevel    = "Info"
    TimerRefresh   = 10
    Start          = "Now"
    UpdateScript   = {
        Update-DSAsset -LargeImageText "Timer worked!" -SmallImageText "Lvl 10"
        Update-DSRichPresence -State (Split-Path -Path $pwd -Leaf)
    }
}

Start-DSClient @params

This setup will show logs in the console, which is inconvenient unless you are debugging something.

Build your presence, step by step

Here's the code I used while testing the module. You can use this to see how different settings can change the presence.

$assets = New-DSAsset -LargeImageKey avatar -LargeImageText "Summoners Rift" -SmallImageKey icon -SmallImageText "Lvl 7"
$timestamp = New-DSTimestamp -Start (Get-Date).AddMinutes(-3) -End (Get-Date).AddMinutes(3)
$button = New-DSButton -Label "Potato 🥔" -Url https://github.com/potatoqualitee/discordrpc
$party = New-DSParty -Size 10 -Privacy Public -Max 100
$presence = New-DSRichPresence -Asset $assets -State "presence.ps1" -Details "Some details" -Timestamp $timestamp -Buttons $button -Party $party
$logger = New-DSLogger -Type ConsoleLogger -Level Info
$client = New-DSClient -ApplicationID 824593663883214948 -Presence $presence -Logger $logger

See what other commands are available

Get-Command -Module discordrpc

Get help

A lot of help has been included. If you want to know more about a command such as New-DSLogger run the following:

Get-Help New-DSLogger
Get-Help New-DSLogger -Examples

Reference

Discord RPC is a library for interfacing your game with a locally running Discord desktop client. This PowerShell module wraps Lachee's C# library, discord-rpc-csharp

You can visit the Discord Developer Portal to learn more or create your own assets to set your own app name and icons. You'll need to change the ApplicationID to the client key you are provided.

When searching for a list of Client IDs already available on Discord, I found the PreMiD app, which is a mature product to show a bunch of rich presences (though not PowerShell). If you'd like to create your own Rich Presence for an app, the code in their repo may provide good strategies. Or really, if you like this rich presence functionality and don't want to use PowerShell, premid.app is awesome and just works.

About

Discord Rich Presence Client written in PowerShell

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PowerShell 100.0%