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.cpp
152 lines (139 loc) · 5.48 KB
/
discordclient.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "constants.h"
#include "discordclient.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QWebSocket>
void DiscordClient::login(const QString& token)
{
m_token = token;
connect(m_networkManager, &QNetworkAccessManager::finished, this, [&](QNetworkReply* reply)
{
disconnect(m_networkManager, &QNetworkAccessManager::finished, this, nullptr);
QJsonDocument jsonReply = QJsonDocument::fromJson(reply->readAll());
m_websocket->open(jsonReply.object().find("url").value().toString());
});
QNetworkRequest gatewayRequest(Constants::DiscordAPI::gatewayURI());
gatewayRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
m_networkManager->get(gatewayRequest);
}
void DiscordClient::messageReceived(const QString& message)
{
QJsonObject payload = QJsonDocument::fromJson(message.toUtf8()).object();
// Switch on OP
switch (static_cast<Constants::Gateway::OpCode>(payload.find("op").value().toInt()))
{
case Constants::Gateway::OpCode::DISPATCH:
{
if (!payload.find("s").value().isNull())
m_sequenceNumber = payload.find("s").value().toInt();
QString eventName = payload.find("t").value().toString();
if (eventName == "READY")
{
// Resume if needed
m_loggedIn = true;
m_sessionId = payload.find("d").value().toObject().find("session_id").value().toString();
m_user.id = payload.find("d").value().toObject().find("user").value().toObject().find("id").value().toString();
m_user.username = payload.find("d").value().toObject().find("user").value().toObject().find("username").value().toString();
m_user.discriminator = payload.find("d").value().toObject().find("user").value().toObject().find("discriminator").value().toString();
emit onReady(m_user);
}
else if (eventName == "MESSAGE_CREATE")
{
User author;
author.id = payload.find("d").value().toObject().find("author").value().toObject().find("id").value().toString();
author.username = payload.find("d").value().toObject().find("author").value().toObject().find("username").value().toString();
author.discriminator = payload.find("d").value().toObject().find("author").value().toObject().find("discriminator").value().toString();
Message message;
message.author = author;
message.content = payload.find("d").value().toObject().find("content").value().toString();
message.channelId = payload.find("d").value().toObject().find("channel_id").value().toString();
emit onMessage(message);
}
break;
}
case Constants::Gateway::OpCode::INVALID_SESSION:
{
// Identify
QJsonDocument identifyPayload;
identifyPayload.object().insert("op", (static_cast<int>(Constants::Gateway::OpCode::IDENTIFY)));
QJsonObject data;
data.insert("intents",
static_cast<int>(Constants::Gateway::Intents::GUILD_MESSAGES)
| static_cast<int>(Constants::Gateway::Intents::DIRECT_MESSAGES));
data.insert("token", m_token);
QJsonObject properties;
properties.insert("$os", "Windows");
properties.insert("$device", "");
properties.insert("$browser", "Chrome");
data.insert("properties", properties);
identifyPayload.object().insert("d", data);
QTimer* timer = new QTimer;
connect(timer, &QTimer::timeout, this, [&]
{
m_websocket->sendTextMessage(identifyPayload.toJson(QJsonDocument::JsonFormat::Compact));
delete timer;
});
timer->start((rand() % 5 + 1) * 1000);
}
case Constants::Gateway::OpCode::HELLO:
{
// Send heartbeats
m_heartbeat->start(payload.find("d").value().toObject().find("heartbeat_interval").value().toInt());
if (m_loggedIn)
{
// Resume
QJsonDocument resumePayload;
resumePayload.object().insert("op", static_cast<int>(Constants::Gateway::OpCode::RESUME));
QJsonObject data;
data.insert("token", m_token);
data.insert("session_id", m_sessionId);
data.insert("seq", m_sequenceNumber);
resumePayload.object().insert("d", data);
m_websocket->sendTextMessage(resumePayload.toJson(QJsonDocument::JsonFormat::Compact));
}
else
{
// Identify
QJsonDocument identifyPayload;
identifyPayload.object().insert("op", (static_cast<int>(Constants::Gateway::OpCode::IDENTIFY)));
QJsonObject data;
data.insert("intents",
static_cast<int>(Constants::Gateway::Intents::GUILD_MESSAGES)
| static_cast<int>(Constants::Gateway::Intents::DIRECT_MESSAGES));
data.insert("token", m_token);
QJsonObject properties;
properties.insert("$os", "Windows");
properties.insert("$device", "");
properties.insert("$browser", "Chrome");
data.insert("properties", properties);
identifyPayload.object().insert("d", data);
m_websocket->sendTextMessage(identifyPayload.toJson(QJsonDocument::JsonFormat::Compact));
}
break;
}
default:
break;
}
}
void DiscordClient::connectionClosed()
{
qDebug() << "Connection closed (" << (m_loggedIn ? m_user.username + "#" + m_user.discriminator : "not logged in") << ")";
qDebug() << "Trying to reconnect...";
m_heartbeat->stop();
login(m_token);
}
void DiscordClient::connectionError(QAbstractSocket::SocketError error)
{
qDebug() << error;
}
void DiscordClient::sendHeartbeat()
{
QJsonDocument payload;
payload.object().insert("op", static_cast<int>(Constants::Gateway::OpCode::HEARTBEAT));
payload.object().insert("d", m_sequenceNumber);
m_websocket->sendTextMessage(payload.toJson(QJsonDocument::JsonFormat::Compact));
}