재생목록 추가기능 추가

This commit is contained in:
2024-01-02 00:40:40 +09:00
parent 5f08bc4c09
commit dd330fbf47
12 changed files with 117 additions and 92 deletions

View File

@@ -7,7 +7,7 @@
class IBot { class IBot {
private: private:
public: public:
IBot(std::string Token); IBot(std::string Token, int TotalShard);
void AddCommand(ICommand &Command); void AddCommand(ICommand &Command);
void Start(); void Start();

View File

@@ -8,10 +8,7 @@
class BumbleCeepp : public IBot { class BumbleCeepp : public IBot {
public: public:
static BumbleCeepp* GetInstance(std::string Token) { BumbleCeepp(std::string Token, int TotalShard);
static BumbleCeepp Instance(Token);
return &Instance;
}
void enqueue(struct FQueueElement Element); void enqueue(struct FQueueElement Element);
struct FQueueElement QueueDelete(int Index); struct FQueueElement QueueDelete(int Index);
@@ -20,13 +17,10 @@ public:
uint32_t VoiceJoinedShardId; uint32_t VoiceJoinedShardId;
bool Repeat; bool Repeat;
std::mutex YTDLMutex; std::mutex YTDLMutex;
std::list<struct FQueueElement> MusicQueue;
protected: protected:
private: private:
BumbleCeepp(std::string Token);
void OnCommand(const dpp::slashcommand_t& Event); void OnCommand(const dpp::slashcommand_t& Event);
std::list<struct FQueueElement> MusicQueue;
std::mutex QueueMutex; std::mutex QueueMutex;
bool QueuePlaying; bool QueuePlaying;
}; };

View File

@@ -4,12 +4,14 @@
#include <dpp/dpp.h> #include <dpp/dpp.h>
struct FQueueElement { struct FQueueElement {
std::string URL;
std::string title; std::string title;
std::string description; std::string description;
std::string FileName; std::string FileName;
std::string thumbnail; std::string thumbnail;
std::string duration; std::string duration;
dpp::snowflake guild_id; dpp::snowflake guild_id;
dpp::embed embed;
}; };
#endif #endif

View File

@@ -1,7 +1,7 @@
#include <Bot.hpp> #include <Bot.hpp>
IBot::IBot(std::string Token) { IBot::IBot(std::string Token, int TotalShard) {
BotCluster = std::make_shared<dpp::cluster>(Token); BotCluster = std::make_shared<dpp::cluster>(Token, dpp::i_default_intents, TotalShard);
BotCluster->on_log(dpp::utility::cout_logger()); BotCluster->on_log(dpp::utility::cout_logger());
BotCluster->on_slashcommand([this](const dpp::slashcommand_t& Event) {OnCommand(Event);}); BotCluster->on_slashcommand([this](const dpp::slashcommand_t& Event) {OnCommand(Event);});

View File

@@ -6,7 +6,7 @@
#include <FQueueElement.hpp> #include <FQueueElement.hpp>
#include <thread> #include <thread>
BumbleCeepp::BumbleCeepp(std::string Token) : IBot(Token) { BumbleCeepp::BumbleCeepp(std::string Token, int TotalShard) : IBot(Token, TotalShard) {
VoiceJoinedShardId = 0; VoiceJoinedShardId = 0;
} }
@@ -46,6 +46,7 @@ void BumbleCeepp::QueuePlay(){
dpp::voiceconn* v = JoinedShared->get_voice(Music.guild_id); dpp::voiceconn* v = JoinedShared->get_voice(Music.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) { if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
std::cout << "not in voicechat. quit musicplay";
return; return;
} }

View File

@@ -24,11 +24,7 @@ namespace Commands {
auto PopedElement = Bot->QueueDelete(atoi(Pos.c_str())); auto PopedElement = Bot->QueueDelete(atoi(Pos.c_str()));
dpp::embed embed = dpp::embed() dpp::embed embed = PopedElement
.set_title(PopedElement.title)
.set_description(PopedElement.description)
.set_color(dpp::colors::sti_blue)
.set_image(PopedElement.thumbnail)
.set_timestamp(time(0)); .set_timestamp(time(0));
dpp::message msg(Event.command.channel_id, "다음 항목을 큐에서 삭제했습니다!:"); dpp::message msg(Event.command.channel_id, "다음 항목을 큐에서 삭제했습니다!:");

View File

@@ -11,7 +11,7 @@ namespace Commands {
} }
void Leave::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) { void Leave::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
std::cout << "disconnecting.."; std::cout << "disconnecting..\n";
dpp::voiceconn* v = Event.from->get_voice(Event.command.guild_id); dpp::voiceconn* v = Event.from->get_voice(Event.command.guild_id);
@@ -20,6 +20,7 @@ namespace Commands {
} }
v->voiceclient->stop_audio(); v->voiceclient->stop_audio();
MusicQueue.clear();
Event.from->disconnect_voice(Event.command.guild_id); Event.from->disconnect_voice(Event.command.guild_id);
dpp::message msg(Event.command.channel_id, "음성 채팅방을 떠납니다!"); dpp::message msg(Event.command.channel_id, "음성 채팅방을 떠납니다!");

View File

@@ -2,6 +2,7 @@
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <dpp/nlohmann/json.hpp> #include <dpp/nlohmann/json.hpp>
#include <string> #include <string>
#include <filesystem>
#include <ctime> #include <ctime>
using json = nlohmann::json; using json = nlohmann::json;
@@ -38,54 +39,70 @@ namespace Commands {
Bot->YTDLMutex.lock(); Bot->YTDLMutex.lock();
std::cout << "다운로드 시작" << "\n"; std::cout << "다운로드 시작" << "\n";
system(("./yt-dlp -o temp -w --write-info-json -f 251 " + Query + " & wait").c_str()); std::system(("python3 yt-download.py \"" + Query + "\" & wait").c_str());
std::cout << "다운로드 완료" << "\n"; std::cout << "다운로드 완료" << "\n";
json document;
std::ifstream infofile("temp.info.json");
infofile >> document;
infofile.close();
system("rm -f temp.info.json");
system(("yes n 2>/dev/null | ffmpeg -hide_banner -i temp -c copy Music/" + std::string(to_string(document["id"])) + ".ogg").c_str());
system("rm -f temp");
Bot->YTDLMutex.unlock(); Bot->YTDLMutex.unlock();
FQueueElement Data = {std::string(document["title"]),
std::string(document["uploader"]),
std::string(document["id"]),
std::string(document["thumbnail"]),
to_string(document["duration"]),
Event.command.guild_id};
Bot->enqueue(Data);
std::cout << "queued\n";
dpp::message msg(Event.command.channel_id, "큐에 다음 곡을 추가했습니다:"); dpp::message msg(Event.command.channel_id, "큐에 다음 곡을 추가했습니다:");
time_t SongLength = int(document["duration"]); std::ifstream infofile, idfile;
char SongLengthStr[10]; json document;
tm t; std::string ID;
t.tm_sec = SongLength%60; std::queue<FQueueElement> RequestedMusic;
t.tm_min = SongLength/60; idfile.open("Temp/CurMusic");
t.tm_hour = SongLength/360; while (std::getline(idfile, ID)) {
strftime(SongLengthStr, sizeof(SongLengthStr), "%X", &t); std::cout << ID << "\n";
infofile.open("Music/" + ID + ".info.json");
infofile >> document;
infofile.close();
msg.add_embed(dpp::embed() time_t SongLength = int(document["duration"]);
.set_color(dpp::colors::sti_blue) char SongLengthStr[10];
.set_title(document["title"]) tm t;
.set_description(document["uploader"]) t.tm_sec = SongLength%60;
.set_url(Query) t.tm_min = SongLength/60;
.set_image(document["thumbnail"]) t.tm_hour = SongLength/360;
.add_field( strftime(SongLengthStr, sizeof(SongLengthStr), "%X", &t);
"길이",
SongLengthStr,
true
));
FQueueElement Data = {
std::string(document["webpage_url"]),
std::string(document["title"]),
std::string(document["uploader"]),
std::string(document["id"]),
std::string(document["thumbnail"]),
to_string(document["duration"]),
Event.command.guild_id,
dpp::embed()
.set_color(dpp::colors::sti_blue)
.set_title(Data.title)
.set_description(Data.description)
.set_url(Data.URL)
.set_image(Data.thumbnail)
.add_field(
"길이",
SongLengthStr,
true
)
};
Bot->enqueue(Data);
RequestedMusic.push(Data);
}
idfile.close();
//std::system("rm -f Temp/CurMusic");
std::cout << "queued\n";
msg.add_embed(RequestedMusic.front().embed);
RequestedMusic.pop();
Event.edit_original_response(msg); Event.edit_original_response(msg);
while (!RequestedMusic.empty()) {
dpp::message followMsg(Event.command.channel_id, "");
followMsg.add_embed(RequestedMusic.front().embed);
RequestedMusic.pop();
Bot->BotCluster->message_create(followMsg);
}
std::cout << "replied\n"; std::cout << "replied\n";
dpp::voiceconn* v = Event.from->get_voice(Event.command.guild_id); dpp::voiceconn* v = Event.from->get_voice(Event.command.guild_id);

View File

@@ -1,42 +1,31 @@
#include <Commands/Queue.hpp> #include <Commands/Queue.hpp>
#include <iostream> #include <iostream>
#include <sstream>
#include <cmath>
namespace Commands { namespace Commands {
Queue::Queue(std::shared_ptr<BumbleCeepp> Bot) { dpp::embed MakeEmbed(std::list<FQueueElement>::iterator& iter, std::list<FQueueElement>::iterator end, bool Repeat = false, int Index = 0) {
this->Bot = Bot; dpp::embed embed = dpp::embed()
.set_color(dpp::colors::sti_blue);
dpp::slashcommand Command = dpp::slashcommand("q", "노래 예약 큐 확인", Bot->BotCluster->me.id); if (iter == end) {
embed
CommandObjectVector.push_back(Command);
}
void Queue::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
if (MusicQueue.empty()) {
dpp::embed embed = dpp::embed()
.set_title("큐가 비었습니다!") .set_title("큐가 비었습니다!")
.set_color(dpp::colors::sti_blue)
.set_timestamp(time(0)); .set_timestamp(time(0));
if (Bot->Repeat) if (Repeat)
embed.add_field(":repeat:",""); embed.add_field(":repeat:","");
dpp::message msg(Event.command.channel_id, "현재 큐에 있는 항목:"); return embed;
msg.add_embed(embed);
Event.reply(msg);
return;
} }
dpp::embed embed = dpp::embed() std::ostringstream Number;
.set_title("큐 항목:") int Start = Index;
.set_color(dpp::colors::sti_blue)
.set_timestamp(time(0));
int i = 0; for (; (Index < Start + 5) && (iter != end); iter++, Index++) {
char number[30] = {0, }; Number.clear();
Number.str("");
for (auto iter = MusicQueue.begin(); iter != MusicQueue.end(); iter++) { if (!Index) {
if (!i) {
embed.add_field( embed.add_field(
"현재 재생 중", "현재 재생 중",
"", "",
@@ -44,9 +33,9 @@ namespace Commands {
); );
} }
else { else {
sprintf(number, "%d", i); Number << Index;
embed.add_field( embed.add_field(
number, Number.str(),
"", "",
true true
); );
@@ -61,16 +50,42 @@ namespace Commands {
"", "",
"" ""
); );
i++;
} }
if (Bot->Repeat) if (iter == end) {
embed.add_field(":repeat:",""); embed.set_timestamp(time(0));
if (Repeat)
embed.add_field(":repeat:","");
}
return embed;
}
Queue::Queue(std::shared_ptr<BumbleCeepp> Bot) {
this->Bot = Bot;
dpp::slashcommand Command = dpp::slashcommand("q", "노래 예약 큐 확인", Bot->BotCluster->me.id);
CommandObjectVector.push_back(Command);
}
void Queue::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
auto iter = MusicQueue.begin();
dpp::embed embed = MakeEmbed(iter, MusicQueue.end(), Bot->Repeat, 0);
dpp::message msg(Event.command.channel_id, "현재 큐에 있는 항목:"); dpp::message msg(Event.command.channel_id, "현재 큐에 있는 항목:");
msg.add_embed(embed); msg.add_embed(embed);
Event.reply(msg); Event.reply(msg, [this, Event](const dpp::confirmation_callback_t &_Event) {
auto _iter = Bot->MusicQueue.begin();
std::advance(_iter, 5);
for (int i = 0; i < ceil(Bot->MusicQueue.size() / 5.0) - 1; i++) {
dpp::embed FollowEmbed = MakeEmbed(_iter, Bot->MusicQueue.end(), Bot->Repeat, (i+1)*5);
dpp::message FollowMsg(Event.command.channel_id, "");
FollowMsg.add_embed(FollowEmbed);
Bot->BotCluster->message_create(FollowMsg);
}
});
} }
} }

View File

@@ -9,7 +9,7 @@ int main() {
std::ifstream configfile("config.json"); std::ifstream configfile("config.json");
configfile >> configdocument; configfile >> configdocument;
std::shared_ptr<BumbleCeepp> BumbleBee(BumbleCeepp::GetInstance(configdocument["token"])); std::shared_ptr<BumbleCeepp> BumbleBee = std::make_shared<BumbleCeepp>(configdocument["token"], 1);
Commands::Play Command1(BumbleBee); Commands::Play Command1(BumbleBee);
Commands::Repeat Command2(BumbleBee); Commands::Repeat Command2(BumbleBee);

BIN
temp

Binary file not shown.

File diff suppressed because one or more lines are too long