Skip to content

Commit f877d90

Browse files
committed
DiscordCoreAPI Release v2.0.1
-Modified the connection behavior of the VoiceConnection class. -Added support for the Discord "custom bot status"es. -Modified the getCached(x) functions to retrieve from the HTTPS endpoints when the object is absent from the cache. -Replaced all of std::string with jsonifier::string, std::string_view with jsonifier::string_view. -Added a ToEntity interface for collecting data-structures from their Snowflake ids. -Modified the CMakeLists.txt files. -Also modified calls to stoull et al. -Removed a bunch of superfluous concepts. -Refactored the KeyHasher and KeyAccessor classes. -Modified the VoiceConnection class to better avoid unnecessary calls to getUserAsync. -Switched from using strings to using string_views in the Https client classes. -Fixed an issue with a moved-from object. -Updated the caching behaviors of the EventManager class.
2 parents d68e1d5 + b2bff17 commit f877d90

File tree

13 files changed

+143
-9347
lines changed

13 files changed

+143
-9347
lines changed

BuildTools/Classes/Packager/Vcpkg.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ function secondBuild(string $portFileContent): bool
257257
echo GREEN . "Copy back port files from /usr/local/share...\n" . WHITE;
258258
chdir(getenv('HOME') . '/discordcoreapi');
259259
system('cp -v -R /usr/local/share/vcpkg/ports/discordcoreapi/vcpkg.json ./Vcpkg/ports/discordcoreapi/vcpkg.json');
260-
system('cp -v -R /usr/local/share/vcpkg/versions/baseline.json ./Vcpkg/versions/baseline.json');
261260
system('cp -v -R /usr/local/share/vcpkg/versions/d-/discordcoreapi.json ./Vcpkg/versions/d-/discordcoreapi.json');
262261

263262
echo GREEN . "Commit and push changes to main branch\n" . WHITE;

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ endif()
3333
cmake_minimum_required(VERSION 3.20)
3434
if (WIN32)
3535
set(OS "windows")
36-
elseif(UNIX)
37-
set(OS "linux")
3836
elseif(APPLE)
3937
set(OS "osx")
38+
elseif(UNIX)
39+
set(OS "linux")
4040
endif()
4141
if (EXISTS "$ENV{VCPKG_INSTALLATION_ROOT}")
4242
set(VCPKG_INSTALLATION_ROOT_NEW "$ENV{VCPKG_INSTALLATION_ROOT}")

Include/discordcoreapi/SongAPI.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ namespace DiscordCoreAPI {
7070
/// @brief Search for a Song to play.
7171
/// @param searchQuery The Song to search for.
7272
/// @return A vector of Song objects representing the search results.
73-
jsonifier::vector<Song> searchForSong(jsonifier::string_view searchQuery);
73+
jsonifier::vector<Song> searchForSong(jsonifier::string_view searchQuery, int32_t limit = 20);
7474

7575
/// @brief Plays the current Song. (Assuming that you are currently connected to a VoiceConnection).
7676
/// @param songNew The song to play.

Include/discordcoreapi/SoundCloudAPI.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace DiscordCoreAPI {
5050

5151
Song constructDownloadInfo(const Song& songNew);
5252

53-
jsonifier::vector<Song> collectSearchResults(jsonifier::string_view string);
53+
jsonifier::vector<Song> collectSearchResults(jsonifier::string_view string, int32_t limit = 20);
5454

5555
virtual Song collectFinalSong(const Song& songNew);
5656

@@ -67,8 +67,8 @@ namespace DiscordCoreAPI {
6767

6868
CoRoutine<void, false> downloadAndStreamAudio(const Song songNew, NewThreadAwaiter<void, false> threadHandle = NewThreadAwaiter<void, false>{},
6969
uint64_t currentReconnectTries = 0);
70-
71-
jsonifier::vector<Song> searchForSong(jsonifier::string_view searchQuery);
70+
71+
jsonifier::vector<Song> searchForSong(jsonifier::string_view searchQuery, int32_t limit);
7272

7373
void weFailedToDownloadOrDecode(const Song& songNew);
7474

Include/discordcoreapi/Utilities/TCPConnection.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
#ifndef OPENSSL_NO_DEPRECATED
3737
#define OPENSSL_NO_DEPRECATED
3838
#endif
39-
4039
#include <openssl/err.h>
40+
4141
#include <openssl/ssl.h>
4242

4343
#ifdef _WIN32

Include/discordcoreapi/YouTubeAPI.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace DiscordCoreAPI {
4747

4848
Song constructDownloadInfo(const Song& songNew, uint64_t currentRecursionDepth);
4949

50-
jsonifier::vector<Song> collectSearchResults(jsonifier::string_view string);
50+
jsonifier::vector<Song> collectSearchResults(jsonifier::string_view string, int32_t limit = 20);
5151

5252
virtual Song collectFinalSong(const Song& songNew);
5353

@@ -63,7 +63,7 @@ namespace DiscordCoreAPI {
6363
CoRoutine<void, false> downloadAndStreamAudio(const Song songNew, NewThreadAwaiter<void, false> threadHandle = NewThreadAwaiter<void, false>{},
6464
uint64_t currentReconnectTries = 0);
6565

66-
jsonifier::vector<Song> searchForSong(jsonifier::string_view searchQuery);
66+
jsonifier::vector<Song> searchForSong(jsonifier::string_view searchQuery, int32_t limit);
6767

6868
void weFailedToDownloadOrDecode(const Song& songNew);
6969

@@ -129,6 +129,7 @@ namespace DiscordCoreAPI {
129129
};
130130

131131
struct VideoDetails {
132+
jsonifier::string shortDescription;
132133
VideoDetailsThumbnail thumbnail;
133134
jsonifier::string lengthSeconds;
134135
jsonifier::string videoId;
@@ -149,13 +150,6 @@ namespace DiscordCoreAPI {
149150
jsonifier::string videoId{};
150151
};
151152

152-
struct YouTubeSearchResult {
153-
jsonifier::string description{};
154-
jsonifier::string viewUrl{};
155-
jsonifier::string songId{};
156-
SongType type{};
157-
};
158-
159153
struct VideoRenderer {
160154
jsonifier::string videoId{};
161155
};

Library/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ set_target_properties(
7272
OUTPUT_NAME "discordcoreapi"
7373
)
7474

75-
if("${DEV}")
75+
if(DEV)
7676
include(FetchContent)
7777
FetchContent_Declare(
7878
Jsonifier

Source/DiscordCoreClient.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,20 @@ namespace DiscordCoreAPI {
157157
}
158158

159159
void DiscordCoreClient::runBot() {
160-
if (!instantiateWebSockets()) {
161-
doWeQuit.store(true, std::memory_order_release);
162-
return;
163-
}
164-
while (getBotUser().id == 0) {
165-
std::this_thread::sleep_for(1ms);
166-
}
167-
registerFunctionsInternal();
168-
while (!doWeQuit.load(std::memory_order_acquire)) {
169-
std::this_thread::sleep_for(1ms);
160+
try {
161+
if (!instantiateWebSockets()) {
162+
doWeQuit.store(true, std::memory_order_release);
163+
return;
164+
}
165+
while (getBotUser().id == 0) {
166+
std::this_thread::sleep_for(1ms);
167+
}
168+
registerFunctionsInternal();
169+
while (!doWeQuit.load(std::memory_order_acquire)) {
170+
std::this_thread::sleep_for(1ms);
171+
}
172+
} catch (const DCAException& error) {
173+
MessagePrinter::printError<PrintMessageType::General>(error.what());
170174
}
171175
}
172176

Source/SongAPI.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ namespace DiscordCoreAPI {
5454
return returnValue;
5555
}
5656

57-
jsonifier::vector<Song> SongAPI::searchForSong(jsonifier::string_view searchQuery) {
57+
jsonifier::vector<Song> SongAPI::searchForSong(jsonifier::string_view searchQuery, int32_t limit) {
5858
std::unique_lock lock{ accessMutex };
5959
jsonifier::vector<Song> vector01{};
6060
jsonifier::vector<Song> vector02{};
61-
if (searchQuery.find("soundcloud.com") == jsonifier::string::npos && searchQuery.find("youtube.com") == jsonifier::string::npos) {
62-
vector01 = DiscordCoreClient::getSoundCloudAPI(guildId).searchForSong(searchQuery);
63-
vector02 = DiscordCoreClient::getYouTubeAPI(guildId).searchForSong(searchQuery);
61+
if (searchQuery.find("soundcloud") == jsonifier::string::npos && searchQuery.find("youtube") == jsonifier::string::npos) {
62+
vector01 = DiscordCoreClient::getSoundCloudAPI(guildId).searchForSong(searchQuery, limit);
63+
vector02 = DiscordCoreClient::getYouTubeAPI(guildId).searchForSong(searchQuery, limit);
6464
} else if (searchQuery.find("youtube") != jsonifier::string::npos) {
65-
return DiscordCoreClient::getYouTubeAPI(guildId).collectSingleResult(searchQuery);
65+
vector02 = DiscordCoreClient::getYouTubeAPI(guildId).searchForSong(searchQuery, limit);
6666
} else if (searchQuery.find("soundcloud") != jsonifier::string::npos) {
67-
return DiscordCoreClient::getSoundCloudAPI(guildId).collectSingleResult(searchQuery);
67+
vector01 = DiscordCoreClient::getSoundCloudAPI(guildId).searchForSong(searchQuery, limit);
6868
}
6969
uint64_t totalLength = vector01.size() + vector02.size();
7070
jsonifier::vector<Song> newVector{};

Source/SoundCloudAPI.cpp

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,12 @@ namespace DiscordCoreAPI {
8686

8787
SoundCloudRequestBuilder::SoundCloudRequestBuilder(ConfigManager* configManagerNew) : HttpsClientCore{ jsonifier::string{ configManagerNew->getBotToken() } } {};
8888

89-
jsonifier::string collectSongIdFromSearchQuery(jsonifier::string_view string) {
89+
inline bool collectSongIdFromSearchQuery(jsonifier::string_view string, jsonifier::string& stringNew) {
9090
if (string.find("soundcloud.com") != jsonifier::string::npos) {
91-
jsonifier::string returnString{};
92-
returnString = string.substr(string.find("soundcloud.com/") + jsonifier::string{ "soundcloud.com/" }.size());
93-
return returnString;
91+
stringNew = string.substr(string.find("soundcloud.com/") + jsonifier::string{ "soundcloud.com/" }.size());
92+
return true;
9493
} else {
95-
return static_cast<jsonifier::string>(string);
94+
return false;
9695
}
9796
}
9897

@@ -104,7 +103,7 @@ namespace DiscordCoreAPI {
104103
HttpsWorkloadData dataPackage{ HttpsWorkloadType::SoundCloudGetSearchResults };
105104
dataPackage.baseUrl = "https://soundcloud.com/";
106105
dataPackage.headersToInsert["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36";
107-
dataPackage.relativePath = collectSongIdFromSearchQuery(songQuery);
106+
dataPackage.relativePath = songQuery;
108107
dataPackage.workloadClass = HttpsWorkloadClass::Get;
109108
HttpsResponseData returnData = submitWorkloadAndGetResult(std::move(dataPackage));
110109
Song results{};
@@ -172,26 +171,34 @@ namespace DiscordCoreAPI {
172171
}
173172
}
174173
}
174+
results.type = SongType::SoundCloud;
175175
return results;
176176
} catch (const HttpsError& error) {
177177
MessagePrinter::printError<PrintMessageType::Https>("SoundCloudRequestBuilder::collectSearchResults() Error: " + jsonifier::string{ error.what() });
178178
}
179179
return {};
180180
}
181181

182-
jsonifier::vector<Song> SoundCloudRequestBuilder::collectSearchResults(jsonifier::string_view songQuery) {
182+
jsonifier::vector<Song> SoundCloudRequestBuilder::collectSearchResults(jsonifier::string_view songQuery, int32_t limit) {
183183
if (clientId == "") {
184184
SoundCloudRequestBuilder::collectClientId();
185185
}
186186
try {
187+
jsonifier::string newString{};
188+
jsonifier::vector<Song> results{};
189+
if (collectSongIdFromSearchQuery(songQuery, newString)) {
190+
auto result = collectSingleResult(newString);
191+
if (result.songId != "") {
192+
results.emplace_back(result);
193+
}
194+
return results;
195+
}
187196
HttpsWorkloadData dataPackage{ HttpsWorkloadType::SoundCloudGetSearchResults };
188197
dataPackage.baseUrl = baseUrl02;
189198
dataPackage.headersToInsert["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36";
190-
dataPackage.relativePath =
191-
"/search?q=" + urlEncode(collectSongIdFromSearchQuery(songQuery).data()) + "&facet=model&client_id=" + SoundCloudRequestBuilder::clientId;
192-
dataPackage.workloadClass = HttpsWorkloadClass::Get;
193-
HttpsResponseData returnData = submitWorkloadAndGetResult(std::move(dataPackage));
194-
jsonifier::vector<Song> results{};
199+
dataPackage.relativePath = "/search?q=" + urlEncode(songQuery) + "&facet=model&client_id=" + SoundCloudRequestBuilder::clientId;
200+
dataPackage.workloadClass = HttpsWorkloadClass::Get;
201+
HttpsResponseData returnData = submitWorkloadAndGetResult(std::move(dataPackage));
195202
SoundCloudSearchResults resultsNew{};
196203
parser.parseJson<true, true>(resultsNew, returnData.responseData);
197204
for (auto& value: resultsNew.collection) {
@@ -208,7 +215,7 @@ namespace DiscordCoreAPI {
208215
}
209216
}
210217
if (isItFound) {
211-
jsonifier::string newString = value.title;
218+
newString = value.title;
212219
if (newString.size() > 0) {
213220
if (newString.size() > 256) {
214221
newString = newString.substr(0, 256);
@@ -227,14 +234,18 @@ namespace DiscordCoreAPI {
227234
if (newString.size() > 0) {
228235
songNew.thumbnailUrl = newString;
229236
}
237+
songNew.type = SongType::SoundCloud;
230238
songNew.viewUrl = value.viewUrl;
231239
songNew.duration = TimeStamp::convertMsToDurationString(value.duration);
232240
songNew.firstDownloadUrl += "?client_id=" + SoundCloudRequestBuilder::clientId + "&track_authorization=" + value.trackAuthorization;
233241
if (songNew.thumbnailUrl.find("-") != jsonifier::string::npos) {
234242
jsonifier::string newerString = songNew.thumbnailUrl.substr(0, songNew.thumbnailUrl.findLastOf("-") + 1);
235243
newerString += "t500x500.jpg";
236244
songNew.thumbnailUrl = newerString;
237-
results.emplace_back(songNew);
245+
}
246+
results.emplace_back(songNew);
247+
if (results.size() >= limit) {
248+
break;
238249
}
239250
}
240251
}
@@ -256,7 +267,7 @@ namespace DiscordCoreAPI {
256267
}
257268
dataPackage01.workloadClass = HttpsWorkloadClass::Get;
258269
dataPackage01.headersToInsert["Connection"] = "Keep-Alive";
259-
dataPackage01.headersToInsert["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36";
270+
dataPackage01.headersToInsert["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36";
260271
HttpsResponseData results = submitWorkloadAndGetResult(std::move(dataPackage01));
261272
SecondDownloadUrl downloadUrl{};
262273
Song newerSong{ songNew };
@@ -272,15 +283,15 @@ namespace DiscordCoreAPI {
272283
while (newString.find("#EXTINF:") != jsonifier::string::npos) {
273284
jsonifier::string newString01 = "#EXTINF:";
274285
jsonifier::string newString02 = newString.substr(newString.find("#EXTINF:") + newString01.size());
275-
auto commandFind = newString02.find(",");
286+
auto commandFind = newString02.find(",");
276287
jsonifier::string newString00 = newString02.substr(0, commandFind);
277288
jsonifier::string newString03 = newString02.substr(commandFind + 2, newString02.find("#EXTINF:") - (newString00.size() + 3));
278-
newString = newString02.substr(commandFind);
289+
newString = newString02.substr(commandFind);
279290
if (newString03.find("#EXT-X-ENDLIST") != jsonifier::string::npos) {
280291
newString03 = newString03.substr(0, newString03.find("#EXT-X-ENDLIST"));
281292
}
282293
jsonifier::string newString04 = newString03.substr(newString03.findFirstOf("1234567890"));
283-
uint64_t firstNumber01 = std::stoull(newString04.substr(0, newString04.find("/")).data());
294+
uint64_t firstNumber01 = std::stoull(newString04.substr(0, newString04.find("/")).data());
284295
jsonifier::string newString05 = newString04.substr(newString04.find("/") + 1);
285296
uint64_t secondNumber = std::stoull(newString05.substr(0, newString05.find("/")).data());
286297
DownloadUrl downloadUrlNew{};
@@ -301,7 +312,7 @@ namespace DiscordCoreAPI {
301312
dataPackage02.baseUrl = newerSong.secondDownloadUrl;
302313
dataPackage02.workloadClass = HttpsWorkloadClass::Get;
303314
dataPackage02.headersToInsert["Connection"] = "Keep-Alive";
304-
dataPackage02.headersToInsert["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36";
315+
dataPackage02.headersToInsert["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36";
305316
auto headersNew = submitWorkloadAndGetResult(std::move(dataPackage02));
306317
uint64_t valueBitRate{};
307318
uint64_t valueLength{};
@@ -333,20 +344,20 @@ namespace DiscordCoreAPI {
333344
HttpsWorkloadData dataPackage02{ HttpsWorkloadType::SoundCloudGetClientId };
334345
dataPackage02.baseUrl = baseUrl;
335346
dataPackage02.relativePath = "/search?q=testValue";
336-
dataPackage02.headersToInsert["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36";
347+
dataPackage02.headersToInsert["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36";
337348
dataPackage02.workloadClass = HttpsWorkloadClass::Get;
338349
HttpsResponseData returnData = submitWorkloadAndGetResult(std::move(dataPackage02));
339350
jsonifier::vector<jsonifier::string> assetPaths{};
340351
jsonifier::string newString01 = "crossorigin src=";
341352
jsonifier::string newerString{};
342353
newerString = returnData.responseData;
343354
if (newerString.find(newString01) != jsonifier::string::npos) {
344-
jsonifier::string newString = newerString.substr(newerString.find(newString01) + newString01.size());
355+
jsonifier::string newString = newerString.substr(newerString.find(newString01) + newString01.size());
345356
jsonifier::string newString02 = newString.substr(1, newString.find(".js") + 2);
346357
assetPaths.emplace_back(newString02);
347358
while (newString.find("crossorigin src=") != jsonifier::string::npos) {
348359
jsonifier::string newString03 = newString.substr(1, newString.find(".js") + 2);
349-
newString = newString.substr(newString.find("crossorigin src=") + newString01.size());
360+
newString = newString.substr(newString.find("crossorigin src=") + newString01.size());
350361
assetPaths.emplace_back(newString03);
351362
}
352363
HttpsWorkloadData dataPackage03{ HttpsWorkloadType::SoundCloudGetClientId };
@@ -356,7 +367,8 @@ namespace DiscordCoreAPI {
356367
jsonifier::string newerString02{};
357368
newerString02.insert(newerString02.begin(), returnData02.responseData.begin(), returnData02.responseData.end());
358369

359-
jsonifier::string newString03 = newerString02.substr(newerString02.find("JSON.stringify({client_id:\"") + jsonifier::string_view{ "JSON.stringify({client_id:\"" }.size());
370+
jsonifier::string newString03 =
371+
newerString02.substr(newerString02.find("JSON.stringify({client_id:\"") + jsonifier::string_view{ "JSON.stringify({client_id:\"" }.size());
360372

361373
if (newString03.find("\",nonce:e.nonce}))))") != jsonifier::string::npos) {
362374
clientIdNew = newString03.substr(0, newString03.find("\",nonce:e.nonce}))))"));
@@ -402,8 +414,10 @@ namespace DiscordCoreAPI {
402414
for (uint64_t x = 0; x < songNew.finalDownloadUrls.size(); ++x) {
403415
HttpsWorkloadData dataPackage03{ HttpsWorkloadType::SoundCloudGetSearchResults };
404416
if (counter < songNew.finalDownloadUrls.size()) {
405-
jsonifier::string baseUrlNew = songNew.finalDownloadUrls.at(x).urlPath.substr(0, jsonifier::string_view{ "https://cf-hls-opus-media.sndcdn.com/media/" }.size());
406-
jsonifier::string relativeUrl = songNew.finalDownloadUrls.at(x).urlPath.substr(jsonifier::string_view{ "https://cf-hls-opus-media.sndcdn.com/media/" }.size());
417+
jsonifier::string baseUrlNew =
418+
songNew.finalDownloadUrls.at(x).urlPath.substr(0, jsonifier::string_view{ "https://cf-hls-opus-media.sndcdn.com/media/" }.size());
419+
jsonifier::string relativeUrl =
420+
songNew.finalDownloadUrls.at(x).urlPath.substr(jsonifier::string_view{ "https://cf-hls-opus-media.sndcdn.com/media/" }.size());
407421
dataPackage03.baseUrl = baseUrlNew;
408422
dataPackage03.relativePath = relativeUrl;
409423
}
@@ -466,8 +480,8 @@ namespace DiscordCoreAPI {
466480
return SoundCloudRequestBuilder::collectSingleResult(searchQuery);
467481
}
468482

469-
jsonifier::vector<Song> SoundCloudAPI::searchForSong(jsonifier::string_view searchQuery) {
470-
return collectSearchResults(searchQuery);
483+
jsonifier::vector<Song> SoundCloudAPI::searchForSong(jsonifier::string_view searchQuery, int32_t limit) {
484+
return collectSearchResults(searchQuery, limit);
471485
}
472486

473487
Song SoundCloudAPI::collectFinalSong(const Song& songNew) {

0 commit comments

Comments
 (0)