스킵 구현

This commit is contained in:
2023-12-20 19:45:25 +09:00
parent 6503fd167b
commit 8a987320e0
775 changed files with 162601 additions and 135 deletions

View File

@@ -1,8 +1,10 @@
#include "BumbleCeepp.hpp"
#include <string>
#include <ogg/ogg.h>
#include <oggz/oggz.h>
#include <opus/opusfile.h>
#include <FQueueElement.hpp>
#include <thread>
BumbleCeepp::BumbleCeepp(std::string Token) : IBot(Token) {
VoiceJoinedShardId = 0;
@@ -15,137 +17,96 @@ void BumbleCeepp::enqueue(struct FQueueElement Element) {
}
void BumbleCeepp::QueuePlay(){
dpp::discord_client* JoinedShared = BotCluster->get_shard(VoiceJoinedShardId);
if (!JoinedShared) {
if (QueuePlaying) {
return;
}
QueuePlaying = true;
QueuePlayMutex.lock();
while (!MusicQueue.empty()) {
QueueMutex.lock();
FQueueElement Music = MusicQueue.front();
MusicQueue.pop_front();
QueueMutex.unlock();
dpp::voiceconn* v = JoinedShared->get_voice(Music.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
std::thread T1([this] (){
dpp::discord_client* JoinedShared = BotCluster->get_shard(VoiceJoinedShardId);
if (!JoinedShared) {
return;
}
ogg_sync_state oy;
ogg_stream_state os;
ogg_page og;
ogg_packet op;
OpusHead header;
char *buffer;
while (!MusicQueue.empty()) {
QueueMutex.lock();
FQueueElement Music = MusicQueue.front();
QueueMutex.unlock();
FILE *fd;
fd = fopen((std::string(Music.FileName.c_str()) + ".ogg").c_str(), "rb");
if (!fd) {
continue;
}
fseek(fd, 0L, SEEK_END);
size_t sz = ftell(fd);
rewind(fd);
ogg_sync_init(&oy);
buffer = ogg_sync_buffer(&oy, sz);
fread(buffer, 1, sz, fd);
ogg_sync_wrote(&oy, sz);
if (ogg_sync_pageout(&oy, &og) != 1) {
fprintf(stderr,"Does not appear to be ogg stream.\n");
exit(1);
}
ogg_stream_init(&os, ogg_page_serialno(&og));
if (ogg_stream_pagein(&os,&og) < 0) {
fprintf(stderr,"Error reading initial page of ogg stream.\n");
exit(1);
}
if (ogg_stream_packetout(&os,&op) != 1) {
fprintf(stderr,"Error reading header packet of ogg stream.\n");
exit(1);
}
/* We must ensure that the ogg stream actually contains opus data */
if (!(op.bytes > 8 && !memcmp("OpusHead", op.packet, 8))) {
fprintf(stderr,"Not an ogg opus stream.\n");
exit(1);
}
/* Parse the header to get stream info */
int err = opus_head_parse(&header, op.packet, op.bytes);
if (err) {
fprintf(stderr,"Not a ogg opus stream\n");
exit(1);
}
/* Now we ensure the encoding is correct for Discord */
if (header.channel_count != 2 && header.input_sample_rate != 48000) {
fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n");
exit(1);
}
std::list<ogg_packet> OpusList;
/* Now loop though all the pages and send the packets to the vc */
while (ogg_sync_pageout(&oy, &og) == 1) {
ogg_stream_init(&os, ogg_page_serialno(&og));
if(ogg_stream_pagein(&os,&og)<0) {
fprintf(stderr,"Error reading page of Ogg bitstream data.\n");
exit(1);
dpp::voiceconn* v = JoinedShared->get_voice(Music.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
return;
}
while (ogg_stream_packetout(&os,&op) != 0) {
/* load the audio file with oggz */
OGGZ *track_og = oggz_open((std::string(Music.FileName.c_str()) + ".ogg").c_str(), OGGZ_READ);
/* Read remaining headers */
if (op.bytes > 8 && !memcmp("OpusHead", op.packet, 8)) {
int err = opus_head_parse(&header, op.packet, op.bytes);
if (err) {
fprintf(stderr,"Not a ogg opus stream\n");
exit(1);
}
/* If there was an issue reading the file, tell the user and stop */
if (!track_og) {
fprintf(stderr, "Error opening file\n");
return;
}
if (header.channel_count != 2 && header.input_sample_rate != 48000) {
fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n");
exit(1);
}
/* set read callback, this callback will be called on packets with the serialno,
* -1 means every packet will be handled with this callback.
*/
oggz_set_read_callback(
track_og, -1,
[](OGGZ *oggz, oggz_packet *packet, long serialno, void *user_data) {
dpp::voiceconn *voiceconn = (dpp::voiceconn *)user_data;
continue;
/* send the audio */
voiceconn->voiceclient->send_audio_opus(packet->op.packet, packet->op.bytes);
/* make sure to always return 0 here, read more on oggz documentation */
return 0;
},
/* this will be the value of void *user_data */
(void *)v
);
// read loop
while (v && v->voiceclient && !v->voiceclient->terminating) {
/* you can tweak this to whatever. Here I use BUFSIZ, defined in
* stdio.h as 8192.
*/
static const constexpr long CHUNK_READ = BUFSIZ * 2;
const long read_bytes = oggz_read(track_og, CHUNK_READ);
/* break on eof */
if (!read_bytes) {
break;
}
}
/* Skip the opus tags */
if (op.bytes > 8 && !memcmp("OpusTags", op.packet, 8))
continue;
/* Don't forget to free the memory */
oggz_close(track_og);
ogg_packet D;
std::cout << "audio sending complete\n";
memcpy(&D, &op, sizeof(ogg_packet));
int n = 0;
OpusList.push_back(D);
//v->voiceclient->send_audio_opus(op.packet, op.bytes, samples / 48);
while(v->voiceclient->is_playing()) {}
QueueMutex.lock();
MusicQueue.pop_front();
QueueMutex.unlock();
if (Repeat) {
QueueMutex.lock();
MusicQueue.push_back(Music);
QueueMutex.unlock();
}
}
QueuePlayMutex.unlock();
QueuePlaying = false;
while (!OpusList.empty()) {
ogg_packet D = OpusList.front();
OpusList.pop_front();
v->voiceclient->send_audio_opus(D.packet, D.bytes);
}
/* Cleanup */
ogg_stream_clear(&os);
ogg_sync_clear(&oy);
system(("rm -f " + std::string(Music.FileName.c_str()) + ".ogg").c_str());
}
std::cout << "Queue ended\n";
});
T1.detach();
}
void BumbleCeepp::OnCommand(const dpp::slashcommand_t& Event) {

BIN
src/CBDepu2ufEY.ogg Normal file

Binary file not shown.

View File

@@ -1,7 +1,9 @@
// #include <Python.h>
#include <Commands/Play.hpp>
#include <dpp/dpp.h>
#include <dpp/nlohmann/json.hpp>
#include <stdlib.h>
#include <string>
#include <ctime>
using json = nlohmann::json;
@@ -36,6 +38,10 @@ void Play::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcomm
}
std::string Query = std::get<std::string>(Event.get_parameter("query"));
// Event.thinking(false, [](const dpp::confirmation_callback_t &Callback) {
// return dpp::confirmation();
// });
system(("./yt-dlp -o temp --write-info-json -f 251 " + Query).c_str());
@@ -44,7 +50,7 @@ void Play::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcomm
infofile >> document;
system("rm -f temp.info.json");
system(("ffmpeg -i temp -c copy " + std::string(to_string(document["id"])) + ".ogg").c_str());
system(("ffmpeg -y -i temp -c copy " + std::string(to_string(document["id"])) + ".ogg").c_str());
system("rm -f temp");
FQueueElement Data = {std::string(document["title"]),
@@ -55,9 +61,18 @@ void Play::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcomm
Event.command.guild_id};
Bot->enqueue(Data);
std::cout << "queued\n";
dpp::message msg(Event.command.channel_id, "큐에 다음 곡을 추가했습니다:");
time_t SongLength = int(document["duration"]);
char SongLengthStr[10];
tm t;
t.tm_sec = SongLength%60;
t.tm_min = SongLength/60;
t.tm_hour = SongLength/360;
strftime(SongLengthStr, sizeof(SongLengthStr), "%X", &t);
msg.add_embed(dpp::embed()
.set_color(dpp::colors::sti_blue)
.set_title(document["title"])
@@ -66,12 +81,14 @@ void Play::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcomm
.set_image(document["thumbnail"])
.add_field(
"길이",
to_string(document["duration"]),
SongLengthStr,
true
));
Event.reply(msg);
std::cout << "replied\n";
dpp::voiceconn* v = Event.from->get_voice(Event.command.guild_id);
Bot->VoiceJoinedShardId = Event.from->shard_id;

View File

@@ -13,19 +13,18 @@ Queue::Queue(std::shared_ptr<BumbleCeepp> Bot) {
void Queue::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
dpp::embed embed = dpp::embed()
.set_title("큐 항목:")
.set_color(dpp::colors::sti_blue)
.set_title("영상 제목&링크")
.set_url("https://dpp.dev/")
.set_description("영상 설명란")
.set_image("https://dpp.dev/DPP-Logo.png")
.set_footer(
dpp::embed_footer()
.set_text("업로더 이름")
.set_icon("https://dpp.dev/DPP-Logo.png")
)
.set_timestamp(time(0));
dpp::message msg(Event.command.channel_id, "some Text");
for (auto iter = MusicQueue.begin(); iter != MusicQueue.end(); iter++) {
embed.add_field(
iter->title,
iter->description
);
}
dpp::message msg(Event.command.channel_id, "현재 큐에 있는 항목:");
msg.add_embed(embed);
Event.reply(msg);

30
src/Commands/Repeat.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include <Commands/Repeat.hpp>
#include <dpp/dpp.h>
#include <dpp/nlohmann/json.hpp>
#include <string>
#include <ctime>
using json = nlohmann::json;
Repeat::Repeat(std::shared_ptr<BumbleCeepp> Bot) {
this->Bot = Bot;
dpp::slashcommand Command = dpp::slashcommand("repeat", "반복 켜기/끄기", Bot->BotCluster->me.id);
dpp::slashcommand Alias = dpp::slashcommand("r", "반복 켜기/끄기", Bot->BotCluster->me.id);
CommandObjectVector.push_back(Command);
CommandObjectVector.push_back(Alias);
}
void Repeat::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
if (Bot->Repeat) {
Event.reply("반복을 껐습니다.");
Bot->Repeat = false;
}
else {
Event.reply("반복을 켰습니다.");
Bot->Repeat = true;
}
return;
}

31
src/Commands/Skip.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include <Commands/Skip.hpp>
#include <dpp/dpp.h>
#include <dpp/nlohmann/json.hpp>
#include <string>
#include <ctime>
using json = nlohmann::json;
Skip::Skip(std::shared_ptr<BumbleCeepp> Bot) {
this->Bot = Bot;
dpp::slashcommand Command = dpp::slashcommand("skip", "현재곡 스킵", Bot->BotCluster->me.id);
dpp::slashcommand Alias = dpp::slashcommand("s", "현재곡 스킵", Bot->BotCluster->me.id);
CommandObjectVector.push_back(Command);
CommandObjectVector.push_back(Alias);
}
void Skip::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
dpp::voiceconn* v = Event.from->get_voice(Event.command.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
return;
}
v->voiceclient->stop_audio();
Event.reply("스킵했습니다!");
return;
}

View File

@@ -6,14 +6,20 @@ using json = nlohmann::json;
int main() {
json configdocument;
std::ifstream configfile("../config.json");
std::ifstream configfile("config.json");
configfile >> configdocument;
std::shared_ptr<BumbleCeepp> BumbleBee(BumbleCeepp::GetInstance(configdocument["token"]));
Play Command1(BumbleBee);
Repeat Command2(BumbleBee);
Queue Command3(BumbleBee);
Skip Command4(BumbleBee);
BumbleBee->AddCommand(Command1);
BumbleBee->AddCommand(Command2);
BumbleBee->AddCommand(Command3);
BumbleBee->AddCommand(Command4);
BumbleBee->Start();