-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlayQ.hpp
330 lines (305 loc) · 17.9 KB
/
PlayQ.hpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// play_q.hpp - header for the "play right now" command.
// sep 9, 2021
// chris m.
// https://github.com/real_time_chris
#pragma once
#include "HelperFunctions.hpp"
namespace discord_core_api {
class play_q : public base_function {
public:
static std::unordered_map<uint64_t, uint64_t> timeOfLastPlay;
play_q() {
this->commandName = "playq";
this->helpDescription = "Plays a specific song from the current queue.";
embed_data msgEmbed{};
msgEmbed.setDescription("------\nSimply enter /playq songnumber.\n------");
msgEmbed.setTitle("__**Play-Q Usage:**__");
msgEmbed.setTimeStamp(getTimeAndDate());
msgEmbed.setColor("fefefe");
this->helpEmbed = msgEmbed;
}
unique_ptr<base_function> create() {
return makeUnique<play_q>();
}
void execute(const base_function_arguments& argsNew) {
try {
channel_data channel{ argsNew.getChannelData() };
guild_data guild{ argsNew.getInteractionData().guildId };
discord_guild discordGuild{ guild };
bool areWeAllowed = checkIfAllowedPlayingInChannel(argsNew.getInputEventData(), discordGuild);
if (!areWeAllowed) {
return;
}
guild_member_data guildMember{ argsNew.getGuildMemberData() };
bool doWeHaveControl = checkIfWeHaveControl(argsNew.getInputEventData(), discordGuild, guildMember);
if (!doWeHaveControl) {
return;
}
input_event_data newEvent = argsNew.getInputEventData();
int64_t currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
int64_t previousPlayedTime{ 0 };
if (play_q::timeOfLastPlay.contains(argsNew.getInteractionData().guildId.operator const uint64_t&())) {
previousPlayedTime = static_cast<int64_t>(play_q::timeOfLastPlay.at(argsNew.getInteractionData().guildId.operator const uint64_t&()));
}
if (currentTime - previousPlayedTime < 5000) {
unique_ptr<embed_data> newEmbed{ makeUnique<embed_data>() };
newEmbed->setAuthor(argsNew.getUserData().userName, argsNew.getUserData().getUserImageUrl<user_image_types::Avatar>());
newEmbed->setDescription("------\n__**Sorry, but please wait a total of 5 seconds in between plays!**__\n------");
newEmbed->setTimeStamp(getTimeAndDate());
newEmbed->setTitle("__**Timing Issue:**__");
newEmbed->setColor("fefefe");
respond_to_input_event_data dataPackage{ newEvent };
dataPackage.addMessageEmbed(*newEmbed);
dataPackage.setResponseType(input_event_response_type::Ephemeral_Follow_Up_Message);
auto newerEvent = input_events::respondToInputEventAsync(dataPackage).get();
input_events::deleteInputEventResponseAsync(newerEvent, 20000);
return;
}
respond_to_input_event_data dataPackage(argsNew.getInputEventData());
dataPackage.setResponseType(input_event_response_type::Ephemeral_Deferred_Response);
newEvent = input_events::respondToInputEventAsync(dataPackage).get();
previousPlayedTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
play_q::timeOfLastPlay.insert_or_assign(argsNew.getInteractionData().guildId.operator const uint64_t&(), previousPlayedTime);
snowflake currentVoiceChannelId{};
if (guildMember.getVoiceStateData().channelId != 0) {
currentVoiceChannelId = guildMember.getVoiceStateData().channelId;
} else {
unique_ptr<embed_data> newEmbed{ makeUnique<embed_data>() };
newEmbed->setAuthor(argsNew.getUserData().userName, argsNew.getUserData().getUserImageUrl<user_image_types::Avatar>());
newEmbed->setDescription("------\n__**Sorry, but you need to be in a correct voice channel to issue those commands!**__\n------");
newEmbed->setTimeStamp(getTimeAndDate());
newEmbed->setTitle("__**Playing Issue:**__");
newEmbed->setColor("fefefe");
respond_to_input_event_data dataPackageNew{ newEvent };
dataPackageNew.addMessageEmbed(*newEmbed);
dataPackageNew.setResponseType(input_event_response_type::Ephemeral_Follow_Up_Message);
auto newerEvent = input_events::respondToInputEventAsync(dataPackageNew).get();
input_events::deleteInputEventResponseAsync(newerEvent, 20000);
return;
}
voice_connection& voiceConnection = guild.connectToVoice(guildMember.user.id);
if (!voiceConnection.areWeConnected()) {
unique_ptr<embed_data> newEmbed{ makeUnique<embed_data>() };
newEmbed->setAuthor(argsNew.getUserData().userName, argsNew.getUserData().getUserImageUrl<user_image_types::Avatar>());
newEmbed->setDescription("------\n__**Sorry, but there is no voice connection that is currently held by me!**__\n------");
newEmbed->setTimeStamp(getTimeAndDate());
newEmbed->setTitle("__**Connection Issue:**__");
newEmbed->setColor("fefefe");
respond_to_input_event_data dataPackageNew{ newEvent };
dataPackageNew.addMessageEmbed(*newEmbed);
dataPackageNew.setResponseType(input_event_response_type::Ephemeral_Follow_Up_Message);
auto newerEvent = input_events::respondToInputEventAsync(dataPackageNew).get();
input_events::deleteInputEventResponseAsync(newerEvent, 20000);
return;
}
if (guildMember.getVoiceStateData().channelId == 0 || guildMember.getVoiceStateData().channelId != voiceConnection.getChannelId()) {
unique_ptr<embed_data> newEmbed{ makeUnique<embed_data>() };
newEmbed->setAuthor(newEvent.getUserData().userName, newEvent.getUserData().getUserImageUrl<user_image_types::Avatar>());
newEmbed->setDescription("------\n__**Sorry, but you need to be in a correct voice channel to issue those commands!**__\n------");
newEmbed->setTimeStamp(getTimeAndDate());
newEmbed->setTitle("__**Playing Issue:**__");
newEmbed->setColor("fefefe");
respond_to_input_event_data dataPackageNew{ newEvent };
dataPackageNew.addMessageEmbed(*newEmbed);
dataPackageNew.setResponseType(input_event_response_type::Ephemeral_Follow_Up_Message);
auto newerEvent = input_events::respondToInputEventAsync(dataPackageNew).get();
input_events::deleteInputEventResponseAsync(newerEvent, 20000);
return;
}
if (!discordGuild.data.playlist.songQueue.size()) {
unique_ptr<embed_data> newEmbed{ makeUnique<embed_data>() };
newEmbed->setAuthor(newEvent.getUserData().userName, newEvent.getUserData().getUserImageUrl<user_image_types::Avatar>());
newEmbed->setDescription("------\n__**Sorry, but there's nothing to play!**__\n------");
newEmbed->setTimeStamp(getTimeAndDate());
newEmbed->setTitle("__**Playing Issue:**__");
newEmbed->setColor("fefefe");
respond_to_input_event_data dataPackageNew{ newEvent };
dataPackageNew.addMessageEmbed(*newEmbed);
dataPackageNew.setResponseType(input_event_response_type::Ephemeral_Follow_Up_Message);
auto newerEvent = input_events::respondToInputEventAsync(dataPackageNew).get();
input_events::deleteInputEventResponseAsync(newerEvent, 20000);
return;
}
uint64_t trackNumber{};
trackNumber = argsNew.getCommandArguments().values["tracknumber"].get<std::streamoff>() - 1;
if (trackNumber >= discordGuild.data.playlist.songQueue.size()) {
unique_ptr<embed_data> newEmbed{ makeUnique<embed_data>() };
newEmbed->setAuthor(newEvent.getUserData().userName, newEvent.getUserData().getUserImageUrl<user_image_types::Avatar>());
newEmbed->setDescription("------\n__**Sorry, but that number is out of the range of the current track list!**__\n------");
newEmbed->setTimeStamp(getTimeAndDate());
newEmbed->setTitle("__**Playing Issue:**__");
newEmbed->setColor("fefefe");
respond_to_input_event_data dataPackageNew{ newEvent };
dataPackageNew.addMessageEmbed(*newEmbed);
dataPackageNew.setResponseType(input_event_response_type::Ephemeral_Follow_Up_Message);
auto newerEvent = input_events::respondToInputEventAsync(dataPackageNew).get();
input_events::deleteInputEventResponseAsync(newerEvent, 20000);
return;
}
discordGuild.getDataFromDB();
playlist currentPlaylist = discordGuild.data.playlist;
song currentSong = discordGuild.data.playlist.currentSong;
song currentNew = currentPlaylist.songQueue[trackNumber];
currentPlaylist.songQueue.erase(currentPlaylist.songQueue.begin() + static_cast<int64_t>(trackNumber));
jsonifier::vector<song> newVector{};
playlist newPlaylist{};
newVector.emplace_back(currentNew);
newVector.emplace_back(currentSong);
for (auto& value : currentPlaylist.songQueue) {
newVector.emplace_back(value);
}
discordGuild.data.playlist.currentSong = {};
discordGuild.data.playlist.songQueue = newVector;
discordGuild.writeDataToDB();
auto channelId = argsNew.getInputEventData().getChannelData().id;
auto theTask = [=](song_completion_event_data eventData) mutable -> co_routine<void, false> {
auto argsNewer = std::move(argsNew);
co_await newThreadAwaitable<void, false>();
user_data userNew = users::getCachedUser({ eventData.guildMemberId });
std::this_thread::sleep_for(150ms);
discordGuild.getDataFromDB();
if (discordGuild.data.playlist.songQueue.size()) {
unique_ptr<embed_data> newEmbed{ makeUnique<embed_data>() };
if (!eventData.wasItAFail) {
discordGuild.getDataFromDB();
discordGuild.data.playlist.sendNextSong();
discord_core_client::getSongAPI(guild.id).play(discordGuild.data.playlist.currentSong);
discordGuild.writeDataToDB();
newEmbed->setAuthor(userNew.userName, userNew.getUserImageUrl<user_image_types::Avatar>());
newEmbed->setDescription("__**Title:**__ [" + discordGuild.data.playlist.currentSong.songTitle + "](" + discordGuild.data.playlist.currentSong.viewUrl +
")" + "\n__**Description:**__ " + discordGuild.data.playlist.currentSong.description + "\n__**Duration:**__ " +
discordGuild.data.playlist.currentSong.duration + "\n__**Added By:**__ <@!" +
discordGuild.data.playlist.currentSong.addedByUserId + ">");
newEmbed->setImage(discordGuild.data.playlist.currentSong.thumbnailUrl);
newEmbed->setTimeStamp(getTimeAndDate());
newEmbed->setTitle("__**Now Playing:**__");
newEmbed->setColor("fefefe");
if (discordGuild.data.playlist.isLoopAllEnabled && discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("✅ Loop-All, ✅ Loop-Song");
}
if (!discordGuild.data.playlist.isLoopAllEnabled && discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("❌ Loop-All, ✅ Loop-Song");
}
if (discordGuild.data.playlist.isLoopAllEnabled && !discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("✅ Loop-All, ❌ Loop-Song");
}
if (!discordGuild.data.playlist.isLoopAllEnabled && !discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("❌ Loop-All, ❌ Loop-Song");
}
create_message_data dataPackage02(channelId);
dataPackage02.addMessageEmbed(*newEmbed);
messages::createMessageAsync(dataPackage02).get();
} else {
discordGuild.getDataFromDB();
discordGuild.data.playlist.sendNextSong();
discord_core_client::getSongAPI(guild.id).play(discordGuild.data.playlist.currentSong);
discordGuild.writeDataToDB();
newEmbed->setAuthor(userNew.userName, userNew.getUserImageUrl<user_image_types::Avatar>());
newEmbed->setDescription("__**It appears as though there was an error when trying to play the previous track!**__");
newEmbed->setTimeStamp(getTimeAndDate());
newEmbed->setTitle("__**Playing Error:**__");
newEmbed->setColor("fe0000");
if (discordGuild.data.playlist.isLoopAllEnabled && discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("✅ Loop-All, ✅ Loop-Song");
}
if (!discordGuild.data.playlist.isLoopAllEnabled && discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("❌ Loop-All, ✅ Loop-Song");
}
if (discordGuild.data.playlist.isLoopAllEnabled && !discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("✅ Loop-All, ❌ Loop-Song");
}
if (!discordGuild.data.playlist.isLoopAllEnabled && !discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("❌ Loop-All, ❌ Loop-Song");
}
create_message_data dataPackage02(channelId);
dataPackage02.addMessageEmbed(*newEmbed);
messages::createMessageAsync(dataPackage02).get();
newEmbed->setAuthor(userNew.userName, userNew.getUserImageUrl<user_image_types::Avatar>());
newEmbed->setDescription("__**Title:**__ [" + discordGuild.data.playlist.currentSong.songTitle + "](" + discordGuild.data.playlist.currentSong.viewUrl +
")" + "\n__**Description:**__ " + discordGuild.data.playlist.currentSong.description + "\n__**Duration:**__ " +
discordGuild.data.playlist.currentSong.duration + "\n__**Added By:**__ <@!" +
discordGuild.data.playlist.currentSong.addedByUserId + ">");
newEmbed->setImage(discordGuild.data.playlist.currentSong.thumbnailUrl);
newEmbed->setTimeStamp(getTimeAndDate());
newEmbed->setTitle("__**Now Playing:**__");
newEmbed->setColor("fefefe");
if (discordGuild.data.playlist.isLoopAllEnabled && discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("✅ Loop-All, ✅ Loop-Song");
}
if (!discordGuild.data.playlist.isLoopAllEnabled && discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("❌ Loop-All, ✅ Loop-Song");
}
if (discordGuild.data.playlist.isLoopAllEnabled && !discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("✅ Loop-All, ❌ Loop-Song");
}
if (!discordGuild.data.playlist.isLoopAllEnabled && !discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("❌ Loop-All, ❌ Loop-Song");
}
create_message_data dataPackage03(channelId);
dataPackage03.addMessageEmbed(*newEmbed);
messages::createMessageAsync(dataPackage03).get();
}
discordGuild.writeDataToDB();
} else {
unique_ptr<embed_data> newEmbed{ makeUnique<embed_data>() };
newEmbed->setAuthor(userNew.userName, userNew.getUserImageUrl<user_image_types::Avatar>());
newEmbed->setDescription("------\n__**Sorry, but there's nothing left to play here!**__\n------");
newEmbed->setTimeStamp(getTimeAndDate());
newEmbed->setTitle("__**Now Playing:**__");
newEmbed->setColor("fefefe");
if (discordGuild.data.playlist.isLoopAllEnabled && discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("✅ Loop-All, ✅ Loop-Song");
} else if (!discordGuild.data.playlist.isLoopAllEnabled && discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("❌ Loop-All, ✅ Loop-Song");
} else if (discordGuild.data.playlist.isLoopAllEnabled && !discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("✅ Loop-All, ❌ Loop-Song");
} else if (!discordGuild.data.playlist.isLoopAllEnabled && !discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("❌ Loop-All, ❌ Loop-Song");
}
create_message_data dataPackage02(channelId);
dataPackage02.addMessageEmbed(*newEmbed);
messages::createMessageAsync(dataPackage02).get();
discord_core_client::getSongAPI(guild.id).stop();
}
co_return;
};
discord_core_client::getSongAPI(guild.id).onSongCompletion(theTask);
if (discordGuild.data.playlist.songQueue.size()) {
discord_core_client::getSongAPI(guild.id).stop();
discordGuild.getDataFromDB();
discordGuild.data.playlist.sendNextSong();
discord_core_client::getSongAPI(guild.id).play(discordGuild.data.playlist.currentSong);
discordGuild.writeDataToDB();
unique_ptr<embed_data> newEmbed{ makeUnique<embed_data>() };
newEmbed->setAuthor(argsNew.getUserData().userName, argsNew.getUserData().getUserImageUrl<user_image_types::Avatar>());
newEmbed->setDescription("__**Title:**__ [" + discordGuild.data.playlist.currentSong.songTitle + "](" + discordGuild.data.playlist.currentSong.viewUrl + ")" +
"\n__**Description:**__ " + discordGuild.data.playlist.currentSong.description + "\n__**Duration:**__ " +
discordGuild.data.playlist.currentSong.duration + "\n__**Added By:**__ <@!" + discordGuild.data.playlist.currentSong.addedByUserId +
">");
newEmbed->setImage(discordGuild.data.playlist.currentSong.thumbnailUrl);
newEmbed->setTimeStamp(getTimeAndDate());
newEmbed->setTitle("__**Now Playing:**__");
newEmbed->setColor("fefefe");
if (discordGuild.data.playlist.isLoopAllEnabled && discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("✅ Loop-All, ✅ Loop-Song");
} else if (!discordGuild.data.playlist.isLoopAllEnabled && discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("❌ Loop-All, ✅ Loop-Song");
} else if (discordGuild.data.playlist.isLoopAllEnabled && !discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("✅ Loop-All, ❌ Loop-Song");
} else if (!discordGuild.data.playlist.isLoopAllEnabled && !discordGuild.data.playlist.isLoopSongEnabled) {
newEmbed->setFooter("❌ Loop-All, ❌ Loop-Song");
}
respond_to_input_event_data dataPackageNew{ newEvent };
dataPackageNew.setResponseType(input_event_response_type::Edit_Interaction_Response);
dataPackageNew.addMessageEmbed(*newEmbed);
newEvent = input_events::respondToInputEventAsync(dataPackageNew).get();
}
return;
} catch (const std::runtime_error& error) {
std::cout << "play_q::execute()" << error.what() << std::endl;
}
};
~play_q(){};
};
std::unordered_map<uint64_t, uint64_t> play_q::timeOfLastPlay{};
}// namespace discord_core_api