This fork provides a more up-to-date and maintained version of the SwordRPC library, which allows you to integrate Discord Rich Presence into your Swift apps. The code has been heavily documented and the package has been updated with new features and fixes, hopefully making it somewhat more enjoyable to use.
View the complete documentation here.
Some of the changes so far include:
- Source updated for Swift 5.0 or higher, support for Swift Package Manager, and updated the Socket dependency.
- Extensive documentation with DocC & reformatted source code
- Timestamp
Double->Intauto conversion that integrates nicely with any existing usages out of the box. - Return
Result<Void,SwordRPCError>inconnect()to provide explicit.failurereasons for additional context (e.g. Discord not detected). - Make
JoinRequestproperties public so that callers can display information about the users who request to join the activity. - Fix encoding by using Optionals to skip nil rich presence values instead of sending an empty string.
- Upgraded platform req to macOS
v10_15- Removes usages of
DispatchQueuein favor of Swift Concurrency'sTaskqueues.
- Removes usages of
- Other random things like
Secrets?nil to allow buttons, favoring strict types such asTimeIntervaloverInt, etc. - Added support for RPC Buttons (Discord only allows up to 2 buttons)
- uhhh a bunch of other stuff I'm too lazy to name, just view the source diff
Tip
Open an issue or PR if you have any suggestions/contributions/bugs.
- macOS or Linux
- Swift 5.0 or greater
- In your Xcode project, go to File > Add Package Dependencies...
- Enter the repo URL
https://github.com/sjalkote/SwordRPC.gitand press the button to add it - Yeah that's it lol
import SwordRPC
/// Additional arguments:
/// handlerInterval: TimeInterval = 1 (decides how fast to check discord for updates, if needed use floats like 0.5 for 500ms)
/// autoRegister: Bool = true (automatically registers your application to discord's url scheme (discord-appid://))
/// steamId: String? = nil (this is for steam games on these platforms)
let rpc = SwordRPC(appId: "123")
rpc.onConnect { rpc in
var presence = RichPresence()
presence.details = "Ranked | Mode: \(mode)"
presence.state = "In a Group"
presence.timestamps.start = Date()
presence.timestamps.end = Date() + 600 // 600s = 10m
presence.assets.largeImage = "map1"
presence.assets.largeText = "Map 1"
presence.assets.smallImage = "character1"
presence.assets.smallText = "Character 1"
presence.party.max = 5
presence.party.size = 3
presence.party.id = "partyId"
presence.secrets.match = "matchSecret"
presence.secrets.join = "joinSecret"
presence.secrets.joinRequest = "joinRequestSecret"
rpc.setPresence(presence)
}
rpc.onDisconnect { rpc, code, msg in
print("It appears we have disconnected from Discord")
}
rpc.onError { rpc, code, msg in
print("It appears we have discovered an error!")
}
rpc.onJoinGame { rpc, secret in
print("We have found us a join game secret!")
}
rpc.onSpectateGame { rpc, secret in
print("Our user wants to spectate!")
}
rpc.onJoinRequest { rpc, request, secret in
print("Some user wants to play with us!")
print(request.username)
print(request.avatar)
print(request.discriminator)
print(request.userId)
rpc.reply(to: request, with: .yes) // or .no or .ignore
}
rpc.connect()import SwordRPC
class ViewController {
override func viewDidLoad() {
let rpc = SwordRPC(appId: "123")
rpc.delegate = self
rpc.connect()
}
}
extension ViewController: SwordRPCDelegate {
func swordRPCDidConnect(
_ rpc: SwordRPC
) {}
func swordRPCDidDisconnect(
_ rpc: SwordRPC,
code: Int?,
message msg: String?
) {}
func swordRPCDidReceiveError(
_ rpc: SwordRPC,
code: Int,
message msg: String
) {}
func swordRPCDidJoinGame(
_ rpc: SwordRPC,
secret: String
) {}
func swordRPCDidSpectateGame(
_ rpc: SwordRPC,
secret: String
) {}
func swordRPCDidReceiveJoinRequest(
_ rpc: SwordRPC,
request: JoinRequest,
secret: String
) {}
}