Discord Client - nuclearace/SwiftDiscord GitHub Wiki

The main class that you will interacting with. The DiscordClient is in charge of keeping track of a Bot's/User's state. This includes connected guilds and its associated channels, members roles, etc.

open class DiscordClient : DiscordClientSpec, DiscordDispatchEventHandler {
	public let token: String

	public var engine: DiscordEngineSpec?
	public var handleQueue = DispatchQueue.main
	public var voiceEngine: DiscordVoiceEngineSpec?

	public var isBot: Bool {
		guard let user = self.user else { return false }

		return user.bot
	}

	public private(set) var connected = false
	public private(set) var guilds = [String: DiscordGuild]()
	public private(set) var relationships = [String: Any](/nuclearace/SwiftDiscord/wiki/String:-Any)()
	public private(set) var user: DiscordUser?
	public private(set) var voiceState: DiscordVoiceState?
}

Creating a Client

A client requires a JWT to authenticate with Discord. This can be found on your bot/application's page.

Once you have your JWT, you can create a client instance.

let client = DiscordClient(token: "my token")

Connecting Your Client

Once you've created your client and added all the event listeners, you can connect. A connect event will be fired once the client is connected

client.connect()

Events

Most Discord events are intercepted by the client and used to update its internal state. An event is then fired for that event for which you can add listeners for. For example, to listen for channels messages you would attach a listener like so:

client.on("messageCreate") {data in
	let message = data[0] as! DiscordMessage
	// do something with your message
}

Events that are not handled by the client are immediately delegated to listeners, using the raw event name.

In the event that the event is handled, then the event name emitted will be the camelCase of the event.

Event names can be found here. You can find the currently handled events here. Because this library is being rapidly updated, the list of handled events may change.

REST API

The Discord REST API can be easily accessed through the DiscordClient. Most endpoints that are used to retrieve things, i.e. GET, take a callback as their final argument. While most other endpoints do not take a callback, their changes will be received through event listeners.

More documentation to come soon...