This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
discordclient.h
71 lines (57 loc) · 1.63 KB
/
discordclient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef DISCORDCLIENT_H
#define DISCORDCLIENT_H
#include <QNetworkAccessManager>
#include <QObject>
#include <QTimer>
#include <QWebSocket>
#include "constants.h"
struct User
{
QString discriminator;
QString id;
QString username;
};
struct Message
{
User author;
QString channelId;
QString content;
};
class DiscordClient : public QObject
{
Q_OBJECT
public:
inline explicit DiscordClient(QNetworkAccessManager* networkManager)
: m_loggedIn(false),
m_networkManager(networkManager),
m_heartbeat(new QTimer),
m_websocket(new QWebSocket)
{
m_sequenceNumber = 0;
connect(m_websocket, &QWebSocket::disconnected, this, &DiscordClient::connectionClosed);
connect(m_websocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this, &DiscordClient::connectionError);
connect(m_websocket, &QWebSocket::textMessageReceived, this, &DiscordClient::messageReceived);
connect(m_heartbeat, &QTimer::timeout, this, &DiscordClient::sendHeartbeat);
};
void login(const QString& token);
inline bool loggedIn() { return m_loggedIn; }
inline const User user() { return m_user; }
private:
bool m_loggedIn;
int m_sequenceNumber;
QString m_sessionId;
QString m_token;
User m_user;
QNetworkAccessManager* m_networkManager;
QTimer* m_heartbeat;
QWebSocket* m_websocket;
signals:
void onMessage(const Message& message);
void onReady(const User& user);
private slots:
void connectionClosed();
void connectionError(QAbstractSocket::SocketError error);
void messageReceived(const QString& message);
void sendHeartbeat();
};
#endif // DISCORDCLIENT_H