From b7b101887104a6d42b50d0c49404fe9629a3d788 Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Mon, 22 Jan 2024 03:26:22 +0900 Subject: [PATCH] =?UTF-8?q?=EC=99=84=3F=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 -- include/Commands/CommandType.hpp | 11 ++++++ include/Commands/Delete.hpp | 4 +-- include/Commands/Leave.hpp | 4 +-- include/Commands/Play.hpp | 4 +-- include/Commands/Queue.hpp | 4 +-- include/Commands/Repeat.hpp | 4 +-- include/Commands/Skip.hpp | 4 +-- include/MusicQueue.hpp | 8 ++--- src/Commands/CommandType.cpp | 13 +++++++ src/Commands/Delete.cpp | 27 ++++++++------ src/Commands/Leave.cpp | 13 ++----- src/Commands/Play.cpp | 33 ++++++++--------- src/Commands/Queue.cpp | 27 +++++++------- src/Commands/Repeat.cpp | 13 ++----- src/Commands/Skip.cpp | 16 ++------- src/MusicQueue.cpp | 62 ++++++++++++++++---------------- src/main.cpp | 3 +- 18 files changed, 118 insertions(+), 134 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 67f107d..7142f9a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,8 +35,6 @@ target_include_directories(${BOT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${OPENSSL_INCLUDE_DIR} /usr/include/opus - /usr/include - /usr/local/include ) target_link_libraries(${BOT_NAME} diff --git a/include/Commands/CommandType.hpp b/include/Commands/CommandType.hpp index 2beb393..6ff0086 100644 --- a/include/Commands/CommandType.hpp +++ b/include/Commands/CommandType.hpp @@ -15,4 +15,15 @@ public: protected: std::shared_ptr botCluster; }; +} + +namespace commands { +class VCCommand : public ICommand { +public: + VCCommand(std::shared_ptr botCluster) : ICommand(botCluster) {} + + std::shared_ptr getQueue(const dpp::slashcommand_t& event); +protected: + std::unordered_map> *queueMap; +}; } \ No newline at end of file diff --git a/include/Commands/Delete.hpp b/include/Commands/Delete.hpp index 2bdd931..37ae4ac 100644 --- a/include/Commands/Delete.hpp +++ b/include/Commands/Delete.hpp @@ -4,12 +4,10 @@ #include namespace commands { -class Delete : public ICommand { +class Delete : public VCCommand { public: Delete(std::shared_ptr botCluster, std::unordered_map> *queueMap); void operator()(const dpp::slashcommand_t& event); -private: - std::unordered_map> *queueMap; }; } \ No newline at end of file diff --git a/include/Commands/Leave.hpp b/include/Commands/Leave.hpp index 194c806..084bcd3 100644 --- a/include/Commands/Leave.hpp +++ b/include/Commands/Leave.hpp @@ -4,12 +4,10 @@ #include namespace commands { -class Leave : public ICommand { +class Leave : public VCCommand { public: Leave(std::shared_ptr botCluster, std::unordered_map> *queueMap); void operator()(const dpp::slashcommand_t& event); -private: - std::unordered_map> *queueMap; }; } \ No newline at end of file diff --git a/include/Commands/Play.hpp b/include/Commands/Play.hpp index c95982e..bcd6a73 100644 --- a/include/Commands/Play.hpp +++ b/include/Commands/Play.hpp @@ -4,12 +4,10 @@ #include namespace commands { -class Play : public ICommand { +class Play : public VCCommand { public: Play(std::shared_ptr botCluster, std::unordered_map> *queueMap); void operator()(const dpp::slashcommand_t& event); -private: - std::unordered_map> *queueMap; }; } \ No newline at end of file diff --git a/include/Commands/Queue.hpp b/include/Commands/Queue.hpp index a78de32..ca302ee 100644 --- a/include/Commands/Queue.hpp +++ b/include/Commands/Queue.hpp @@ -4,12 +4,10 @@ #include namespace commands { -class Queue : public ICommand { +class Queue : public VCCommand { public: Queue(std::shared_ptr botCluster, std::unordered_map> *queueMap); void operator()(const dpp::slashcommand_t& event); -private: - std::unordered_map> *queueMap; }; } \ No newline at end of file diff --git a/include/Commands/Repeat.hpp b/include/Commands/Repeat.hpp index f684cbb..901d803 100644 --- a/include/Commands/Repeat.hpp +++ b/include/Commands/Repeat.hpp @@ -4,12 +4,10 @@ #include namespace commands { -class Repeat : public ICommand { +class Repeat : public VCCommand { public: Repeat(std::shared_ptr botCluster, std::unordered_map> *queueMap); void operator()(const dpp::slashcommand_t& event); -private: - std::unordered_map> *queueMap; }; } \ No newline at end of file diff --git a/include/Commands/Skip.hpp b/include/Commands/Skip.hpp index e741133..d4bdb25 100644 --- a/include/Commands/Skip.hpp +++ b/include/Commands/Skip.hpp @@ -4,12 +4,10 @@ #include namespace commands { -class Skip : public ICommand { +class Skip : public VCCommand { public: Skip(std::shared_ptr botCluster, std::unordered_map> *queueMap); void operator()(const dpp::slashcommand_t& event); -private: - std::unordered_map> *queueMap; }; } \ No newline at end of file diff --git a/include/MusicQueue.hpp b/include/MusicQueue.hpp index c559805..b1b2aa5 100644 --- a/include/MusicQueue.hpp +++ b/include/MusicQueue.hpp @@ -9,7 +9,7 @@ struct FMusicQueueID { class MusicQueue { public: - MusicQueue(FMusicQueueID id); + MusicQueue(FMusicQueueID id, std::shared_ptr botCluster); void operator+=(FQueueElement operand); FQueueElement pop(int index); FQueueElement peek(int index); @@ -19,9 +19,9 @@ public: std::list::iterator end(); std::size_t size(); FMusicQueueID getId(); - void play(std::shared_ptr botCluster); + void play(); - void markerCallback(std::shared_ptr botCluster); + void markerCallback(); bool repeat; private: @@ -29,5 +29,5 @@ private: std::mutex mutex; std::mutex playMutex; FMusicQueueID id; - bool queuePlaying; + std::shared_ptr botCluster; }; \ No newline at end of file diff --git a/src/Commands/CommandType.cpp b/src/Commands/CommandType.cpp index b7abb76..577a16a 100644 --- a/src/Commands/CommandType.cpp +++ b/src/Commands/CommandType.cpp @@ -4,3 +4,16 @@ commands::ICommand::ICommand(std::shared_ptr botCluster) { this->botCluster = botCluster; } + +std::shared_ptr commands::VCCommand::getQueue(const dpp::slashcommand_t& event) { + auto findResult = queueMap->find(event.command.guild_id); + if (findResult == queueMap->end()) + { + FMusicQueueID queueID; + queueID.guild_id = event.command.guild_id; + queueID.shard_id = event.from->shard_id; + + (*queueMap)[queueID.guild_id] = std::make_shared(queueID, botCluster); + } + return queueMap->find(event.command.guild_id)->second; +} \ No newline at end of file diff --git a/src/Commands/Delete.cpp b/src/Commands/Delete.cpp index 049e60b..bdb9fc3 100644 --- a/src/Commands/Delete.cpp +++ b/src/Commands/Delete.cpp @@ -2,7 +2,7 @@ #include commands::Delete::Delete(std::shared_ptr botCluster, std::unordered_map> *queueMap) - : ICommand(botCluster) + : VCCommand(botCluster) { this->queueMap = queueMap; dpp::slashcommand Command = dpp::slashcommand("d", "큐의 해당하는 번호의 노래를 지웁니다", botCluster->me.id); @@ -23,25 +23,29 @@ void commands::Delete::operator()(const dpp::slashcommand_t& event) std::string Pos = std::get(event.get_parameter("pos")); event.thinking(); - auto findResult = queueMap->find(event.command.guild_id); - if (findResult == queueMap->end()) - { - FMusicQueueID queueID; - queueID.guild_id = event.command.guild_id; - queueID.shard_id = event.from->shard_id; + std::shared_ptr queue = getQueue(event); - (*queueMap)[queueID.guild_id] = std::make_shared(queueID); + auto index = atoi(Pos.c_str()); + + int queueSize = queue->size(); + + std::cout << "queue size : " << queueSize << "\n"; + + if (index < 0 || (queueSize - 1) < index) { + std::cout << "invalid index : " << index << ", " + Pos + "\n"; + + event.edit_original_response(dpp::message(event.command.channel_id, "이상한 인덱스 위치. Pos : " + Pos)); + return; } - std::shared_ptr queue = queueMap->find(event.command.guild_id)->second; - auto PopedElement = queue->pop(atoi(Pos.c_str())); + auto PopedElement = queue->pop(index); dpp::embed embed = PopedElement.embed .set_timestamp(time(0)); dpp::message msg(event.command.channel_id, "다음 항목을 큐에서 삭제했습니다!:"); - if (!atoi(Pos.c_str())) { + if (atoi(Pos.c_str()) == 0) { dpp::voiceconn* v = event.from->get_voice(event.command.guild_id); if (!v || !v->voiceclient || !v->voiceclient->is_ready()) { @@ -49,6 +53,7 @@ void commands::Delete::operator()(const dpp::slashcommand_t& event) } v->voiceclient->stop_audio(); + v->voiceclient->insert_marker("end of music"); } msg.add_embed(embed); diff --git a/src/Commands/Leave.cpp b/src/Commands/Leave.cpp index 0f3e104..3814726 100644 --- a/src/Commands/Leave.cpp +++ b/src/Commands/Leave.cpp @@ -2,7 +2,7 @@ #include commands::Leave::Leave(std::shared_ptr botCluster, std::unordered_map> *queueMap) - : ICommand(botCluster) + : VCCommand(botCluster) { this->queueMap = queueMap; dpp::slashcommand command = dpp::slashcommand("l", "음챗을 떠납니다", botCluster->me.id); @@ -19,16 +19,7 @@ void commands::Leave::operator()(const dpp::slashcommand_t& event) } v->voiceclient->stop_audio(); - auto findResult = queueMap->find(event.command.guild_id); - if (findResult == queueMap->end()) - { - FMusicQueueID queueID; - queueID.guild_id = event.command.guild_id; - queueID.shard_id = event.from->shard_id; - - (*queueMap)[queueID.guild_id] = std::make_shared(queueID); - } - std::shared_ptr queue = queueMap->find(event.command.guild_id)->second; + std::shared_ptr queue = getQueue(event); queue->clear(); event.from->disconnect_voice(event.command.guild_id); diff --git a/src/Commands/Play.cpp b/src/Commands/Play.cpp index 993c511..07b1304 100644 --- a/src/Commands/Play.cpp +++ b/src/Commands/Play.cpp @@ -8,7 +8,7 @@ using json = nlohmann::json; commands::Play::Play(std::shared_ptr botCluster, std::unordered_map> *queueMap) - : ICommand(botCluster) + : VCCommand(botCluster) { this->queueMap = queueMap; dpp::slashcommand command = dpp::slashcommand("p", "노래 재생", botCluster->me.id); @@ -20,7 +20,8 @@ commands::Play::Play(std::shared_ptr botCluster, std::unordered_ma commandObjectVector.push_back(command); } -void commands::Play::operator()(const dpp::slashcommand_t& event) { +void commands::Play::operator()(const dpp::slashcommand_t& event) +{ if (std::holds_alternative(event.get_parameter("query"))) { event.reply("노래를 재생하려면 검색어 또는 링크를 입력해 주십시오."); @@ -28,25 +29,19 @@ void commands::Play::operator()(const dpp::slashcommand_t& event) { } /* Attempt to connect to a voice channel, returns false if we fail to connect. */ - if (event.from->get_voice(event.command.guild_id) || !dpp::find_guild(event.command.guild_id)->connect_member_voice(event.command.get_issuing_user().id)) + if (!event.from->get_voice(event.command.guild_id)) { - return event.reply("노래를 재생할 음성 채팅방에 먼저 참가하고 신청해야 합니다!"); + if (!dpp::find_guild(event.command.guild_id)->connect_member_voice(event.command.get_issuing_user().id)) + { + return event.reply("노래를 재생할 음성 채팅방에 먼저 참가하고 신청해야 합니다!"); + } } std::string Query = std::get(event.get_parameter("query")); event.thinking(); - auto findResult = queueMap->find(event.command.guild_id); - if (findResult == queueMap->end()) - { - FMusicQueueID queueID; - queueID.guild_id = event.command.guild_id; - queueID.shard_id = event.from->shard_id; - - (*queueMap)[queueID.guild_id] = std::make_shared(queueID); - } - std::shared_ptr queue = queueMap->find(event.command.guild_id)->second; + std::shared_ptr queue = getQueue(event); std::cout << "다운로드 시작" << "\n"; std::system(("python3 yt-download.py \"" + Query + "\" & wait").c_str()); @@ -106,7 +101,8 @@ void commands::Play::operator()(const dpp::slashcommand_t& event) { RequestedMusic.pop(); event.edit_original_response(msg); - while (!RequestedMusic.empty()) { + while (!RequestedMusic.empty()) + { dpp::message followMsg(event.command.channel_id, ""); followMsg.add_embed(RequestedMusic.front().embed); @@ -119,10 +115,11 @@ void commands::Play::operator()(const dpp::slashcommand_t& event) { dpp::voiceconn* v = event.from->get_voice(event.command.guild_id); /* If the voice channel was invalid, or there is an issue with it, then tell the user. */ - if (v && v->voiceclient && v->voiceclient->is_ready()) { - queue->play(botCluster); + if (v && v->voiceclient && v->voiceclient->is_ready()) + { + queue->play(); } - botCluster->on_voice_ready([this, queue](const dpp::voice_ready_t& Voice){ queue->play(botCluster); }); + botCluster->on_voice_ready([this, queue](const dpp::voice_ready_t& Voice){ queue->play(); }); return; } \ No newline at end of file diff --git a/src/Commands/Queue.cpp b/src/Commands/Queue.cpp index a6675cd..1dd7b91 100644 --- a/src/Commands/Queue.cpp +++ b/src/Commands/Queue.cpp @@ -54,7 +54,7 @@ dpp::embed makeEmbed(std::list::iterator& iter, std::list botCluster, std::unordered_map> *queueMap) - : ICommand(botCluster) + : VCCommand(botCluster) { this->queueMap = queueMap; dpp::slashcommand command = dpp::slashcommand("q", "노래 예약 큐 확인", botCluster->me.id); @@ -63,25 +63,24 @@ commands::Queue::Queue(std::shared_ptr botCluster, std::unordered_ } void commands::Queue::operator()(const dpp::slashcommand_t& event) { - dpp::message msg(event.command.channel_id, "지금 재생 중:"); + dpp::message msg; + msg.set_channel_id(event.command.channel_id); + std::shared_ptr queue = getQueue(event); - auto findResult = queueMap->find(event.command.guild_id); - if (findResult == queueMap->end()) - { - FMusicQueueID queueID; - queueID.guild_id = event.command.guild_id; - queueID.shard_id = event.from->shard_id; - - (*queueMap)[queueID.guild_id] = std::make_shared(queueID); + if (queue->size() < 1) { + auto iter = queue->begin(); + msg.add_embed(makeEmbed(iter, queue->end(), queue->repeat)); + } + else { + msg.set_content("지금 재생 중:"); + msg.add_embed(queue->peek(0).embed); } - std::shared_ptr queue = queueMap->find(event.command.guild_id)->second; - - msg.add_embed(queue->peek(0).embed); event.reply(msg, [this, queue, event](const dpp::confirmation_callback_t &_event) { auto iter = queue->begin(); + int queueSize = queue->size(); iter++; - for (int i = 0; i < ceil((queue->size() - 1) / 5.0); i++) { + for (int i = 0; i < ceil(queueSize / 5.0); i++) { dpp::embed followEmbed = makeEmbed(iter, queue->end(), queue->repeat, i * 5 + 1); dpp::message followMsg; diff --git a/src/Commands/Repeat.cpp b/src/Commands/Repeat.cpp index ecbf3a5..12e9e44 100644 --- a/src/Commands/Repeat.cpp +++ b/src/Commands/Repeat.cpp @@ -3,7 +3,7 @@ #include commands::Repeat::Repeat(std::shared_ptr botCluster, std::unordered_map> *queueMap) - : ICommand(botCluster) + : VCCommand(botCluster) { this->queueMap = queueMap; dpp::slashcommand command = dpp::slashcommand("r", "반복 켜기/끄기", botCluster->me.id); @@ -12,16 +12,7 @@ commands::Repeat::Repeat(std::shared_ptr botCluster, std::unordere } void commands::Repeat::operator()(const dpp::slashcommand_t& event) { - auto findResult = queueMap->find(event.command.guild_id); - if (findResult == queueMap->end()) - { - FMusicQueueID queueID; - queueID.guild_id = event.command.guild_id; - queueID.shard_id = event.from->shard_id; - - (*queueMap)[queueID.guild_id] = std::make_shared(queueID); - } - std::shared_ptr queue = queueMap->find(event.command.guild_id)->second; + std::shared_ptr queue = getQueue(event); if (queue->repeat) { event.reply("반복을 껐습니다."); diff --git a/src/Commands/Skip.cpp b/src/Commands/Skip.cpp index c484917..73bb7d6 100644 --- a/src/Commands/Skip.cpp +++ b/src/Commands/Skip.cpp @@ -3,7 +3,7 @@ #include commands::Skip::Skip(std::shared_ptr botCluster, std::unordered_map> *queueMap) - : ICommand(botCluster) + : VCCommand(botCluster) { this->queueMap = queueMap; dpp::slashcommand command = dpp::slashcommand("s", "현재곡 스킵", botCluster->me.id); @@ -19,19 +19,9 @@ void commands::Skip::operator()(const dpp::slashcommand_t& event) { } v->voiceclient->stop_audio(); + v->voiceclient->insert_marker("next marker"); - auto findResult = queueMap->find(event.command.guild_id); - if (findResult == queueMap->end()) - { - FMusicQueueID queueID; - queueID.guild_id = event.command.guild_id; - queueID.shard_id = event.from->shard_id; - - (*queueMap)[queueID.guild_id] = std::make_shared(queueID); - } - std::shared_ptr queue = queueMap->find(event.command.guild_id)->second; - - queue->play(botCluster); + std::shared_ptr queue = getQueue(event); event.reply("스킵했습니다!"); diff --git a/src/MusicQueue.cpp b/src/MusicQueue.cpp index 2ba41e6..7719736 100644 --- a/src/MusicQueue.cpp +++ b/src/MusicQueue.cpp @@ -4,11 +4,31 @@ #include #include -MusicQueue::MusicQueue(FMusicQueueID id) +MusicQueue::MusicQueue(FMusicQueueID id, std::shared_ptr botCluster) { this->id = id; repeat = false; - queuePlaying = false; + this->botCluster = botCluster; + + botCluster->on_voice_track_marker([this, botCluster](const dpp::voice_track_marker_t &marker) + { + std::cout << marker.track_meta << " Marker reached.\n"; + + if (empty()) + { + std::cout << "Queue ended\n"; + playMutex.unlock(); + return; + } + + auto music = pop(0); + if (repeat) + { + (*this) += music; + } + + markerCallback(); + }); } void MusicQueue::operator+=(FQueueElement operand) @@ -89,7 +109,7 @@ FMusicQueueID MusicQueue::getId() return id; } -void MusicQueue::markerCallback(std::shared_ptr botCluster) +void MusicQueue::markerCallback() { std::cout << "Music play started\n"; @@ -97,14 +117,13 @@ void MusicQueue::markerCallback(std::shared_ptr botCluster) if (!joinedShard) { std::cout << "No shard\n"; - queuePlaying = false; return; } if (empty()) { std::cout << "Queue ended\n"; - queuePlaying = false; + playMutex.unlock(); return; } @@ -114,7 +133,6 @@ void MusicQueue::markerCallback(std::shared_ptr botCluster) if (!v || !v->voiceclient || !v->voiceclient->is_ready()) { std::cout << "not in voicechat. quit musicplay"; - queuePlaying = false; return; } @@ -124,7 +142,6 @@ void MusicQueue::markerCallback(std::shared_ptr botCluster) /* If there was an issue reading the file, tell the user and stop */ if (!track_og) { fprintf(stderr, "Error opening file\n"); - queuePlaying = false; return; } @@ -169,23 +186,19 @@ void MusicQueue::markerCallback(std::shared_ptr botCluster) std::cout << "audio sending complete\n"; } -void MusicQueue::play(std::shared_ptr botCluster) +void MusicQueue::play() { - playMutex.lock(); - if (queuePlaying) + if (!playMutex.try_lock()) { - std::cout << "Already Playing\n"; - playMutex.unlock(); + std::cout << "Already playing\n"; return; } - queuePlaying = true; - playMutex.unlock(); dpp::discord_client* joinedShard = botCluster->get_shard(id.shard_id); if (!joinedShard) { std::cout << "No shard\n"; - queuePlaying = false; + playMutex.unlock(); return; } @@ -193,22 +206,9 @@ void MusicQueue::play(std::shared_ptr botCluster) if (!v || !v->voiceclient || !v->voiceclient->is_ready()) { std::cout << "not in voicechat. quit musicplay"; - queuePlaying = false; + playMutex.unlock(); return; } - - botCluster->on_voice_track_marker([this, botCluster, v](const dpp::voice_track_marker_t &marker) - { - std::cout << marker.track_meta << " Marker reached.\n"; - - auto music = pop(0); - if (repeat) - { - (*this) += music; - } - - markerCallback(botCluster); - }); - - markerCallback(botCluster); + + markerCallback(); } diff --git a/src/main.cpp b/src/main.cpp index 48b5c15..9aee70f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,8 @@ using json = nlohmann::json; -int main() { +int main() +{ json configdocument; std::ifstream configfile("config.json"); configfile >> configdocument;