Skip to content

oomfinator/SwiftRPC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftRPC

GitHub Release GitHub Licence

A Swift package to interface with Discord's IPC sockets and display a rich presence activity.

Usage

Xcode project

Navigator > Project Name > Add Package Dependencies... > Add SwiftRPC.

Swift package

Add SwiftRPC to your package's dependencies.

.package(url: "https://github.com/darwinx64/SwiftRPC.git", branch: "master")

Example

import SwiftRPC
import AppKit

class PresenceDelegate: RichPresenceDelegate {

	/// A RichPresence instance that will get info
	/// from this delegate's methods.
	var rpc: RichPresence? = nil

	/// Make an `Activity` with a few of `Activity`'s
	/// overloads. There are more, such as `timestamps`
	static func makeActivity() -> Activity {
		Activity(
			details: "Working in a project",
			state: "In a file",
			assets: .init(
				largeImage: "appIcon"
			)
		)
	}

	/// Override default error handling with our own
	/// In this case, safely recover from any error
	///
	/// You can do things such as notify UI changes
	/// here, to tell the user something went wrong,
	/// or handle different errors differently
	override func richPresence(responseTo error: any Error) {
		print(error)
		self.rpc?.stop()
		do {
			try self.rpc?.start()
		} catch {
			richPresence(responseTo: error)
		}
	}
	
	required init() {
		super.init(id: "1234567890987654321", activity: Self.makeActivity())
		self.rpc = .init(delegate: self)
		do {
			try self.rpc?.start()
		} catch {
			richPresence(responseTo: error)
		}
	}
}

About

Discord RPC library for Swift

Resources

License

Stars

Watchers

Forks

Languages