Skip to content
This repository has been archived by the owner on Dec 4, 2020. It is now read-only.

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacques authored May 21, 2020
1 parent 05124cf commit 76ba2d8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
54 changes: 48 additions & 6 deletions discordclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ void DiscordClient::messageReceived(const QString& message)
{
case Constants::Gateway::OpCode::DISPATCH:
{
if (payload["s"] != nullptr)
m_sequenceNumber = payload["s"];

QString eventName = payload["t"].get<std::string>().c_str();
if (eventName == "READY")
{
// Resume if needed
m_loggedIn = true;
m_sessionId = payload["d"]["session_id"].get<std::string>().c_str();
m_user.id = payload["d"]["user"]["id"].get<std::string>().c_str();
m_user.username = payload["d"]["user"]["username"].get<std::string>().c_str();
m_user.discriminator = payload["d"]["user"]["discriminator"].get<std::string>().c_str();
emit onReady(m_user);
}
else if (eventName == "MESSAGE_CREATE")
else if (eventName == "MESSAGE_CREATE" || eventName == "MESSAGE_UPDATE")
{
User author;
author.id = payload["d"]["author"]["id"].get<std::string>().c_str();
Expand All @@ -58,10 +63,8 @@ void DiscordClient::messageReceived(const QString& message)
}
break;
}
case Constants::Gateway::OpCode::HELLO:
case Constants::Gateway::OpCode::INVALID_SESSION:
{
// Send heartbeats
m_heartbeat->start(payload["d"]["heartbeat_interval"].get<int>());
// Identify
nlohmann::json identifyPayload;
identifyPayload["op"] = static_cast<int>(Constants::Gateway::OpCode::IDENTIFY);
Expand All @@ -72,7 +75,42 @@ void DiscordClient::messageReceived(const QString& message)
identifyPayload["d"]["intents"]
= static_cast<int>(Constants::Gateway::Intents::GUILD_MESSAGES)
| static_cast<int>(Constants::Gateway::Intents::DIRECT_MESSAGES);
m_websocket->sendTextMessage(identifyPayload.dump().c_str());
QTimer* timer = new QTimer;
connect(timer, &QTimer::timeout, this, [&]
{
m_websocket->sendTextMessage(identifyPayload.dump().c_str());
delete timer;
});
timer->start((rand() % 5 + 1) * 1000);
}
case Constants::Gateway::OpCode::HELLO:
{
// Send heartbeats
m_heartbeat->start(payload["d"]["heartbeat_interval"].get<int>());
if (m_loggedIn)
{
// Resume
nlohmann::json resumePayload;
resumePayload["op"] = static_cast<int>(Constants::Gateway::OpCode::RESUME);
resumePayload["d"]["token"] = m_token.toStdString();
resumePayload["d"]["session_id"] = m_sessionId.toStdString();
resumePayload["d"]["seq"] = m_sequenceNumber;
m_websocket->sendTextMessage(resumePayload.dump().c_str());
}
else
{
// Identify
nlohmann::json identifyPayload;
identifyPayload["op"] = static_cast<int>(Constants::Gateway::OpCode::IDENTIFY);
identifyPayload["d"]["token"] = m_token.toStdString();
identifyPayload["d"]["properties"]["$os"] = "Windows";
identifyPayload["d"]["properties"]["$device"] = "";
identifyPayload["d"]["properties"]["$browser"] = "Chrome";
identifyPayload["d"]["intents"]
= static_cast<int>(Constants::Gateway::Intents::GUILD_MESSAGES)
| static_cast<int>(Constants::Gateway::Intents::DIRECT_MESSAGES);
m_websocket->sendTextMessage(identifyPayload.dump().c_str());
}
break;
}
default:
Expand All @@ -82,7 +120,10 @@ void DiscordClient::messageReceived(const QString& message)

void DiscordClient::connectionClosed()
{
qDebug() << "Connection closed (" << (m_loggedIn ? m_user.username + "#" + m_user.discriminator + ", " + m_user.id : "not logged in") << ")";
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)
Expand All @@ -95,5 +136,6 @@ void DiscordClient::sendHeartbeat()
qDebug() << "Heartbeat sent";
nlohmann::json payload;
payload["op"] = static_cast<int>(Constants::Gateway::OpCode::HEARTBEAT);
payload["d"] = m_sequenceNumber;
m_websocket->sendTextMessage(payload.dump().c_str());
}
4 changes: 4 additions & 0 deletions discordclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class DiscordClient : public QObject
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);
Expand All @@ -46,6 +48,8 @@ class DiscordClient : public QObject

private:
bool m_loggedIn;
int m_sequenceNumber;
QString m_sessionId;
QString m_token;
User m_user;

Expand Down
12 changes: 9 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ int main(int argc, char *argv[])

a.connect(client, &DiscordClient::onMessage, &a, [&](const Message& message)
{
if (!message.content.contains("discord.gift/")) return;
if (!message.content.contains("discord.gift/") && !message.content.contains("discordapp.com/gifts/") && !message.content.contains("discord.com/gifts")) return;

a.connect(mgr, &QNetworkAccessManager::finished, &a, [&](QNetworkReply* reply)
{
a.disconnect(mgr, &QNetworkAccessManager::finished, &a, nullptr);
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QString replyText = reply->readAll();

// Actually I have no idea of how u can know if the code was valid, pls create an issue if you can help

if (nlohmann::json::parse(replyText.toStdString())["code"] != nullptr)
switch (static_cast<Constants::DiscordAPI::redeemResponseErrorCode>(nlohmann::json::parse(replyText.toStdString())["code"].get<int>()))
{
Expand All @@ -64,8 +67,11 @@ int main(int argc, char *argv[])
qDebug() << "Successfully redeemed";
});

qDebug() << "caca";
QRegExp reg("(?:discord.gift/\\S+)");
// For :
// discord.gift/code
// discord.com/gifts/code
// discordapp.com/gifts/code
QRegExp reg("(?:discord(.gift|.com/gifts|app.com/gifts)/\\S+)");
reg.indexIn(message.content);
QString code = reg.cap().split("/").back();
QString url = "https://discordapp.com/api/v6/entitlements/gift-codes/"+code+"/redeem";
Expand Down

0 comments on commit 76ba2d8

Please sign in to comment.