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#include <map>
37
38namespace discord_core_api {
39
40 song_api::song_api(const snowflake guildIdNew) {
41 guildId = guildIdNew;
42 }
43
45 onSongCompletionEvent.functions.clear();
46 eventToken = onSongCompletionEvent.add(handler);
47 }
48
49 bool song_api::skip(bool wasItAFail) {
50 audio_frame_data dataFrame{};
51 audioDataBuffer.clearContents();
52 auto returnValue = discord_core_client::getVoiceConnection(guildId).skip(wasItAFail);
53 audioDataBuffer.send(std::move(dataFrame));
54 return returnValue;
55 }
56
57 jsonifier::vector<song> song_api::searchForSong(jsonifier::string_view searchQuery, uint64_t limit) {
58 std::unique_lock lock{ accessMutex };
59 jsonifier::vector<song> vector01{};
60 jsonifier::vector<song> vector02{};
61 if (searchQuery.find("soundcloud") == jsonifier::string::npos && searchQuery.find("youtube") == jsonifier::string::npos) {
62 vector01 = discord_core_client::getSoundCloudAPI(guildId).searchForSong(searchQuery, limit);
63 vector02 = discord_core_client::getYouTubeAPI(guildId).searchForSong(searchQuery, limit);
64 } else if (searchQuery.find("youtube") != jsonifier::string::npos) {
65 vector02 = discord_core_client::getYouTubeAPI(guildId).searchForSong(searchQuery, limit);
66 } else if (searchQuery.find("soundcloud") != jsonifier::string::npos) {
67 vector01 = discord_core_client::getSoundCloudAPI(guildId).searchForSong(searchQuery, limit);
68 }
69 uint64_t totalLength = vector01.size() + vector02.size();
70 jsonifier::vector<song> newVector{};
71 uint64_t vector01Used{};
72 uint64_t vector02Used{};
73 if (vector01.size() == 0 && vector02.size() != 0 || (vector01.size() >= 1 && vector02.size() >= 1)) {
74 return vector02;
75 } else if (vector02.size() == 0 && vector01.size() != 0 || (vector02.size() >= 1 && vector01.size() >= 1)) {
76 return vector01;
77 } else {
78 for (uint64_t x = 0; x < totalLength; ++x) {
79 if ((vector01Used < vector01.size()) && (x % 2 == 0) && vector01.size() > 0) {
80 newVector.emplace_back(vector01[vector01Used]);
81 newVector[newVector.size() - 1].type = song_type::SoundCloud;
82 ++vector01Used;
83 } else if (vector02Used < vector02.size() && vector02.size() > 0) {
84 newVector.emplace_back(vector02[vector02Used]);
85 newVector[newVector.size() - 1].type = song_type::YouTube;
86 ++vector02Used;
87 }
88 }
89 return newVector;
90 }
91 }
92
93 bool song_api::play(song songNew) {
94 std::unique_lock lock{ accessMutex };
95 audioDataBuffer.clearContents();
96 if (taskThread.getStatus() == co_routine_status::running) {
97 taskThread.cancelAndWait();
98 }
99 discord_core_client::getVoiceConnection(guildId).currentUserId = songNew.addedByUserId;
100 if (songNew.type == song_type::SoundCloud) {
101 song newerSong{ discord_core_client::getSoundCloudAPI(guildId).collectFinalSong(songNew) };
102 taskThread = discord_core_client::getSoundCloudAPI(guildId).downloadAndStreamAudio(newerSong);
103
104 } else if (songNew.type == song_type::YouTube) {
105 song newerSong{ discord_core_client::getYouTubeAPI(guildId).collectFinalSong(songNew) };
106 taskThread = discord_core_client::getYouTubeAPI(guildId).downloadAndStreamAudio(newerSong);
107 };
108 return discord_core_client::getVoiceConnection(guildId).play();
109 }
110
112 return discord_core_client::getVoiceConnection(guildId).areWeCurrentlyPlaying();
113 }
114
116 return discord_core_client::getVoiceConnection(guildId).play();
117 }
118
120 return discord_core_client::getVoiceConnection(guildId).pauseToggle();
121 }
122
124 bool returnValue = discord_core_client::getVoiceConnection(guildId).stop();
125 if (taskThread.getStatus() == co_routine_status::running) {
126 taskThread.cancelAndWait();
127 }
128 audioDataBuffer.clearContents();
129 return returnValue;
130 }
131
132 void song_api::disconnect() {
133 if (taskThread.getStatus() == co_routine_status::running) {
134 taskThread.cancelAndWait();
135 }
136 onSongCompletionEvent.erase(eventToken);
137 audioDataBuffer.clearContents();
138 stop_watch<milliseconds> stopWatch{ 10000ms };
139 stopWatch.reset();
140 while (discord_core_client::getSoundCloudAPI(guildId).areWeWorking() || discord_core_client::getYouTubeAPI(guildId).areWeWorking()) {
141 if (stopWatch.hasTimeElapsed()) {
142 break;
143 }
144 std::this_thread::sleep_for(1ms);
145 }
146 }
147};
A co_routine - representing a potentially asynchronous operation/function.
A class representing a snowflake identifier with various operations.
Definition Base.hpp:701
bool areWeCurrentlyPlaying() const
Checks if there is currently playing music for the current guild.
Definition SongAPI.cpp:111
bool skip(bool wasItAfail=false)
Skips to the next song in the queue, if applicable.
Definition SongAPI.cpp:49
bool pauseToggle()
Toggles pausing on and off.
Definition SongAPI.cpp:119
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:44
bool play()
Plays the current song. (assuming that you are currently connected to a voice_connection).
Definition SongAPI.cpp:115
jsonifier::vector< song > searchForSong(jsonifier::string_view searchQuery, uint64_t limit=20)
Search for a song to play.
Definition SongAPI.cpp:57
bool stop()
Stops the currently playing song.
Definition SongAPI.cpp:123
The main namespace for the forward-facing interfaces.