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...
Public Member Functions | |
virtual void | log (dpp::loglevel severity, const std::string &msg) const |
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 () |
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... | |
virtual | ~discord_client () |
Destroy the discord client object. More... | |
uint64_t | get_decompressed_bytes_in () |
Get the decompressed bytes in objectGet decompressed total bytes received. More... | |
virtual bool | handle_frame (const std::string &buffer) |
Handle JSON from the websocket. More... | |
virtual void | error (uint32_t errorcode) |
Handle a websocket error. More... | |
void | run () |
Start and monitor I/O loop. More... | |
discord_client & | connect_voice (snowflake guild_id, snowflake channel_id, bool self_mute=false, bool self_deaf=false) |
Connect to a voice channel. More... | |
discord_client & | change_voice_channel (snowflake guild_id, snowflake channel_id, bool self_mute=false, bool self_deaf=false) |
Change 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 &data) |
Write to websocket. Encapsulates data in frames if the status is CONNECTED. More... | |
virtual bool | handle_buffer (std::string &buffer) |
Processes incoming frames from the SSL socket input buffer. More... | |
virtual void | close () |
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... | |
uint64_t | get_bytes_out () |
Get the bytes out objectGet total bytes sent. More... | |
uint64_t | get_bytes_in () |
Get total bytes received. More... | |
std::string | get_cipher () |
Get SSL cipher name. More... | |
void | read_loop () |
Nonblocking I/O loop. 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... | |
std::thread::native_handle_type | thread_id |
Thread ID. 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... | |
socket_callback_t | custom_readable_fd |
Attaching an additional file descriptor to this function will send notifications when there is data to read. More... | |
socket_callback_t | custom_writeable_fd |
Attaching an additional file descriptor to this function will send notifications when you are able to write to the socket. More... | |
socket_notification_t | custom_readable_ready |
This event will be called when you can read from the custom fd. More... | |
socket_notification_t | custom_writeable_ready |
This event will be called when you can write to a custom fd. More... | |
bool | keepalive |
True if we are keeping the connection alive after it has finished. 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... | |
virtual void | connect () |
(Re)connect More... | |
ws_state | get_state () |
Get websocket state. More... | |
Protected Attributes | |
bool | terminating |
True if the shard is terminating. More... | |
std::string | buffer |
Input buffer received from socket. More... | |
std::string | obuffer |
Output buffer for sending to socket. More... | |
bool | nonblocking |
True if in nonblocking mode. The socket switches to nonblocking mode once ReadLoop is called. 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... | |
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 | make_new |
True if we are establishing a new connection, false if otherwise. 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 |
|
virtual |
Destroy the discord client object.
discord_client& dpp::discord_client::change_voice_channel | ( | snowflake | guild_id, |
snowflake | channel_id, | ||
bool | self_mute = false , |
||
bool | self_deaf = false |
||
) |
Change 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 |
discord_client& dpp::discord_client::clear_queue | ( | ) |
Clear the outbound message queue.
|
virtualinherited |
Close websocket.
Reimplemented from dpp::ssl_client.
|
protectedvirtualinherited |
(Re)connect
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 |
||
) |
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 |
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. |
|
virtual |
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 the decompressed bytes in objectGet decompressed total bytes received.
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.
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 |
|
virtualinherited |
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 |
|
virtual |
Handle JSON from the websocket.
buffer | The entire buffer content from the websocket client |
Reimplemented from dpp::websocket_client.
bool dpp::discord_client::is_connected | ( | ) |
Returns true if the shard is connected.
|
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.
severity | The log level from dpp::loglevel |
msg | The log message to output |
Reimplemented from dpp::ssl_client.
|
virtual |
Fires every second from the underlying socket I/O loop, used for sending heartbeats
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 |
Nonblocking I/O loop.
std::exception | Any std::exception (or derivative) thrown from read_loop() causes reconnection of the shard |
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.
|
virtualinherited |
Write to websocket. Encapsulates data in frames if the status is CONNECTED.
data | The data to send. |
Reimplemented from dpp::ssl_client.
|
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.
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.
|
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.
|
inherited |
This event will be called when you can read from the custom 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).
|
inherited |
This event will be called when you can write to a custom fd.
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)
uint64_t dpp::discord_client::last_seq |
Last sequence number received, for resumes and pings.
|
protectedinherited |
For timers.
|
protectedinherited |
True if we are establishing a new connection, false if otherwise.
uint32_t dpp::discord_client::max_shards |
Total number of shards.
|
protectedinherited |
True if in nonblocking mode. The socket switches to nonblocking mode once ReadLoop is called.
|
protectedinherited |
Output buffer for sending to socket.
|
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.
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.
|
protected |
True if the shard is terminating.
std::thread::native_handle_type dpp::discord_client::thread_id |
Thread ID.
std::string dpp::discord_client::token |
Discord bot token.
std::shared_mutex dpp::discord_client::voice_mutex |
Mutex for voice connections map.
double dpp::discord_client::websocket_ping |
Websocket latency in fractional seconds.