코드 가독성 리팩터

This commit is contained in:
2025-02-13 16:43:11 +09:00
parent bf0268c1e9
commit 52146c1f6a
10 changed files with 87 additions and 64 deletions

View File

@@ -1,7 +1,4 @@
#!/bin/bash
if [ "$(id -u)" -ne 0 ]; then
exec sudo "$0" "$@"
fi
cd build
cmake .. && make
cd ..

View File

@@ -1,21 +1,21 @@
FROM debian:sid
WORKDIR /
RUN apt update
RUN apt install -y curl
RUN apt install -y libopus0
RUN apt install -y tini
RUN apt install -y liboggz2
RUN apt install -y xz-utils
RUN apt install -y python3
RUN apt install -y python3-pip
RUN apt install -y ffmpeg
RUN apt install -y python3-certifi
RUN apt install -y python3-brotli
RUN apt install -y python3-websockets
RUN apt install -y python3-requests
RUN apt install -y python3-mutagen
RUN pip3 install --break-system-packages curl_cffi
RUN pip3 install --break-system-packages pycryptodome
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get install -y libopus0
RUN apt-get install -y tini
RUN apt-get install -y liboggz2
RUN apt-get install -y xz-utils
RUN apt-get install -y python3
RUN apt-get install -y python3-pip
RUN apt-get install -y ffmpeg
RUN apt-get install -y python3-certifi
RUN apt-get install -y python3-brotli
RUN apt-get install -y python3-websockets
RUN apt-get install -y python3-requests
RUN apt-get install -y python3-mutagen
RUN pip3 install --break-system-packages --no-cache-dir curl_cffi
RUN pip3 install --break-system-packages --no-cache-dir pycryptodome
RUN curl -Lo dpp.deb https://dl.dpp.dev/
RUN dpkg -i dpp.deb
RUN rm dpp.deb

View File

@@ -43,6 +43,7 @@ public:
std::shared_ptr<dpp::cluster> cluster;
/// @brief guild id 배열
std::vector<dpp::snowflake> GIDs;
private:
/// @brief Command 목록
std::unordered_map<std::string, std::shared_ptr<commands::ICommand>> commands;

View File

@@ -23,9 +23,11 @@ public:
/// @brief 명령어 별명
std::vector<std::string> aliases;
private:
/// @brief 봇 ID
dpp::snowflake botID;
protected:
/// @brief 음악재생 매니저
std::shared_ptr<MusicPlayManager> musicManager;
@@ -51,9 +53,10 @@ public: \
description = DESCRIPTION; \
init(); \
} \
virtual void execute(const dpp::slashcommand_t &event) override; \
void execute(const dpp::slashcommand_t &event) override; \
\
protected: \
virtual void init() override; \
void init() override; \
}; \
}

View File

@@ -15,22 +15,35 @@ public:
currentPlayingPosition = queue.begin();
repeat = true;
}
void enqueue(std::shared_ptr<MusicQueueElement> Element);
std::shared_ptr<MusicQueueElement> dequeue();
std::list<std::shared_ptr<MusicQueueElement>>::iterator findById(std::string id);
std::list<std::shared_ptr<MusicQueueElement>>::iterator findByIndex(int index);
std::shared_ptr<MusicQueueElement> nowplaying();
std::list<std::shared_ptr<MusicQueueElement>>::iterator next_music();
std::shared_ptr<MusicQueueElement> jump_to_index(int idx);
void clear();
std::shared_ptr<MusicQueueElement> erase(std::list<std::shared_ptr<MusicQueueElement>>::iterator it);
std::pair<std::shared_ptr<std::list<std::shared_ptr<MusicQueueElement>>>, std::list<std::shared_ptr<MusicQueueElement>>::iterator> getQueueCopy();
int size();
std::list<std::shared_ptr<MusicQueueElement>>::iterator end();
void
enqueue(std::shared_ptr<MusicQueueElement> Element);
std::shared_ptr<MusicQueueElement>
dequeue();
std::list<std::shared_ptr<MusicQueueElement>>::iterator
findById(std::string id);
std::list<std::shared_ptr<MusicQueueElement>>::iterator
findByIndex(int index);
std::shared_ptr<MusicQueueElement>
nowplaying();
std::list<std::shared_ptr<MusicQueueElement>>::iterator
next_music();
std::shared_ptr<MusicQueueElement>
jump_to_index(int idx);
void
clear();
std::shared_ptr<MusicQueueElement>
erase(std::list<std::shared_ptr<MusicQueueElement>>::iterator it);
std::pair<std::shared_ptr<std::list<std::shared_ptr<MusicQueueElement>>>, std::list<std::shared_ptr<MusicQueueElement>>::iterator>
getQueueCopy();
int
size();
std::list<std::shared_ptr<MusicQueueElement>>::iterator
end();
bool repeat;
std::list<std::shared_ptr<MusicQueueElement>>::iterator currentPlayingPosition;
private:
std::list<std::shared_ptr<MusicQueueElement>> queue;
std::mutex queueMutex;

View File

@@ -20,6 +20,7 @@ public:
}
return returnValue;
}
private:
static dpp::embed makeEmbedPart(
std::shared_ptr<std::list<std::shared_ptr<MusicQueueElement>>> queue,

View File

@@ -87,45 +87,52 @@ MusicQueueElement MusicPlayManager::getNowPlaying(const dpp::snowflake guildId)
}
void MusicPlayManager::send_audio_to_voice(std::shared_ptr<bumbleBee::MusicQueueElement> music, dpp::discord_voice_client* client) {
std::string command = "./streamOpus.sh ";
command += SettingsManager::getYTDLP_CMD() + " ";
command += SettingsManager::getFFMPEG_CMD() + " ";
command += "https://youtu.be/";
command += music->id;
std::thread t([](std::shared_ptr<bumbleBee::MusicQueueElement> music, dpp::discord_voice_client* client) {
std::string command = "./streamOpus.sh ";
command += SettingsManager::getYTDLP_CMD() + " ";
command += SettingsManager::getFFMPEG_CMD() + " ";
command += "https://youtu.be/";
command += music->id;
OGGZ* og = oggz_open_stdio(popen(command.c_str(), "r"), OGGZ_READ);
OGGZ* og = oggz_open_stdio(popen(command.c_str(), "r"), OGGZ_READ);
client->stop_audio();
// client->stop_audio(); //이거 필요함??
oggz_set_read_callback(
og, -1,
[](OGGZ *oggz, oggz_packet *packet, long serialno, void *user_data) {
auto voiceConn = (dpp::discord_voice_client *)user_data;
oggz_set_read_callback(
og, -1,
[](OGGZ *oggz, oggz_packet *packet, long serialno, void *user_data) {
auto voiceConn = (dpp::discord_voice_client *)user_data;
voiceConn->send_audio_opus(packet->op.packet, packet->op.bytes);
voiceConn->send_audio_opus(packet->op.packet, packet->op.bytes);
return 0;
},
(void *)client
);
return 0;
},
(void *)client
);
while (client && !client->terminating && music != nullptr) {
static const constexpr long CHUNK_READ = BUFSIZ * 2;
while (client && !client->terminating && music != nullptr) {
static const constexpr long CHUNK_READ = BUFSIZ * 2;
const long read_bytes = oggz_read(og, CHUNK_READ);
const long read_bytes = oggz_read(og, CHUNK_READ);
/* break on eof */
if (!read_bytes) {
break;
/* break on eof */
if (!read_bytes) {
break;
}
}
}
if (music != nullptr)
client->creator->log(dpp::ll_info, "Sending " + music->embed.title + " - " + music->id + " complete!");
if (music != nullptr)
client->creator->log(dpp::ll_info, "Sending " + music->embed.title + " - " + music->id + " complete!");
else {
client->stop_audio();
client->pause_audio(true);
}
oggz_close(og);
oggz_close(og);
client->insert_marker();
client->insert_marker();
}, music, client);
t.detach();
}
}

View File

@@ -7,7 +7,6 @@ namespace bumbleBee::commands {
if (pos < 0 || pos > musicManager->size(event.command.guild_id))
{
event.edit_original_response(dpp::message(std::string("이상한 인덱스 위치. Pos :") + std::to_string(pos)));
return;
}

View File

@@ -222,8 +222,8 @@ namespace bumbleBee::commands {
event.edit_original_response(dpp::message("현재 음성 채팅방에 있는 상태가 아닙니다!"));
return;
}
musicManager->play(v->voiceclient);
v->voiceclient->insert_marker();
v->voiceclient->pause_audio(false);
}
}

View File

@@ -46,8 +46,10 @@ std::list<std::shared_ptr<MusicQueueElement>>::iterator MusicQueue::next_music()
std::lock_guard<std::mutex> lock(queueMutex);
if (currentPlayingPosition == --queue.end() && repeat)
currentPlayingPosition = queue.begin();
else if (currentPlayingPosition == --queue.end() && !repeat)
else if (currentPlayingPosition == --queue.end() && !repeat) // 반복이 꺼져있을 때 큐 재생이 끝난 경우
currentPlayingPosition = queue.end();
else if (currentPlayingPosition == queue.end() && !repeat) // 반복이 꺼져있고 현재 재생 곡이 없는데 새 곡이 들어왔을 경우
currentPlayingPosition = --queue.end();
else
++currentPlayingPosition;
return currentPlayingPosition;