DiscordCoreAPI
A Discord bot library written in C++, with custom asynchronous coroutines.
Loading...
Searching...
No Matches
SongAPI.cpp
Go to the documentation of this file.
1/*
2 MIT License
3
4 DiscordCoreAPI, A bot library for Discord, written in C++, and featuring explicit multithreading through the usage of custom, asynchronous C++ CoRoutines.
5
6 Copyright 2022, 2023 Chris M. (RealTimeChris)
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25*/
26/// SongAPI.cpp - Source file for the song api related stuff.
27/// Sep 17, 2021
28/// https://discordcoreapi.com
29/// \file SongAPI.cpp
30
36
37namespace discord_core_api {
38
39 song_api::song_api(const snowflake guildIdNew) {
40 guildId = guildIdNew;
41 }
42
44 onSongCompletionEvent.functions.clear();
45 eventToken = onSongCompletionEvent.add(handler);
46 }
47
48 bool song_api::skip(bool wasItAFail) {
49 audio_frame_data dataFrame{};
50 audioDataBuffer.clearContents();
51 auto returnValue = discord_core_client::getVoiceConnection(guildId).skip(wasItAFail);
52 audioDataBuffer.send(std::move(dataFrame));
53 return returnValue;
54 }
55
56 jsonifier::vector<song> song_api::searchForSong(jsonifier::string_view searchQuery, uint64_t limit) {
57 std::unique_lock lock{ accessMutex };
58 jsonifier::vector<song> vector01{};
59 jsonifier::vector<song> vector02{};
60 if (searchQuery.find("soundcloud") == jsonifier::string::npos && searchQuery.find("youtube") == jsonifier::string::npos) {
61 vector01 = discord_core_client::getSoundCloudAPI(guildId).searchForSong(searchQuery, limit);
62 vector02 = discord_core_client::getYouTubeAPI(guildId).searchForSong(searchQuery, limit);
63 } else if (searchQuery.find("youtube") != jsonifier::string::npos) {
64 vector02 = discord_core_client::getYouTubeAPI(guildId).searchForSong(searchQuery, limit);
65 } else if (searchQuery.find("soundcloud") != jsonifier::string::npos) {
66 vector01 = discord_core_client::getSoundCloudAPI(guildId).searchForSong(searchQuery, limit);
67 }
68 uint64_t totalLength = vector01.size() + vector02.size();
69 jsonifier::vector<song> newVector{};
70 uint64_t vector01Used{};
71 uint64_t vector02Used{};
72 for (uint64_t x = 0; x < totalLength; ++x) {
73 if ((vector01Used < vector01.size()) && (x % 2 == 0) && vector01.size() > 0) {
74 newVector.emplace_back(vector01[vector01Used]);
75 newVector[newVector.size() - 1].type = song_type::SoundCloud;
76 ++vector01Used;
77 } else if (vector02Used < vector02.size() && vector02.size() > 0) {
78 newVector.emplace_back(vector02[vector02Used]);
79 newVector[newVector.size() - 1].type = song_type::YouTube;
80 ++vector02Used;
81 }
82 }
83 return newVector;
84 }
85
86 bool song_api::play(song songNew) {
87 std::unique_lock lock{ accessMutex };
88 if (taskThread.getStatus() == co_routine_status::running) {
89 taskThread.cancelAndWait();
90 }
91 discord_core_client::getVoiceConnection(guildId).currentUserId = songNew.addedByUserId;
92 if (songNew.type == song_type::SoundCloud) {
93 song newerSong{ discord_core_client::getSoundCloudAPI(guildId).collectFinalSong(songNew) };
94 taskThread = discord_core_client::getSoundCloudAPI(guildId).downloadAndStreamAudio(newerSong);
95
96 } else if (songNew.type == song_type::YouTube) {
97 song newerSong{ discord_core_client::getYouTubeAPI(guildId).collectFinalSong(songNew) };
98 taskThread = discord_core_client::getYouTubeAPI(guildId).downloadAndStreamAudio(newerSong);
99 };
100 return discord_core_client::getVoiceConnection(guildId).play();
101 }
102
104 return discord_core_client::getVoiceConnection(guildId).areWeCurrentlyPlaying();
105 }
106
108 return discord_core_client::getVoiceConnection(guildId).play();
109 }
110
112 return discord_core_client::getVoiceConnection(guildId).pauseToggle();
113 }
114
116 bool returnValue = discord_core_client::getVoiceConnection(guildId).stop();
117 if (taskThread.getStatus() == co_routine_status::running) {
118 taskThread.cancelAndWait();
119 }
120 audioDataBuffer.clearContents();
121 return returnValue;
122 }
123
124 void song_api::disconnect() {
125 if (taskThread.getStatus() == co_routine_status::running) {
126 taskThread.cancelAndWait();
127 }
128 onSongCompletionEvent.erase(eventToken);
129 audioDataBuffer.clearContents();
130 stop_watch<milliseconds> stopWatch{ 10000ms };
131 stopWatch.reset();
132 while (discord_core_client::getSoundCloudAPI(guildId).areWeWorking() || discord_core_client::getYouTubeAPI(guildId).areWeWorking()) {
133 if (stopWatch.hasTimeElapsed()) {
134 break;
135 }
136 std::this_thread::sleep_for(1ms);
137 }
138 }
139};
A co_routine - representing a potentially asynchronous operation/function.
Definition: CoRoutine.hpp:83
co_routine_status getStatus()
Collects the status of the co_routine.
Definition: CoRoutine.hpp:178
return_type cancelAndWait()
Cancels the currently running co_routine, while blocking to wait for it to complete.
Definition: CoRoutine.hpp:217
bool areWeCurrentlyPlaying() const
Checks if there is currently playing music for the current guild.
Definition: SongAPI.cpp:103
bool skip(bool wasItAfail=false)
Skips to the next song in the queue, if applicable.
Definition: SongAPI.cpp:48
bool pauseToggle()
Toggles pausing on and off.
Definition: SongAPI.cpp:111
void onSongCompletion(std::function< co_routine< void, false >(song_completion_event_data)> handler)
For setting up behavior in response to a completed song.
Definition: SongAPI.cpp:43
bool play()
Plays the current song. (assuming that you are currently connected to a voice_connection).
Definition: SongAPI.cpp:107
jsonifier::vector< song > searchForSong(jsonifier::string_view searchQuery, uint64_t limit=20)
Search for a song to play.
Definition: SongAPI.cpp:56
bool stop()
Stops the currently playing song.
Definition: SongAPI.cpp:115
The main namespace for the forward-facing interfaces.
Represents a single frame of audio data.
Definition: Utilities.hpp:381