D++ (DPP)
C++ Discord API Bot Library
|
Implements a discord client. Each discord_client connects to one shard and derives from a websocket client. More...
#include <discordclient.h>
Public Member Functions | |
virtual void | log (dpp::loglevel severity, const std::string &msg) const override |
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. More... | |
virtual void | handle_event (const std::string &event, json &j, const std::string &raw) |
Handle an event (opcode 0) More... | |
uint64_t | get_guild_count () |
Get the Guild Count for this shard. More... | |
uint64_t | get_member_count () |
Get the Member Count for this shard. More... | |
uint64_t | get_channel_count () |
Get the Channel Count for this shard. More... | |
virtual void | one_second_timer () override |
Fires every second from the underlying socket I/O loop, used for sending heartbeats and any queued outbound websocket frames. More... | |
void | queue_message (const std::string &j, bool to_front=false) |
Queue a message to be sent via the websocket. More... | |
discord_client & | clear_queue () |
Clear the outbound message queue. More... | |
size_t | get_queue_size () |
Get the size of the outbound message queue. More... | |
bool | is_connected () |
Returns true if the shard is connected. More... | |
dpp::utility::uptime | get_uptime () |
Returns the connection time of the shard. More... | |
discord_client (dpp::cluster *_cluster, uint32_t _shard_id, uint32_t _max_shards, const std::string &_token, uint32_t intents=0, bool compressed=true, websocket_protocol_t ws_protocol=ws_json) | |
Construct a new discord_client object. More... | |
discord_client (discord_client &old, uint64_t sequence, const std::string &session_id) | |
Construct a discord_client object from another discord_client object Used when resuming, the url to connect to will be taken from the resume url of the other object, along with the seq number. More... | |
virtual | ~discord_client ()=default |
Destroy the discord client object. More... | |
uint64_t | get_decompressed_bytes_in () |
Get decompressed total bytes received. More... | |
virtual bool | handle_frame (const std::string &buffer, ws_opcode opcode) override |
Handle JSON from the websocket. More... | |
virtual void | error (uint32_t errorcode) override |
Handle a websocket error. More... | |
void | run () |
Start and monitor I/O loop. More... | |
virtual void | on_disconnect () override |
Called when the HTTP socket is closed. More... | |
discord_client & | connect_voice (snowflake guild_id, snowflake channel_id, bool self_mute=false, bool self_deaf=false, bool enable_dave=false) |
Connect to a voice channel. More... | |
discord_client & | disconnect_voice (snowflake guild_id) |
Disconnect from the connected voice channel on a guild. More... | |
voiceconn * | get_voice (snowflake guild_id) |
Get the dpp::voiceconn object for a specific guild on this shard. More... | |
virtual void | write (const std::string_view data, ws_opcode _opcode=OP_AUTO) |
Write to websocket. Encapsulates data in frames if the status is CONNECTED. More... | |
virtual bool | handle_buffer (std::string &buffer) override |
Processes incoming frames from the SSL socket input buffer. More... | |
virtual void | close () override |
Close websocket. More... | |
void | send_close_packet () |
Send OP_CLOSE error code 1000 to the other side of the connection. This indicates graceful close. More... | |
void | enable_raw_tracing () |
For low-level debugging, calling this function will enable low level I/O logging for this connection to the logger. This can be very loud, and output a lot of data, so only enable it selectively where you need it. More... | |
uint64_t | get_bytes_out () |
Get the bytes out objectGet total bytes sent. More... | |
uint64_t | get_bytes_in () |
Get total bytes received. More... | |
uint64_t | get_unique_id () const |
Every request made has a unique ID. This increments for every request, starting at 1. You can use this for statistics, or to associate requests and replies in external event loops. More... | |
std::string | get_cipher () |
Get SSL cipher name. More... | |
void | read_loop () |
Set up non blocking I/O and configure on_read, on_write and on_error. More... | |
void | socket_write (const std::string_view data) |
Write to the output buffer. More... | |
void | complete_handshake (const struct socket_events *ev) |
Called while SSL handshake is in progress. If the handshake completes, the state of the socket is progressed to an established state. More... | |
void | on_read (dpp::socket fd, const struct dpp::socket_events &ev) |
Called when the TCP socket has data to read. More... | |
void | on_write (dpp::socket fd, const struct dpp::socket_events &e) |
Called when the TCP socket can be written to without blocking. More... | |
void | on_error (dpp::socket fd, const struct dpp::socket_events &, int error_code) |
Called when there is an error on the TCP socket. More... | |
Public Attributes | |
class dpp::cluster * | creator |
Owning cluster. More... | |
uint32_t | heartbeat_interval |
Heartbeat interval for sending heartbeat keepalive. More... | |
time_t | last_heartbeat |
Last heartbeat. More... | |
uint32_t | shard_id |
Shard ID of this client. More... | |
uint32_t | max_shards |
Total number of shards. More... | |
uint64_t | last_seq |
Last sequence number received, for resumes and pings. More... | |
std::string | token |
Discord bot token. More... | |
uint32_t | intents |
Privileged gateway intents. More... | |
std::string | sessionid |
Discord session id. More... | |
std::shared_mutex | voice_mutex |
Mutex for voice connections map. More... | |
uint32_t | resumes |
Resume count. More... | |
uint32_t | reconnects |
Reconnection count. More... | |
double | websocket_ping |
Websocket latency in fractional seconds. More... | |
bool | ready |
True if READY or RESUMED has been received. More... | |
time_t | last_heartbeat_ack |
Last heartbeat ACK (opcode 11) More... | |
websocket_protocol_t | protocol |
Current websocket protocol, currently either ETF or JSON. More... | |
std::unordered_map< snowflake, std::unique_ptr< voiceconn > > | connecting_voice_channels |
List of voice channels we are connecting to keyed by guild id. More... | |
std::string | resume_gateway_url |
The gateway address we reconnect to when we resume a session. More... | |
bool | keepalive |
True if we are keeping the connection alive after it has finished. More... | |
class cluster * | owner |
Owning cluster. More... | |
Protected Member Functions | |
void | disconnect_voice_internal (snowflake guild_id, bool send_json=true) |
Disconnect from the connected voice channel on a guild. More... | |
void | start_connecting () |
Start connecting the websocket. More... | |
virtual void | connect () override |
Connect to websocket server. More... | |
ws_state | get_state () const |
Get websocket state. More... | |
void | do_raw_trace (const std::string &message) const |
If raw_trace is set to true, log a debug message for this connection. More... | |
Protected Attributes | |
std::string | last_ping_message |
Stores the most recent ping message on this shard, which we check for to monitor latency. More... | |
bool | timed_out |
If true the connection timed out while waiting, when waiting for SSL negotiation, TCP connect(), or HTTP. More... | |
time_t | timeout |
Time at which the connection should be abandoned, if we are still connecting or negotiating with a HTTP server. More... | |
std::string | buffer |
Input buffer received from socket. More... | |
std::string | obuffer |
Output buffer for sending to socket. More... | |
dpp::socket | sfd |
Raw file descriptor of connection. More... | |
openssl_connection * | ssl |
Openssl opaque contexts. More... | |
std::string | cipher |
SSL cipher in use. More... | |
time_t | last_tick |
For timers. More... | |
time_t | start |
Start time of connection. More... | |
uint8_t | connect_retries {0} |
How many times we retried connect() More... | |
std::string | hostname |
Hostname connected to. More... | |
std::string | port |
Port connected to. More... | |
uint64_t | bytes_out |
Bytes out. More... | |
uint64_t | bytes_in |
Bytes in. More... | |
bool | plaintext |
True for a plain text connection. More... | |
bool | connected {false} |
True if connection is completed. More... | |
bool | tcp_connect_done {false} |
True if tcp connect() succeeded. More... | |
timer | timer_handle |
Timer handle for one second timer. More... | |
uint64_t | unique_id |
Unique ID of socket used as a nonce You can use this to identify requests vs reply if you want. D++ itself only sets this, and does not use it in any logic. It starts at 1 and increments for each request made. More... | |
bool | raw_trace {false} |
Set this to true to log all IO to debug for this connection. This is an internal developer facility. Do not enable it unless you need to, as it will be very noisy. More... | |
Friends | |
class | dpp::events::voice_state_update |
Needed so that voice_state_update can call dpp::discord_client::disconnect_voice_internal. More... | |
class | dpp::events::guild_create |
Needed so that guild_create can request member chunks if you have the correct intents. More... | |
class | dpp::cluster |
Needed to allow cluster::set_presence to use the ETF functions. More... | |
Implements a discord client. Each discord_client connects to one shard and derives from a websocket client.
dpp::discord_client::discord_client | ( | dpp::cluster * | _cluster, |
uint32_t | _shard_id, | ||
uint32_t | _max_shards, | ||
const std::string & | _token, | ||
uint32_t | intents = 0 , |
||
bool | compressed = true , |
||
websocket_protocol_t | ws_protocol = ws_json |
||
) |
Construct a new discord_client object.
_cluster | The owning cluster for this shard |
_shard_id | The ID of the shard to start |
_max_shards | The total number of shards across all clusters |
_token | The bot token to use for identifying to the websocket |
intents | Privileged intents to use, a bitmask of values from dpp::intents |
compressed | True if the received data will be gzip compressed |
ws_protocol | Websocket protocol to use for the connection, JSON or ETF |
std::bad_alloc | Passed up to the caller if any internal objects fail to allocate, after cleanup has completed |
|
explicit |
Construct a discord_client object from another discord_client object Used when resuming, the url to connect to will be taken from the resume url of the other object, along with the seq number.
old | Previous connection to resume from |
sequence | Sequence number of previous session |
session_id | Session ID of previous session |
|
virtualdefault |
Destroy the discord client object.
discord_client & dpp::discord_client::clear_queue | ( | ) |
Clear the outbound message queue.
|
overridevirtualinherited |
Close websocket.
Reimplemented from dpp::ssl_client.
|
inherited |
Called while SSL handshake is in progress. If the handshake completes, the state of the socket is progressed to an established state.
ev | Socket events for the socket |
|
overrideprotectedvirtualinherited |
Connect to websocket server.
Reimplemented from dpp::ssl_client.
discord_client & dpp::discord_client::connect_voice | ( | snowflake | guild_id, |
snowflake | channel_id, | ||
bool | self_mute = false , |
||
bool | self_deaf = false , |
||
bool | enable_dave = false |
||
) |
Connect to a voice channel.
guild_id | Guild where the voice channel is |
channel_id | Channel ID of the voice channel |
self_mute | True if the bot should mute itself |
self_deaf | True if the bot should deafen itself |
enable_dave | True to enable DAVE E2EE - EXPERIMENTAL |
discord_client & dpp::discord_client::disconnect_voice | ( | snowflake | guild_id | ) |
Disconnect from the connected voice channel on a guild.
guild_id | The guild who's voice channel you wish to disconnect from |
|
protected |
Disconnect from the connected voice channel on a guild.
guild_id | The guild who's voice channel you wish to disconnect from |
send_json | True if we should send a json message confirming we are leaving the VC Should be set to false if we already receive this message in an event. |
|
protectedinherited |
If raw_trace is set to true, log a debug message for this connection.
message | debug message |
|
inherited |
For low-level debugging, calling this function will enable low level I/O logging for this connection to the logger. This can be very loud, and output a lot of data, so only enable it selectively where you need it.
Generally, you won't need this, it is a library development utility.
|
overridevirtual |
Handle a websocket error.
errorcode | The error returned from the websocket |
Reimplemented from dpp::websocket_client.
|
inherited |
Get total bytes received.
|
inherited |
Get the bytes out objectGet total bytes sent.
uint64_t dpp::discord_client::get_channel_count | ( | ) |
Get the Channel Count for this shard.
|
inherited |
Get SSL cipher name.
uint64_t dpp::discord_client::get_decompressed_bytes_in | ( | ) |
Get decompressed total bytes received.
This will always return 0 if the connection is not compressed
uint64_t dpp::discord_client::get_guild_count | ( | ) |
Get the Guild Count for this shard.
uint64_t dpp::discord_client::get_member_count | ( | ) |
Get the Member Count for this shard.
size_t dpp::discord_client::get_queue_size | ( | ) |
Get the size of the outbound message queue.
|
protectedinherited |
Get websocket state.
|
inherited |
Every request made has a unique ID. This increments for every request, starting at 1. You can use this for statistics, or to associate requests and replies in external event loops.
dpp::utility::uptime dpp::discord_client::get_uptime | ( | ) |
Returns the connection time of the shard.
Get the dpp::voiceconn object for a specific guild on this shard.
guild_id | The guild ID to retrieve the voice connection for |
|
overridevirtualinherited |
Processes incoming frames from the SSL socket input buffer.
buffer | The buffer contents. Can modify this value removing the head elements when processed. |
Reimplemented from dpp::ssl_client.
|
virtual |
Handle an event (opcode 0)
event | Event name, e.g. MESSAGE_CREATE |
j | JSON object for the event content |
raw | Raw JSON event string |
|
overridevirtual |
Handle JSON from the websocket.
buffer | The entire buffer content from the websocket client |
opcode | The type of frame, e.g. text or binary |
Reimplemented from dpp::websocket_client.
bool dpp::discord_client::is_connected | ( | ) |
Returns true if the shard is connected.
|
overridevirtual |
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.
severity | The log level from dpp::loglevel |
msg | The log message to output |
Reimplemented from dpp::ssl_client.
|
overridevirtual |
Called when the HTTP socket is closed.
Reimplemented from dpp::websocket_client.
|
inherited |
Called when there is an error on the TCP socket.
fd | File descriptor |
error_code | Error code |
|
inherited |
Called when the TCP socket has data to read.
fd | File descriptor |
ev | Socket events |
|
inherited |
Called when the TCP socket can be written to without blocking.
fd | File descriptor |
e | Socket events |
|
overridevirtual |
Fires every second from the underlying socket I/O loop, used for sending heartbeats and any queued outbound websocket frames.
Reimplemented from dpp::websocket_client.
void dpp::discord_client::queue_message | ( | const std::string & | j, |
bool | to_front = false |
||
) |
Queue a message to be sent via the websocket.
j | The JSON data of the message to be sent |
to_front | If 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) |
|
inherited |
Set up non blocking I/O and configure on_read, on_write and on_error.
std::exception | Any std::exception (or derivative) thrown from read_loop() indicates setup failed |
void dpp::discord_client::run | ( | ) |
Start and monitor I/O loop.
|
inherited |
Send OP_CLOSE error code 1000 to the other side of the connection. This indicates graceful close.
|
inherited |
Write to the output buffer.
data | Data to be written to the buffer. |
|
protected |
Start connecting the websocket.
Called from the constructor, or during reconnection
|
virtualinherited |
Write to websocket. Encapsulates data in frames if the status is CONNECTED.
data | The data to send. |
_opcode | The opcode of the data to send, either binary or text. The default is to use the socket's opcode as set in the constructor. |
|
friend |
Needed to allow cluster::set_presence to use the ETF functions.
|
friend |
Needed so that guild_create can request member chunks if you have the correct intents.
|
friend |
Needed so that voice_state_update can call dpp::discord_client::disconnect_voice_internal.
|
protectedinherited |
Input buffer received from socket.
|
protectedinherited |
Bytes in.
|
protectedinherited |
Bytes out.
|
protectedinherited |
SSL cipher in use.
|
protectedinherited |
How many times we retried connect()
|
protectedinherited |
True if connection is completed.
std::unordered_map<snowflake, std::unique_ptr<voiceconn> > dpp::discord_client::connecting_voice_channels |
List of voice channels we are connecting to keyed by guild id.
class dpp::cluster* dpp::discord_client::creator |
Owning cluster.
uint32_t dpp::discord_client::heartbeat_interval |
Heartbeat interval for sending heartbeat keepalive.
|
protectedinherited |
Hostname connected to.
uint32_t dpp::discord_client::intents |
Privileged gateway intents.
|
inherited |
True if we are keeping the connection alive after it has finished.
time_t dpp::discord_client::last_heartbeat |
Last heartbeat.
time_t dpp::discord_client::last_heartbeat_ack |
Last heartbeat ACK (opcode 11)
|
protected |
Stores the most recent ping message on this shard, which we check for to monitor latency.
uint64_t dpp::discord_client::last_seq |
Last sequence number received, for resumes and pings.
|
protectedinherited |
For timers.
uint32_t dpp::discord_client::max_shards |
Total number of shards.
|
protectedinherited |
Output buffer for sending to socket.
|
inherited |
Owning cluster.
|
protectedinherited |
True for a plain text connection.
|
protectedinherited |
Port connected to.
websocket_protocol_t dpp::discord_client::protocol |
Current websocket protocol, currently either ETF or JSON.
|
protectedinherited |
Set this to true to log all IO to debug for this connection. This is an internal developer facility. Do not enable it unless you need to, as it will be very noisy.
bool dpp::discord_client::ready |
True if READY or RESUMED has been received.
uint32_t dpp::discord_client::reconnects |
Reconnection count.
std::string dpp::discord_client::resume_gateway_url |
The gateway address we reconnect to when we resume a session.
uint32_t dpp::discord_client::resumes |
Resume count.
std::string dpp::discord_client::sessionid |
Discord session id.
|
protectedinherited |
Raw file descriptor of connection.
uint32_t dpp::discord_client::shard_id |
Shard ID of this client.
|
protectedinherited |
Openssl opaque contexts.
|
protectedinherited |
Start time of connection.
|
protectedinherited |
True if tcp connect() succeeded.
|
protectedinherited |
If true the connection timed out while waiting, when waiting for SSL negotiation, TCP connect(), or HTTP.
|
protectedinherited |
Time at which the connection should be abandoned, if we are still connecting or negotiating with a HTTP server.
|
protectedinherited |
Timer handle for one second timer.
std::string dpp::discord_client::token |
Discord bot token.
|
protectedinherited |
Unique ID of socket used as a nonce You can use this to identify requests vs reply if you want. D++ itself only sets this, and does not use it in any logic. It starts at 1 and increments for each request made.
std::shared_mutex dpp::discord_client::voice_mutex |
Mutex for voice connections map.
double dpp::discord_client::websocket_ping |
Websocket latency in fractional seconds.