From 8607c4a8a52b80192d966814b87b3148c6db6cb3 Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Wed, 14 Aug 2024 10:28:48 +0900 Subject: [PATCH] =?UTF-8?q?logger=20=EC=97=B0=EA=B2=B0=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/c_cpp_properties.json | 14 ++++-- .vscode/settings.json | 4 +- CMakeLists.txt | 14 ++---- include/Bot.hpp | 15 +++++- include/BumbleCeepp.hpp | 2 +- src/Bot.cpp | 31 ++++-------- src/BumbleCeepp.cpp | 87 ++++++++++++++++------------------ src/Commands/Play.cpp | 17 +++++-- src/main.cpp | 6 +-- youtube-search.py | 22 +++++++++ src/yt-dlp => yt-dlp | Bin 11 files changed, 120 insertions(+), 92 deletions(-) create mode 100644 youtube-search.py rename src/yt-dlp => yt-dlp (100%) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 82d6448..5963335 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -3,14 +3,18 @@ { "name": "Linux", "includePath": [ - "${workspaceFolder}/**", - "/usr/include/opus" + "${workspaceFolder}/**" + ], + "defines": [ + "DDPP_CORO=on" ], - "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "c17", - "cppStandard": "gnu++17", - "intelliSenseMode": "linux-gcc-x64" + "cppStandard": "c++20", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "-DDPP_CORO" + ] } ], "version": 4 diff --git a/.vscode/settings.json b/.vscode/settings.json index 852b34b..e894935 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -73,6 +73,8 @@ "cinttypes": "cpp", "bitset": "cpp", "set": "cpp", - "regex": "cpp" + "regex": "cpp", + "format": "cpp", + "span": "cpp" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 2464c43..1941045 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,10 +13,13 @@ string(ASCII 27 Esc) set(CMAKE_POSITION_INDEPENDENT_CODE ON) set_target_properties(${BOT_NAME} PROPERTIES - CXX_STANDARD 17 + CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON ) +target_compile_definitions(${BOT_NAME} PUBLIC DPP_CORO) +target_compile_features(${BOT_NAME} PUBLIC cxx_std_20) + set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) find_package(DPP) @@ -34,15 +37,11 @@ endif() target_include_directories(${BOT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${OPENSSL_INCLUDE_DIR} - /usr/include/opus ) target_link_libraries(${BOT_NAME} dl dpp - opus - opusfile - ogg oggz mariadbcpp ${CMAKE_THREAD_LIBS_INIT} @@ -76,7 +75,4 @@ else() endif() target_link_libraries(${BOT_NAME} dpp) -endif() - -set(CMAKE_CXX_FLAGS "-g") -set(VMAKE_CXX_FLAGS "-lmariadbcpp") \ No newline at end of file +endif() \ No newline at end of file diff --git a/include/Bot.hpp b/include/Bot.hpp index 29a52d8..d1a3e02 100644 --- a/include/Bot.hpp +++ b/include/Bot.hpp @@ -6,14 +6,25 @@ class IBot { public: - IBot(std::string token, int clusterCount, std::string DBURL, std::string DBID, std::string DBPassword); + IBot(std::string token, std::string DBURL, std::string DBID, std::string DBPassword, int clusterCount = 0); virtual void start(); virtual void onCommand(const dpp::slashcommand_t &event); virtual void onReady(const dpp::ready_t &event); - std::vector> botClusters; + std::shared_ptr botCluster; std::vector> commandsArray; + std::function logger() { + return [&](const dpp::log_t& event){ + if (event.severity >= dpp::ll_error) + std::cerr << "[" << dpp::utility::current_date_time() << "] " << dpp::utility::loglevel(event.severity) << ": " << event.message << std::endl; + else if (event.severity - logLevel >= 0) + std::clog << "[" << dpp::utility::current_date_time() << "] " << dpp::utility::loglevel(event.severity) << ": " << event.message << std::endl; + }; + }; + + dpp::loglevel logLevel = dpp::ll_debug; + protected: sql::Driver* DBDriver; std::shared_ptr DBURL; diff --git a/include/BumbleCeepp.hpp b/include/BumbleCeepp.hpp index a58f109..3c85a67 100644 --- a/include/BumbleCeepp.hpp +++ b/include/BumbleCeepp.hpp @@ -8,7 +8,7 @@ class BumbleCeepp : public IBot { public: - BumbleCeepp(std::string token, int clusterCount, std::string DBURL, std::string DBID, std::string DBPassword); + BumbleCeepp(std::string token, std::string DBURL, std::string DBID, std::string DBPassword, int clusterCount = 0); void enqueueMusic(FQueueElement item, dpp::discord_voice_client* vc); dpp::embed findEmbed(std::string musicID); diff --git a/src/Bot.cpp b/src/Bot.cpp index 9c3b9bb..eec668b 100644 --- a/src/Bot.cpp +++ b/src/Bot.cpp @@ -1,7 +1,7 @@ #include #include -IBot::IBot(std::string token, int clusterCount, std::string DBURL, std::string DBID, std::string DBPassword) +IBot::IBot(std::string token, std::string DBURL, std::string DBID, std::string DBPassword, int clusterCount) { this->DBURL = std::make_shared(DBURL); sql::Properties pro({ @@ -11,15 +11,11 @@ IBot::IBot(std::string token, int clusterCount, std::string DBURL, std::string D this->DBProperties = std::make_shared(pro); DBDriver = sql::mariadb::get_driver_instance(); - for (int i = 0; i cluster = std::make_shared(token, dpp::i_default_intents); + botCluster = std::make_shared(token, dpp::i_default_intents, clusterCount); - cluster->on_log(dpp::utility::cout_logger()); - cluster->on_slashcommand([&](const dpp::slashcommand_t& event){onCommand(event);}); - cluster->on_ready([&](const dpp::ready_t &event){onReady(event);}); - botClusters.push_back(cluster); - } + botCluster->on_log(logger()); + botCluster->on_slashcommand([&](const dpp::slashcommand_t& event){onCommand(event);}); + botCluster->on_ready([&](const dpp::ready_t &event){onReady(event);}); } void IBot::onCommand(const dpp::slashcommand_t &event) @@ -40,23 +36,12 @@ void IBot::onReady(const dpp::ready_t &event) for (auto command : commandsArray) for (auto alias : command->commandObjectVector) - for (auto cluster : botClusters) - cluster->global_command_create(alias); + botCluster->global_command_create(alias); - botClusters[0]->log(dpp::loglevel::ll_info, "Command added to all clusters."); + botCluster->log(dpp::loglevel::ll_info, "Command added to all clusters."); } void IBot::start() { - if (botClusters.size() == 1) - { - botClusters[0]->start(dpp::st_wait); - return; - } - - for (int i = 0; i < botClusters.size() - 1; i++) - { - botClusters[i]->start(dpp::st_return); - } - botClusters[botClusters.size() - 1]->start(dpp::st_wait); + botCluster->start(dpp::st_wait); } \ No newline at end of file diff --git a/src/BumbleCeepp.cpp b/src/BumbleCeepp.cpp index 778da2d..552e06d 100644 --- a/src/BumbleCeepp.cpp +++ b/src/BumbleCeepp.cpp @@ -4,60 +4,57 @@ #include #include -BumbleCeepp::BumbleCeepp(std::string token, int clusterCount, std::string DBURL, std::string DBID, std::string DBPassword) - : IBot(token, clusterCount, DBURL, DBID, DBPassword) +BumbleCeepp::BumbleCeepp(std::string token, std::string DBURL, std::string DBID, std::string DBPassword, int clusterCount) + : IBot(token, DBURL, DBID, DBPassword, clusterCount) { - commandsArray.push_back(std::make_shared(botClusters[0]->me.id, this)); - commandsArray.push_back(std::make_shared(botClusters[0]->me.id, this)); - commandsArray.push_back(std::make_shared(botClusters[0]->me.id, this)); - commandsArray.push_back(std::make_shared(botClusters[0]->me.id, this)); - commandsArray.push_back(std::make_shared(botClusters[0]->me.id, this)); - commandsArray.push_back(std::make_shared(botClusters[0]->me.id, this)); + commandsArray.push_back(std::make_shared(botCluster->me.id, this)); + commandsArray.push_back(std::make_shared(botCluster->me.id, this)); + commandsArray.push_back(std::make_shared(botCluster->me.id, this)); + commandsArray.push_back(std::make_shared(botCluster->me.id, this)); + commandsArray.push_back(std::make_shared(botCluster->me.id, this)); + commandsArray.push_back(std::make_shared(botCluster->me.id, this)); - for (auto cluster : botClusters) + botCluster->on_voice_track_marker([&](const dpp::voice_track_marker_t &marker) { - cluster->on_voice_track_marker([&](const dpp::voice_track_marker_t &marker) + auto voice_members = dpp::find_guild(marker.voice_client->server_id)->voice_members; + dpp::snowflake connectedChannel = marker.voice_client->channel_id; + int memberCount = 0; + for (auto member : voice_members) + if ( member.second.channel_id == connectedChannel ) + memberCount++; + + if (!memberCount) { - auto voice_members = dpp::find_guild(marker.voice_client->server_id)->voice_members; - dpp::snowflake connectedChannel = marker.voice_client->channel_id; - int memberCount = 0; - for (auto member : voice_members) - if ( member.second.channel_id == connectedChannel ) - memberCount++; + auto joinedShard = marker.from; + std::cout << "voicechat is empty."; + marker.voice_client->stop_audio(); + joinedShard->disconnect_voice(marker.voice_client->server_id); + return; + } - if (!memberCount) - { - auto joinedShard = marker.from; - std::cout << "voicechat is empty."; - marker.voice_client->stop_audio(); - joinedShard->disconnect_voice(marker.voice_client->server_id); + + marker.voice_client->log(dpp::loglevel::ll_debug, "Playing " + marker.track_meta + "on channel id " + marker.voice_client->channel_id.str() + "."); + + int remainingSongsCount = marker.voice_client->get_tracks_remaining(); + marker.voice_client->log(dpp::loglevel::ll_trace, "Marker count : " + remainingSongsCount); + + if (remainingSongsCount <= 1 && !marker.voice_client->is_playing()) + { + auto joinedShard = marker.from; + std::cout << "Queue ended\n"; + if (!joinedShard) return; - } + marker.voice_client->stop_audio(); + joinedShard->disconnect_voice(marker.voice_client->server_id); + return; + } + if (repeat) + enqueueMusic({nowPlayingMusic, findEmbed(nowPlayingMusic)}, marker.voice_client); + }); - marker.voice_client->log(dpp::loglevel::ll_debug, "Playing " + marker.track_meta + "on channel id " + marker.voice_client->channel_id.str() + "."); - - int remainingSongsCount = marker.voice_client->get_tracks_remaining(); - marker.voice_client->log(dpp::loglevel::ll_trace, "Marker count : " + remainingSongsCount); - - if (remainingSongsCount <= 1 && !marker.voice_client->is_playing()) - { - auto joinedShard = marker.from; - std::cout << "Queue ended\n"; - if (!joinedShard) - return; - marker.voice_client->stop_audio(); - joinedShard->disconnect_voice(marker.voice_client->server_id); - return; - } - - if (repeat) - enqueueMusic({nowPlayingMusic, findEmbed(nowPlayingMusic)}, marker.voice_client); - }); - - // cluster->on_voice_ready([&](const dpp::voice_ready_t& Voice){ queue->play(); }); - } + // cluster->on_voice_ready([&](const dpp::voice_ready_t& Voice){ queue->play(); }); } diff --git a/src/Commands/Play.cpp b/src/Commands/Play.cpp index 26e0038..b526c0a 100644 --- a/src/Commands/Play.cpp +++ b/src/Commands/Play.cpp @@ -38,11 +38,22 @@ void commands::Play::operator()(const dpp::slashcommand_t& event) std::string Query = std::get(event.get_parameter("query")); - event.thinking(); + dpp::detail::co_await_resolve(event.co_thinking()); - event.from->log(dpp::loglevel::ll_trace, "음악 다운로드 시작"); + event.from->log(dpp::loglevel::ll_debug, "음악 ID 쿼리"); + dpp::utility::exec("python3 youtube-search.py", {Query}, [&](const std::string& output) -> void + { + if (!output.length()) + { + event.from->log(dpp::loglevel::ll_debug, "검색 결과 없음"); + event.edit_response("검색 결과가 없습니다."); + } + }); + std::system(("python3 youtube-search.py \"" + Query + "\" & wait").c_str()); + + event.from->log(dpp::loglevel::ll_debug, "음악 다운로드 시작"); std::system(("python3 yt-download.py \"" + Query + "\" & wait").c_str()); - event.from->log(dpp::loglevel::ll_trace, "음악 다운로드 완료"); + event.from->log(dpp::loglevel::ll_debug, "음악 다운로드 완료"); std::ifstream infofile, idfile; json document; diff --git a/src/main.cpp b/src/main.cpp index 20d98e4..21388dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,12 +10,12 @@ int main() std::ifstream configfile("config.json"); configfile >> configdocument; + // setvbuf(stdout, NULL, _IONBF, 0); + std::shared_ptr bumbleBee = std::make_shared( - configdocument["token"], 1, configdocument["dbURL"], configdocument["user"], configdocument["password"]); + configdocument["token"], configdocument["dbURL"], configdocument["user"], configdocument["password"]); bumbleBee->start(); - - return 0; } \ No newline at end of file diff --git a/youtube-search.py b/youtube-search.py new file mode 100644 index 0000000..a167949 --- /dev/null +++ b/youtube-search.py @@ -0,0 +1,22 @@ +import sys + +if len(sys.argv) != 2: + sys.exit() + +import urllib.parse + +def uri_validator(x): + try: + result = urllib.parse.urlparse(x) + return all([result.scheme, result.netloc]) + except AttributeError: + return False + +if uri_validator(sys.argv[1]) == True: + exit() + +from youtube_search import YoutubeSearch + +results = YoutubeSearch(sys.argv[1], max_results=10).to_dict() + +print(results[0]["id"]) \ No newline at end of file diff --git a/src/yt-dlp b/yt-dlp similarity index 100% rename from src/yt-dlp rename to yt-dlp