D++ (DPP)
C++ Discord API Bot Library
dpp::DiscordClient Class Reference

Implements a discord client. Each DiscordClient connects to one shard and derives from a websocket client. More...

#include <discordclient.h>

+ Inheritance diagram for dpp::DiscordClient:
+ Collaboration diagram for dpp::DiscordClient:

Public Member Functions

virtual void log (dpp::loglevel severity, const std::string &msg) const
 
virtual void HandleEvent (const std::string &event, json &j, const std::string &raw)
 
uint64_t GetGuildCount ()
 Get the Guild Count for this shard. More...
 
uint64_t GetMemberCount ()
 Get the Member Count for this shard. More...
 
uint64_t GetChannelCount ()
 Get the Channel Count for this shard. More...
 
virtual void OneSecondTimer ()
 
void QueueMessage (const std::string &j, bool to_front=false)
 Queue a message to be sent via the websocket. More...
 
void ClearQueue ()
 Clear the outbound message queue. More...
 
size_t GetQueueSize ()
 Get the size of the outbound message queue. More...
 
bool IsConnected ()
 Returns true if the shard is connected. More...
 
dpp::utility::uptime Uptime ()
 Returns the connection time of the shard. More...
 
 DiscordClient (dpp::cluster *_cluster, uint32_t _shard_id, uint32_t _max_shards, const std::string &_token, uint32_t intents=0, bool compressed=true)
 
virtual ~DiscordClient ()
 
uint64_t GetDecompressedBytesIn ()
 
virtual bool HandleFrame (const std::string &buffer)
 
virtual void Error (uint32_t errorcode)
 
void Run ()
 
void ConnectVoice (snowflake guild_id, snowflake channel_id)
 Connect to a voice channel. More...
 
void DisconnectVoice (snowflake guild_id)
 Disconnect from the connected voice channel on a guild. More...
 
voiceconnGetVoice (snowflake guild_id)
 
virtual void write (const std::string &data)
 Write to websocket. Encapsulates data in frames if the status is CONNECTED. More...
 
virtual bool HandleBuffer (std::string &buffer)
 Processes incoming frames from the SSL socket input buffer. More...
 
virtual void close ()
 Close websocket. More...
 
uint64_t GetBytesOut ()
 
uint64_t GetBytesIn ()
 
void ReadLoop ()
 Nonblocking I/O loop. More...
 

Public Attributes

class dpp::clustercreator
 
uint32_t heartbeat_interval
 
time_t last_heartbeat
 
uint32_t shard_id
 
uint32_t max_shards
 
std::thread::native_handle_type thread_id
 
uint64_t last_seq
 
std::string token
 
uint32_t intents
 
std::string sessionid
 
std::mutex voice_mutex
 
uint32_t resumes
 
uint32_t reconnects
 
bool ready
 
time_t last_heartbeat_ack
 
std::unordered_map< snowflake, voiceconn * > connecting_voice_channels
 
std::function< int()> custom_readable_fd
 Attaching an additional file descriptor to this function will send notifications when there is data to read. More...
 
std::function< int()> custom_writeable_fd
 Attaching an additional file descriptor to this function will send notifications when you are able to write to the socket. More...
 
std::function< void()> custom_readable_ready
 This event will be called when you can read from the custom fd. More...
 
std::function< void()> custom_writeable_ready
 This event will be called when you can write to a custom fd. More...
 

Protected Member Functions

virtual void Connect ()
 
WSState GetState ()
 

Protected Attributes

std::string buffer
 
std::string obuffer
 
bool nonblocking
 
int sfd
 
SSL * ssl
 
SSL_CTX * ctx
 
std::string cipher
 
time_t last_tick
 
std::string hostname
 
std::string port
 
uint64_t bytes_out
 
uint64_t bytes_in
 

Detailed Description

Implements a discord client. Each DiscordClient connects to one shard and derives from a websocket client.

Constructor & Destructor Documentation

◆ DiscordClient()

dpp::DiscordClient::DiscordClient ( dpp::cluster _cluster,
uint32_t  _shard_id,
uint32_t  _max_shards,
const std::string &  _token,
uint32_t  intents = 0,
bool  compressed = true 
)

Constructor takes shard id, max shards and token.

Parameters
_clusterThe owning cluster for this shard
_shard_idThe ID of the shard to start
_max_shardsThe total number of shards across all clusters
_tokenThe bot token to use for identifying to the websocket
intentsPrivileged intents to use, a bitmask of values from dpp::intents
compressedTrue if the received data will be gzip compressed

◆ ~DiscordClient()

virtual dpp::DiscordClient::~DiscordClient ( )
virtual

Destructor

Member Function Documentation

◆ ClearQueue()

void dpp::DiscordClient::ClearQueue ( )

Clear the outbound message queue.

◆ close()

virtual void dpp::WSClient::close ( )
virtualinherited

Close websocket.

Reimplemented from dpp::SSLClient.

◆ Connect()

virtual void dpp::WSClient::Connect ( )
protectedvirtualinherited

(Re)connect

Reimplemented from dpp::SSLClient.

◆ ConnectVoice()

void dpp::DiscordClient::ConnectVoice ( snowflake  guild_id,
snowflake  channel_id 
)

Connect to a voice channel.

Parameters
guild_idGuild where the voice channel is
channel_idChannel ID of the voice channel

◆ DisconnectVoice()

void dpp::DiscordClient::DisconnectVoice ( snowflake  guild_id)

Disconnect from the connected voice channel on a guild.

Parameters
guild_idThe guild who's voice channel you wish to disconnect from

◆ Error()

virtual void dpp::DiscordClient::Error ( uint32_t  errorcode)
virtual

Handle a websocket error.

Parameters
errorcodeThe error returned from the websocket

Reimplemented from dpp::WSClient.

◆ GetBytesIn()

uint64_t dpp::SSLClient::GetBytesIn ( )
inherited

Get total bytes received

◆ GetBytesOut()

uint64_t dpp::SSLClient::GetBytesOut ( )
inherited

Get total bytes sent

◆ GetChannelCount()

uint64_t dpp::DiscordClient::GetChannelCount ( )

Get the Channel Count for this shard.

Returns
uint64_t channel count

◆ GetDecompressedBytesIn()

uint64_t dpp::DiscordClient::GetDecompressedBytesIn ( )

Get decompressed total bytes received

◆ GetGuildCount()

uint64_t dpp::DiscordClient::GetGuildCount ( )

Get the Guild Count for this shard.

Returns
uint64_t guild count

◆ GetMemberCount()

uint64_t dpp::DiscordClient::GetMemberCount ( )

Get the Member Count for this shard.

Returns
uint64_t member count

◆ GetQueueSize()

size_t dpp::DiscordClient::GetQueueSize ( )

Get the size of the outbound message queue.

Returns
The size of the queue

◆ GetState()

WSState dpp::WSClient::GetState ( )
protectedinherited

Get websocket state

Returns
websocket state

◆ GetVoice()

voiceconn * dpp::DiscordClient::GetVoice ( snowflake  guild_id)

◆ HandleBuffer()

virtual bool dpp::WSClient::HandleBuffer ( std::string &  buffer)
virtualinherited

Processes incoming frames from the SSL socket input buffer.

Parameters
bufferThe buffer contents. Can modify this value removing the head elements when processed.

Reimplemented from dpp::SSLClient.

◆ HandleEvent()

virtual void dpp::DiscordClient::HandleEvent ( const std::string &  event,
json &  j,
const std::string &  raw 
)
virtual

Handle an event (opcode 0)

Parameters
eventEvent name, e.g. MESSAGE_CREATE
jJSON object for the event content
rawRaw JSON event string

◆ HandleFrame()

virtual bool dpp::DiscordClient::HandleFrame ( const std::string &  buffer)
virtual

Handle JSON from the websocket.

Parameters
bufferThe entire buffer content from the websocket client
Returns
True if a frame has been handled

Reimplemented from dpp::WSClient.

◆ IsConnected()

bool dpp::DiscordClient::IsConnected ( )

Returns true if the shard is connected.

Returns
True if connected

◆ log()

virtual void dpp::DiscordClient::log ( dpp::loglevel  severity,
const std::string &  msg 
) const
virtual

Log a message to whatever log the user is using. The logged message is passed up the chain to the on_log event in user code which can then do whatever it wants to do with it.

Parameters
severityThe log level from dpp::loglevel
msgThe log message to output

Reimplemented from dpp::SSLClient.

◆ OneSecondTimer()

virtual void dpp::DiscordClient::OneSecondTimer ( )
virtual

Fires every second from the underlying socket I/O loop, used for sending heartbeats

Reimplemented from dpp::WSClient.

◆ QueueMessage()

void dpp::DiscordClient::QueueMessage ( const std::string &  j,
bool  to_front = false 
)

Queue a message to be sent via the websocket.

Parameters
jThe JSON data of the message to be sent
to_frontIf set to true, will place the message at the front of the queue not the back (this is for urgent messages such as heartbeat, presence, so they can take precedence over chunk requests etc)

◆ ReadLoop()

void dpp::SSLClient::ReadLoop ( )
inherited

Nonblocking I/O loop.

◆ Run()

void dpp::DiscordClient::Run ( )

Start and monitor I/O loop

◆ Uptime()

dpp::utility::uptime dpp::DiscordClient::Uptime ( )

Returns the connection time of the shard.

Returns
dpp::utility::uptime Detail of how long the shard has been connected for

◆ write()

virtual void dpp::WSClient::write ( const std::string &  data)
virtualinherited

Write to websocket. Encapsulates data in frames if the status is CONNECTED.

Parameters
dataThe data to send.

Reimplemented from dpp::SSLClient.

Member Data Documentation

◆ buffer

std::string dpp::SSLClient::buffer
protectedinherited

Input buffer received from openssl

◆ bytes_in

uint64_t dpp::SSLClient::bytes_in
protectedinherited

Bytes in

◆ bytes_out

uint64_t dpp::SSLClient::bytes_out
protectedinherited

Bytes out

◆ cipher

std::string dpp::SSLClient::cipher
protectedinherited

SSL cipher in use

◆ connecting_voice_channels

std::unordered_map<snowflake, voiceconn*> dpp::DiscordClient::connecting_voice_channels

List of voice channels we are connecting to keyed by guild id

◆ creator

class dpp::cluster* dpp::DiscordClient::creator

Owning cluster

◆ ctx

SSL_CTX* dpp::SSLClient::ctx
protectedinherited

OpenSSL context

◆ custom_readable_fd

std::function<int()> dpp::SSLClient::custom_readable_fd
inherited

Attaching an additional file descriptor to this function will send notifications when there is data to read.

NOTE: Only hook this if you NEED it as it can increase CPU usage of the thread! Returning -1 means that you don't want to be notified.

◆ custom_readable_ready

std::function<void()> dpp::SSLClient::custom_readable_ready
inherited

This event will be called when you can read from the custom fd.

◆ custom_writeable_fd

std::function<int()> dpp::SSLClient::custom_writeable_fd
inherited

Attaching an additional file descriptor to this function will send notifications when you are able to write to the socket.

NOTE: Only hook this if you NEED it as it can increase CPU usage of the thread! You should toggle this to -1 when you do not have anything to write otherwise it'll keep triggering repeatedly (it is level triggered).

◆ custom_writeable_ready

std::function<void()> dpp::SSLClient::custom_writeable_ready
inherited

This event will be called when you can write to a custom fd.

◆ heartbeat_interval

uint32_t dpp::DiscordClient::heartbeat_interval

Heartbeat interval for sending heartbeat keepalive

◆ hostname

std::string dpp::SSLClient::hostname
protectedinherited

Hostname connected to

◆ intents

uint32_t dpp::DiscordClient::intents

Privileged gateway intents

◆ last_heartbeat

time_t dpp::DiscordClient::last_heartbeat

Last heartbeat

◆ last_heartbeat_ack

time_t dpp::DiscordClient::last_heartbeat_ack

Last heartbeat ACK (opcode 11)

◆ last_seq

uint64_t dpp::DiscordClient::last_seq

Last sequence number received, for resumes and pings

◆ last_tick

time_t dpp::SSLClient::last_tick
protectedinherited

For timers

◆ max_shards

uint32_t dpp::DiscordClient::max_shards

Total number of shards

◆ nonblocking

bool dpp::SSLClient::nonblocking
protectedinherited

True if in nonblocking mode. The socket switches to nonblocking mode once ReadLoop is called.

◆ obuffer

std::string dpp::SSLClient::obuffer
protectedinherited

Output buffer for sending to openssl

◆ port

std::string dpp::SSLClient::port
protectedinherited

Port connected to

◆ ready

bool dpp::DiscordClient::ready

True if READY or RESUMED has been received

◆ reconnects

uint32_t dpp::DiscordClient::reconnects

Reconnection count

◆ resumes

uint32_t dpp::DiscordClient::resumes

Resume count

◆ sessionid

std::string dpp::DiscordClient::sessionid

Discord session id

◆ sfd

int dpp::SSLClient::sfd
protectedinherited

Raw file descriptor of connection

◆ shard_id

uint32_t dpp::DiscordClient::shard_id

Shard ID of this client

◆ ssl

SSL* dpp::SSLClient::ssl
protectedinherited

OpenSSL session

◆ thread_id

std::thread::native_handle_type dpp::DiscordClient::thread_id

Thread ID

◆ token

std::string dpp::DiscordClient::token

Discord bot token

◆ voice_mutex

std::mutex dpp::DiscordClient::voice_mutex

Mutex for voice connections map

D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0