Implements a discord client. Each discord_client connects to one shard and derives from a websocket client.
More...
#include <discordclient.h>
|
virtual void | log (dpp::loglevel severity, const std::string &msg) const |
|
virtual void | handle_event (const std::string &event, json &j, const std::string &raw) |
|
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...
|
|
void | 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 () |
|
uint64_t | get_decompressed_bytes_in () |
|
virtual bool | handle_frame (const std::string &buffer) |
|
virtual void | error (uint32_t errorcode) |
|
void | run () |
| Start and monitor I/O loop. Note that this is a blocking call and is usually executed within a thread by whatever creates the object. More...
|
|
void | connect_voice (snowflake guild_id, snowflake channel_id, bool self_mute=false, bool self_deaf=false) |
| Connect to a voice channel. More...
|
|
void | 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...
|
|
uint64_t | get_bytes_out () |
|
uint64_t | get_bytes_in () |
|
std::string | get_cipher () |
|
void | read_loop () |
| Nonblocking I/O loop. More...
|
|
Implements a discord client. Each discord_client connects to one shard and derives from a websocket client.
◆ discord_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.
- Parameters
-
_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 |
◆ ~discord_client()
virtual dpp::discord_client::~discord_client |
( |
| ) |
|
|
virtual |
◆ clear_queue()
void dpp::discord_client::clear_queue |
( |
| ) |
|
Clear the outbound message queue.
◆ close()
virtual void dpp::websocket_client::close |
( |
| ) |
|
|
virtualinherited |
◆ connect()
virtual void dpp::websocket_client::connect |
( |
| ) |
|
|
protectedvirtualinherited |
◆ connect_voice()
void dpp::discord_client::connect_voice |
( |
snowflake |
guild_id, |
|
|
snowflake |
channel_id, |
|
|
bool |
self_mute = false , |
|
|
bool |
self_deaf = false |
|
) |
| |
Connect to a voice channel.
- Parameters
-
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 |
◆ disconnect_voice()
void dpp::discord_client::disconnect_voice |
( |
snowflake |
guild_id | ) |
|
Disconnect from the connected voice channel on a guild.
- Parameters
-
guild_id | The guild who's voice channel you wish to disconnect from |
◆ disconnect_voice_internal()
void dpp::discord_client::disconnect_voice_internal |
( |
snowflake |
guild_id, |
|
|
bool |
send_json = true |
|
) |
| |
|
protected |
Disconnect from the connected voice channel on a guild.
- Parameters
-
guild_id | The guild who's voice channel you wish to disconnect from |
send_json | True if we shold send a json message confirming we are leaving the VC Should be set to false if we already receive this message in an event. |
◆ error()
virtual void dpp::discord_client::error |
( |
uint32_t |
errorcode | ) |
|
|
virtual |
Handle a websocket error.
- Parameters
-
errorcode | The error returned from the websocket |
Reimplemented from dpp::websocket_client.
◆ get_bytes_in()
uint64_t dpp::ssl_client::get_bytes_in |
( |
| ) |
|
|
inherited |
◆ get_bytes_out()
uint64_t dpp::ssl_client::get_bytes_out |
( |
| ) |
|
|
inherited |
◆ get_channel_count()
uint64_t dpp::discord_client::get_channel_count |
( |
| ) |
|
Get the Channel Count for this shard.
- Returns
- uint64_t channel count
◆ get_cipher()
std::string dpp::ssl_client::get_cipher |
( |
| ) |
|
|
inherited |
◆ get_decompressed_bytes_in()
uint64_t dpp::discord_client::get_decompressed_bytes_in |
( |
| ) |
|
Get decompressed total bytes received
◆ get_guild_count()
uint64_t dpp::discord_client::get_guild_count |
( |
| ) |
|
Get the Guild Count for this shard.
- Returns
- uint64_t guild count
◆ get_member_count()
uint64_t dpp::discord_client::get_member_count |
( |
| ) |
|
Get the Member Count for this shard.
- Returns
- uint64_t member count
◆ get_queue_size()
size_t dpp::discord_client::get_queue_size |
( |
| ) |
|
Get the size of the outbound message queue.
- Returns
- The size of the queue
◆ get_state()
ws_state dpp::websocket_client::get_state |
( |
| ) |
|
|
protectedinherited |
Get websocket state
- Returns
- websocket state
◆ get_uptime()
Returns the connection time of the shard.
- Returns
- dpp::utility::uptime Detail of how long the shard has been connected for
◆ get_voice()
Get the dpp::voiceconn object for a specific guild on this shard.
- Parameters
-
guild_id | The guild ID to retrieve the voice connection for |
- Returns
- voiceconn* The voice connection for the guild, or nullptr if there is no voice connection to this guild.
◆ handle_buffer()
virtual bool dpp::websocket_client::handle_buffer |
( |
std::string & |
buffer | ) |
|
|
virtualinherited |
Processes incoming frames from the SSL socket input buffer.
- Parameters
-
buffer | The buffer contents. Can modify this value removing the head elements when processed. |
Reimplemented from dpp::ssl_client.
◆ handle_event()
virtual void dpp::discord_client::handle_event |
( |
const std::string & |
event, |
|
|
json & |
j, |
|
|
const std::string & |
raw |
|
) |
| |
|
virtual |
Handle an event (opcode 0)
- Parameters
-
event | Event name, e.g. MESSAGE_CREATE |
j | JSON object for the event content |
raw | Raw JSON event string |
◆ handle_frame()
virtual bool dpp::discord_client::handle_frame |
( |
const std::string & |
buffer | ) |
|
|
virtual |
Handle JSON from the websocket.
- Parameters
-
buffer | The entire buffer content from the websocket client |
- Returns
- True if a frame has been handled
Reimplemented from dpp::websocket_client.
◆ is_connected()
bool dpp::discord_client::is_connected |
( |
| ) |
|
Returns true if the shard is connected.
- Returns
- True if connected
◆ log()
virtual void dpp::discord_client::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
-
severity | The log level from dpp::loglevel |
msg | The log message to output |
Reimplemented from dpp::ssl_client.
◆ one_second_timer()
virtual void dpp::discord_client::one_second_timer |
( |
| ) |
|
|
virtual |
Fires every second from the underlying socket I/O loop, used for sending heartbeats
Reimplemented from dpp::websocket_client.
◆ queue_message()
void dpp::discord_client::queue_message |
( |
const std::string & |
j, |
|
|
bool |
to_front = false |
|
) |
| |
Queue a message to be sent via the websocket.
- Parameters
-
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) |
◆ read_loop()
void dpp::ssl_client::read_loop |
( |
| ) |
|
|
inherited |
Nonblocking I/O loop.
- Exceptions
-
std::exception | Any std::exception (or derivative) thrown from read_loop() causes reconnection of the shard |
◆ run()
void dpp::discord_client::run |
( |
| ) |
|
Start and monitor I/O loop. Note that this is a blocking call and is usually executed within a thread by whatever creates the object.
◆ write()
virtual void dpp::websocket_client::write |
( |
const std::string & |
data | ) |
|
|
virtualinherited |
Write to websocket. Encapsulates data in frames if the status is CONNECTED.
- Parameters
-
Reimplemented from dpp::ssl_client.
◆ dpp::cluster
◆ dpp::events::guild_create
Needed so that guild_create can request member chunks if you have the correct intents.
◆ dpp::events::voice_state_update
◆ buffer
std::string dpp::ssl_client::buffer |
|
protectedinherited |
Input buffer received from openssl
◆ bytes_in
uint64_t dpp::ssl_client::bytes_in |
|
protectedinherited |
◆ bytes_out
uint64_t dpp::ssl_client::bytes_out |
|
protectedinherited |
◆ cipher
std::string dpp::ssl_client::cipher |
|
protectedinherited |
◆ connecting_voice_channels
List of voice channels we are connecting to keyed by guild id
◆ creator
◆ custom_readable_fd
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
This event will be called when you can read from the custom fd.
◆ custom_writeable_fd
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
This event will be called when you can write to a custom fd.
◆ heartbeat_interval
uint32_t dpp::discord_client::heartbeat_interval |
Heartbeat interval for sending heartbeat keepalive
◆ hostname
std::string dpp::ssl_client::hostname |
|
protectedinherited |
◆ intents
uint32_t dpp::discord_client::intents |
Privileged gateway intents
◆ last_heartbeat
time_t dpp::discord_client::last_heartbeat |
◆ last_heartbeat_ack
time_t dpp::discord_client::last_heartbeat_ack |
Last heartbeat ACK (opcode 11)
◆ last_seq
uint64_t dpp::discord_client::last_seq |
Last sequence number received, for resumes and pings
◆ last_tick
time_t dpp::ssl_client::last_tick |
|
protectedinherited |
◆ max_shards
uint32_t dpp::discord_client::max_shards |
◆ nonblocking
bool dpp::ssl_client::nonblocking |
|
protectedinherited |
True if in nonblocking mode. The socket switches to nonblocking mode once ReadLoop is called.
◆ obuffer
std::string dpp::ssl_client::obuffer |
|
protectedinherited |
Output buffer for sending to openssl
◆ plaintext
bool dpp::ssl_client::plaintext |
|
protectedinherited |
◆ port
std::string dpp::ssl_client::port |
|
protectedinherited |
◆ protocol
Current websocket protocol, currently either ETF or JSON
◆ ready
bool dpp::discord_client::ready |
True if READY or RESUMED has been received
◆ reconnects
uint32_t dpp::discord_client::reconnects |
◆ resumes
uint32_t dpp::discord_client::resumes |
◆ sessionid
std::string dpp::discord_client::sessionid |
◆ sfd
Raw file descriptor of connection
◆ shard_id
uint32_t dpp::discord_client::shard_id |
◆ ssl
opensslcontext* dpp::ssl_client::ssl |
|
protectedinherited |
◆ terminating
bool dpp::discord_client::terminating |
|
protected |
True if the shard is terminating.
◆ thread_id
std::thread::native_handle_type dpp::discord_client::thread_id |
◆ token
std::string dpp::discord_client::token |
◆ voice_mutex
std::mutex dpp::discord_client::voice_mutex |
Mutex for voice connections map
◆ websocket_ping
double dpp::discord_client::websocket_ping |
Websocket latency in fractional seconds