DiscordClient
open class DiscordClient : DiscordClientSpec, DiscordDispatchEventHandler, DiscordEndpointConsumer
The base class for SwiftDiscord. Most interaction with Discord will be done through this class.
See DiscordEndpointConsumer for methods dealing with sending to Discord.
Creating a client:
self.client = DiscordClient(token: "Bot mysupersecretbottoken", configuration: [.log(.info)])
Once a client is created, you need to set its delegate so that you can start receiving events:
self.client.delegate = self
See DiscordClientDelegate for a list of delegate methods that can be implemented.
-
The rate limiter for this client.
Declaration
Swift
public var rateLimiter: DiscordRateLimiterSpec! -
The Discord JWT token.
Declaration
Swift
public let token: DiscordToken -
The client’s delegate.
Declaration
Swift
public weak var delegate: DiscordClientDelegate? -
If true, the client does not store presences.
Declaration
Swift
public var discardPresences = false -
The queue that callbacks are called on. In addition, any reads from any properties of DiscordClient should be made on this queue, as this is the queue where modifications on them are made.
Declaration
Swift
public var handleQueue = DispatchQueue.main -
The manager for this client’s shards.
Declaration
Swift
public var shardManager: DiscordShardManager! -
If we should only represent a single shard, this is the shard information.
Declaration
Swift
public var shardingInfo = try! DiscordShardInformation(shardRange: 0..<1, totalShards: 1) -
Whether large guilds should have their users fetched as soon as they are created.
Declaration
Swift
public var fillLargeGuilds = false -
Whether the client should query the API for users who aren’t in the guild
Declaration
Swift
public var fillUsers = false -
Whether the client should remove users from guilds when they go offline.
Declaration
Swift
public var pruneUsers = false -
Whether or not this client is connected.
Declaration
Swift
public private(set) var connected = false -
The direct message channels this user is in.
Declaration
Swift
public private(set) var directChannels = [ChannelID: DiscordTextChannel]() -
The guilds that this user is in.
Declaration
Swift
public private(set) var guilds = [GuildID: DiscordGuild]() -
The relationships this user has. Only valid for non-bot users.
Declaration
Swift
public private(set) var relationships = [[String: Any]]() -
The DiscordUser this client is connected to.
Declaration
Swift
public private(set) var user: DiscordUser? -
A manager for the voice engines.
Declaration
Swift
public private(set) var voiceManager: DiscordVoiceManager!
-
Declaration
Swift
public required init(token: DiscordToken, delegate: DiscordClientDelegate, configuration: [DiscordClientOption] = [])Parameters
tokenThe discord token of the user
delegateThe delegate for this client.
configurationAn array of DiscordClientOption that can be used to customize the client
-
Begins the connection to Discord. Once this is called, wait for a
connectevent before trying to interact with the client.Declaration
Swift
open func connect() -
Disconnects from Discord. A
disconnectevent is fired when the client has successfully disconnected.Calling this method turns off automatic resuming, set
resumetotruebefore callingconnect()again.Declaration
Swift
open func disconnect() -
Finds a channel by its snowflake.
Declaration
Swift
public func findChannel(fromId channelId: ChannelID) -> DiscordChannel?Parameters
fromIdA channel snowflake
Return Value
An optional containing a
DiscordChannelif one was found. -
Handles a dispatch event. This will call one of the other handle methods or the standard event handler.
Declaration
Swift
open func handleDispatch(event: DiscordDispatchEvent, data: DiscordGatewayPayloadData)Parameters
eventThe dispatch event
dataThe dispatch event’s data
-
Gets the
DiscordGuildfor a channel snowflake.Declaration
Swift
public func guildForChannel(_ channelId: ChannelID) -> DiscordGuild?Parameters
channelIdA channel snowflake
Return Value
An optional containing a
DiscordGuildif one was found. -
Joins a voice channel. A
voiceEngine.readyevent will be fired when the client has joined the channel.Declaration
Swift
open func joinVoiceChannel(_ channelId: ChannelID)Parameters
channelIdThe snowflake of the voice channel you would like to join
-
Leaves the voice channel that is associated with the guild specified.
Declaration
Swift
open func leaveVoiceChannel(onGuild guildId: GuildID)Parameters
onGuildThe snowflake of the guild that you want to leave.
-
Requests all users from Discord for the guild specified. Use this when you need to get all users on a large guild. Multiple
guildMembersChunkwill be fired.Declaration
Swift
open func requestAllUsers(on guildId: GuildID)Parameters
onThe snowflake of the guild you wish to request all users.
-
Sets the user’s presence.
Declaration
Swift
open func setPresence(_ presence: DiscordPresenceUpdate)Parameters
presenceThe new presence object
-
Signals that the manager has finished connecting.
Declaration
Swift
open func shardManager(_ manager: DiscordShardManager, didConnect connected: Bool)Parameters
managerThe manager.
didConnectShould always be true.
-
Signals that the manager has disconnected.
Declaration
Swift
open func shardManager(_ manager: DiscordShardManager, didDisconnectWithReason reason: String)Parameters
managerThe manager.
didDisconnectWithReasonThe reason the manager disconnected.
-
Signals that the manager received an event. The client should handle this.
Declaration
Swift
open func shardManager(_ manager: DiscordShardManager, shouldHandleEvent event: DiscordDispatchEvent, withPayload payload: DiscordGatewayPayload)Parameters
managerThe manager.
shouldHandleEventThe event to be handled.
withPayloadThe payload that came with the event.
-
Called when an engine disconnects.
Declaration
Swift
open func voiceManager(_ manager: DiscordVoiceManager, didDisconnectEngine engine: DiscordVoiceEngine)Parameters
managerThe manager.
engineThe engine that disconnected.
-
Called when a voice engine receives opus voice data.
Declaration
Swift
open func voiceManager(_ manager: DiscordVoiceManager, didReceiveOpusVoiceData data: DiscordOpusVoiceData, fromEngine engine: DiscordVoiceEngine)Parameters
managerThe manager.
didReceiveVoiceDataThe data received.
fromEngineThe engine that received the data.
-
Called when a voice engine receives raw voice data.
Declaration
Swift
open func voiceManager(_ manager: DiscordVoiceManager, didReceiveRawVoiceData data: DiscordRawVoiceData, fromEngine engine: DiscordVoiceEngine)Parameters
managerThe manager.
didReceiveVoiceDataThe data received.
fromEngineThe engine that received the data.
-
Called when a voice engine needs a data source.
Not called on the handleQueue
Declaration
Swift
open func voiceManager(_ manager: DiscordVoiceManager, needsDataSourceForEngine engine: DiscordVoiceEngine) throws -> DiscordVoiceDataSource?Parameters
managerThe manager that is requesting an encoder.
needsDataSourceForEngineThe engine that needs an encoder
Return Value
An encoder.
-
Called when a voice engine is ready.
Declaration
Swift
open func voiceManager(_ manager: DiscordVoiceManager, engineIsReady engine: DiscordVoiceEngine)Parameters
managerThe manager.
engineThe engine that’s ready.
-
Handles channel creates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didCreateChanneldelegate method.Declaration
Swift
open func handleChannelCreate(with data: [String: Any])Parameters
withThe data from the event
-
Handles channel deletes from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didDeleteChanneldelegate method.Declaration
Swift
open func handleChannelDelete(with data: [String: Any])Parameters
withThe data from the event
-
Handles channel updates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didUpdateChanneldelegate method.Declaration
Swift
open func handleChannelUpdate(with data: [String: Any])Parameters
withThe data from the event
-
Handles guild creates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didCreateGuilddelegate method.Declaration
Swift
open func handleGuildCreate(with data: [String: Any])Parameters
withThe data from the event
-
Handles guild deletes from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didDeleteGuilddelegate method.Declaration
Swift
open func handleGuildDelete(with data: [String: Any])Parameters
withThe data from the event
-
Handles guild emoji updates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didUpdateEmojis:onGuild:delegate method.Declaration
Swift
open func handleGuildEmojiUpdate(with data: [String: Any])Parameters
withThe data from the event
-
Handles guild member adds from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didAddGuildMemberdelegate method.Declaration
Swift
open func handleGuildMemberAdd(with data: [String: Any])Parameters
withThe data from the event
-
Handles guild member removes from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didRemoveGuildMemberdelegate method.Declaration
Swift
open func handleGuildMemberRemove(with data: [String: Any])Parameters
withThe data from the event
-
Handles guild member updates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didUpdateGuildMemberdelegate method.Declaration
Swift
open func handleGuildMemberUpdate(with data: [String: Any])Parameters
withThe data from the event
-
Handles guild members chunks from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didHandleGuildMemberChunk:forGuild:delegate method.Declaration
Swift
open func handleGuildMembersChunk(with data: [String: Any])Parameters
withThe data from the event
-
Handles guild role creates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didCreateRoledelegate method.Declaration
Swift
open func handleGuildRoleCreate(with data: [String: Any])Parameters
withThe data from the event
-
Handles guild role removes from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didDeleteRoledelegate method.Declaration
Swift
open func handleGuildRoleRemove(with data: [String: Any])Parameters
withThe data from the event
-
Handles guild member updates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didUpdateRoledelegate method.Declaration
Swift
open func handleGuildRoleUpdate(with data: [String: Any])Parameters
withThe data from the event
-
Handles guild updates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didUpdateGuilddelegate method.Declaration
Swift
open func handleGuildUpdate(with data: [String: Any])Parameters
withThe data from the event
-
Handles message updates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didUpdateMessagedelegate method.Declaration
Swift
open func handleMessageUpdate(with data: [String: Any])Parameters
withThe data from the event
-
Handles message creates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didCreateMessagedelegate method.Declaration
Swift
open func handleMessageCreate(with data: [String: Any])Parameters
withThe data from the event
-
Handles presence updates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didReceivePresenceUpdatedelegate method.Declaration
Swift
open func handlePresenceUpdate(with data: [String: Any])Parameters
withThe data from the event
-
Handles the ready event from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didReceiveReadydelegate method.Declaration
Swift
open func handleReady(with data: [String: Any])Parameters
withThe data from the event
-
Handles voice server updates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Declaration
Swift
open func handleVoiceServerUpdate(with data: [String: Any])Parameters
withThe data from the event
-
Handles voice state updates from Discord. You shouldn’t need to call this method directly.
Override to provide additional customization around this event.
Calls the
didReceiveVoiceStateUpdatedelegate method.Declaration
Swift
open func handleVoiceStateUpdate(with data: [String: Any])Parameters
withThe data from the event
DiscordClient Class Reference