DiscordClient

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!
  • 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 to true before calling connect() 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

  • 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