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
token
The discord token of the user
delegate
The delegate for this client.
configuration
An array of DiscordClientOption that can be used to customize the client
-
Begins the connection to Discord. Once this is called, wait for a
connect
event before trying to interact with the client.Declaration
Swift
open func connect()
-
Disconnects from Discord. A
disconnect
event is fired when the client has successfully disconnected.Calling this method turns off automatic resuming, set
resume
totrue
before callingconnect()
again.Declaration
Swift
open func disconnect()
-
Finds a channel by its snowflake.
Declaration
Swift
public func findChannel(fromId channelId: ChannelID) -> DiscordChannel?
Parameters
fromId
A channel snowflake
Return Value
An optional containing a
DiscordChannel
if 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
event
The dispatch event
data
The dispatch event’s data
-
Gets the
DiscordGuild
for a channel snowflake.Declaration
Swift
public func guildForChannel(_ channelId: ChannelID) -> DiscordGuild?
Parameters
channelId
A channel snowflake
Return Value
An optional containing a
DiscordGuild
if one was found. -
Joins a voice channel. A
voiceEngine.ready
event will be fired when the client has joined the channel.Declaration
Swift
open func joinVoiceChannel(_ channelId: ChannelID)
Parameters
channelId
The 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
onGuild
The 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
guildMembersChunk
will be fired.Declaration
Swift
open func requestAllUsers(on guildId: GuildID)
Parameters
on
The snowflake of the guild you wish to request all users.
-
Sets the user’s presence.
Declaration
Swift
open func setPresence(_ presence: DiscordPresenceUpdate)
Parameters
presence
The new presence object
-
Signals that the manager has finished connecting.
Declaration
Swift
open func shardManager(_ manager: DiscordShardManager, didConnect connected: Bool)
Parameters
manager
The manager.
didConnect
Should always be true.
-
Signals that the manager has disconnected.
Declaration
Swift
open func shardManager(_ manager: DiscordShardManager, didDisconnectWithReason reason: String)
Parameters
manager
The manager.
didDisconnectWithReason
The 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
manager
The manager.
shouldHandleEvent
The event to be handled.
withPayload
The payload that came with the event.
-
Called when an engine disconnects.
Declaration
Swift
open func voiceManager(_ manager: DiscordVoiceManager, didDisconnectEngine engine: DiscordVoiceEngine)
Parameters
manager
The manager.
engine
The 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
manager
The manager.
didReceiveVoiceData
The data received.
fromEngine
The 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
manager
The manager.
didReceiveVoiceData
The data received.
fromEngine
The 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
manager
The manager that is requesting an encoder.
needsDataSourceForEngine
The 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
manager
The manager.
engine
The 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
didCreateChannel
delegate method.Declaration
Swift
open func handleChannelCreate(with data: [String: Any])
Parameters
with
The 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
didDeleteChannel
delegate method.Declaration
Swift
open func handleChannelDelete(with data: [String: Any])
Parameters
with
The 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
didUpdateChannel
delegate method.Declaration
Swift
open func handleChannelUpdate(with data: [String: Any])
Parameters
with
The 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
didCreateGuild
delegate method.Declaration
Swift
open func handleGuildCreate(with data: [String: Any])
Parameters
with
The 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
didDeleteGuild
delegate method.Declaration
Swift
open func handleGuildDelete(with data: [String: Any])
Parameters
with
The 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
with
The 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
didAddGuildMember
delegate method.Declaration
Swift
open func handleGuildMemberAdd(with data: [String: Any])
Parameters
with
The 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
didRemoveGuildMember
delegate method.Declaration
Swift
open func handleGuildMemberRemove(with data: [String: Any])
Parameters
with
The 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
didUpdateGuildMember
delegate method.Declaration
Swift
open func handleGuildMemberUpdate(with data: [String: Any])
Parameters
with
The 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
with
The 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
didCreateRole
delegate method.Declaration
Swift
open func handleGuildRoleCreate(with data: [String: Any])
Parameters
with
The 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
didDeleteRole
delegate method.Declaration
Swift
open func handleGuildRoleRemove(with data: [String: Any])
Parameters
with
The 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
didUpdateRole
delegate method.Declaration
Swift
open func handleGuildRoleUpdate(with data: [String: Any])
Parameters
with
The 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
didUpdateGuild
delegate method.Declaration
Swift
open func handleGuildUpdate(with data: [String: Any])
Parameters
with
The 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
didUpdateMessage
delegate method.Declaration
Swift
open func handleMessageUpdate(with data: [String: Any])
Parameters
with
The 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
didCreateMessage
delegate method.Declaration
Swift
open func handleMessageCreate(with data: [String: Any])
Parameters
with
The 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
didReceivePresenceUpdate
delegate method.Declaration
Swift
open func handlePresenceUpdate(with data: [String: Any])
Parameters
with
The 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
didReceiveReady
delegate method.Declaration
Swift
open func handleReady(with data: [String: Any])
Parameters
with
The 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
with
The 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
didReceiveVoiceStateUpdate
delegate method.Declaration
Swift
open func handleVoiceStateUpdate(with data: [String: Any])
Parameters
with
The data from the event