Implements a discord client. Each DiscordClient connects to one shard and derives from a websocket client.
More...
#include <discordclient.h>
|
virtual void | log (dpp::loglevel severity, const std::string &msg) |
|
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 | GetDeompressedBytesIn () |
|
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...
|
|
voiceconn * | GetVoice (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...
|
|
Implements a discord client. Each DiscordClient connects to one shard and derives from a websocket client.
◆ 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
-
_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 |
◆ ~DiscordClient()
virtual dpp::DiscordClient::~DiscordClient |
( |
| ) |
|
|
virtual |
◆ ClearQueue()
void dpp::DiscordClient::ClearQueue |
( |
| ) |
|
Clear the outbound message queue.
◆ close()
virtual void dpp::WSClient::close |
( |
| ) |
|
|
virtualinherited |
◆ Connect()
virtual void dpp::WSClient::Connect |
( |
| ) |
|
|
protectedvirtualinherited |
◆ ConnectVoice()
Connect to a voice channel.
- Parameters
-
guild_id | Guild where the voice channel is |
channel_id | Channel ID of the voice channel |
◆ DisconnectVoice()
void dpp::DiscordClient::DisconnectVoice |
( |
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 |
◆ Error()
virtual void dpp::DiscordClient::Error |
( |
uint32_t |
errorcode | ) |
|
|
virtual |
Handle a websocket error.
- Parameters
-
errorcode | The error returned from the websocket |
Reimplemented from dpp::WSClient.
◆ GetBytesIn()
uint64_t dpp::SSLClient::GetBytesIn |
( |
| ) |
|
|
inherited |
◆ GetBytesOut()
uint64_t dpp::SSLClient::GetBytesOut |
( |
| ) |
|
|
inherited |
◆ GetChannelCount()
uint64_t dpp::DiscordClient::GetChannelCount |
( |
| ) |
|
Get the Channel Count for this shard.
- Returns
- uint64_t channel count
◆ GetDeompressedBytesIn()
uint64_t dpp::DiscordClient::GetDeompressedBytesIn |
( |
| ) |
|
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()
◆ HandleBuffer()
virtual bool dpp::WSClient::HandleBuffer |
( |
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::SSLClient.
◆ HandleEvent()
virtual void dpp::DiscordClient::HandleEvent |
( |
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 |
◆ HandleFrame()
virtual bool dpp::DiscordClient::HandleFrame |
( |
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::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 |
|
) |
| |
|
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::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::SSLClient.
◆ QueueMessage()
void dpp::DiscordClient::QueueMessage |
( |
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) |
◆ ReadLoop()
void dpp::SSLClient::ReadLoop |
( |
| ) |
|
|
inherited |
◆ Run()
void dpp::DiscordClient::Run |
( |
| ) |
|
Start and monitor I/O loop
◆ 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
-
Reimplemented from dpp::SSLClient.
◆ buffer
std::string dpp::SSLClient::buffer |
|
protectedinherited |
Input buffer received from openssl
◆ bytes_in
uint64_t dpp::SSLClient::bytes_in |
|
protectedinherited |
◆ bytes_out
uint64_t dpp::SSLClient::bytes_out |
|
protectedinherited |
◆ cipher
std::string dpp::SSLClient::cipher |
|
protectedinherited |
◆ connecting_voice_channels
List of voice channels we are connecting to keyed by guild id
◆ creator
◆ ctx
SSL_CTX* dpp::SSLClient::ctx |
|
protectedinherited |
◆ 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 |
◆ intents
uint32_t dpp::DiscordClient::intents |
Privileged gateway intents
◆ last_heartbeat
time_t dpp::DiscordClient::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 |
◆ max_shards
uint32_t dpp::DiscordClient::max_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 |
◆ ready
bool dpp::DiscordClient::ready |
True if READY or RESUMED has been received
◆ reconnects
uint32_t dpp::DiscordClient::reconnects |
◆ resumes
uint32_t dpp::DiscordClient::resumes |
◆ sessionid
std::string dpp::DiscordClient::sessionid |
◆ sfd
Raw file descriptor of connection
◆ shard_id
uint32_t dpp::DiscordClient::shard_id |
◆ ssl
◆ thread_id
std::thread::native_handle_type dpp::DiscordClient::thread_id |
◆ token
std::string dpp::DiscordClient::token |
◆ voice_mutex
std::mutex dpp::DiscordClient::voice_mutex |
Mutex for voice connections map