코드 갈어엎기(사용성 개박살났으니 빌드는 이전 것으로 할 것.)

This commit is contained in:
2024-05-12 03:54:44 +09:00
parent ba56fe015f
commit 3f1edbbf16
29 changed files with 437 additions and 518 deletions

1
.gitignore vendored
View File

@@ -4,3 +4,4 @@ out/
.idea/ .idea/
tmp/ tmp/
config.json config.json
Music

View File

@@ -20,6 +20,7 @@ set_target_properties(${BOT_NAME} PROPERTIES
set(THREADS_PREFER_PTHREAD_FLAG TRUE) set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
find_package(DPP) find_package(DPP)
find_package(mariadbcpp)
if(APPLE) if(APPLE)
if(CMAKE_APPLE_SILICON_PROCESSOR) if(CMAKE_APPLE_SILICON_PROCESSOR)
set(OPENSSL_ROOT_DIR "/opt/homebrew/opt/openssl") set(OPENSSL_ROOT_DIR "/opt/homebrew/opt/openssl")
@@ -35,6 +36,8 @@ target_include_directories(${BOT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/include
${OPENSSL_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR}
/usr/include/opus /usr/include/opus
/usr/include/mariadb/conncpp
/usr/include/mariadb/conncpp/compat
) )
target_link_libraries(${BOT_NAME} target_link_libraries(${BOT_NAME}
@@ -44,6 +47,7 @@ target_link_libraries(${BOT_NAME}
opusfile opusfile
ogg ogg
oggz oggz
mariadbcpp
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}
${OPENSSL_CRYPTO_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY}
${OPENSSL_SSL_LIBRARY} ${OPENSSL_SSL_LIBRARY}
@@ -77,4 +81,5 @@ else()
target_link_libraries(${BOT_NAME} dpp) target_link_libraries(${BOT_NAME} dpp)
endif() endif()
set(CMAKE_CXX_FLAGS "-g") set(CMAKE_CXX_FLAGS "-g")
set(VMAKE_CXX_FLAGS "-lmariadbcpp")

View File

@@ -2,15 +2,20 @@
#include <Commands/CommandType.hpp> #include <Commands/CommandType.hpp>
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <memory> #include <memory>
#include <mariadb/conncpp.hpp>
class IBot { class IBot {
public: public:
IBot(std::string token, int totalShard); IBot(std::string token, int clusterCount, std::string DBURL, std::string DBID, std::string DBPassword);
void start(); virtual void start();
void onCommand(const dpp::slashcommand_t &event); virtual void onCommand(const dpp::slashcommand_t &event);
void onReady(const dpp::ready_t &event); virtual void onReady(const dpp::ready_t &event);
std::shared_ptr<dpp::cluster> botCluster; std::vector<std::shared_ptr<dpp::cluster>> botClusters;
std::vector<std::shared_ptr<commands::ICommand>> commandsArray; std::vector<std::shared_ptr<commands::ICommand>> commandsArray;
protected: protected:
sql::Driver* DBDriver;
std::shared_ptr<sql::SQLString> DBURL;
std::shared_ptr<sql::Properties> DBProperties;
}; };

View File

@@ -2,14 +2,29 @@
#include <string> #include <string>
#include <Bot.hpp> #include <Bot.hpp>
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <MusicQueue.hpp>
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
#include <FQueueElement.hpp>
class BumbleCeepp : public IBot { class BumbleCeepp : public IBot {
public: public:
BumbleCeepp(std::string token, int totalShard); BumbleCeepp(std::string token, int clusterCount, std::string DBURL, std::string DBID, std::string DBPassword);
void enqueueMusic(FQueueElement item, dpp::discord_voice_client* vc);
dpp::embed findEmbed(std::string musicID);
dpp::embed makeEmbed(
std::string webpage_url,
std::string title,
std::string uploader,
std::string id,
std::string thumbnail,
time_t duration);
bool repeat;
std::string nowPlayingMusic;
private: private:
//<guild_id, queue> 쌍임. //<guild_id, queueMutex> 쌍임.
std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> queueMap; std::unordered_map<dpp::snowflake, std::mutex> enqueuingMutexMap;
std::unordered_map<std::string, dpp::embed> musicEmbedMap;
}; };

View File

@@ -2,28 +2,21 @@
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <vector> #include <vector>
#include <list> #include <list>
#include <MusicQueue.hpp>
class BumbleCeepp;
namespace commands { namespace commands {
class ICommand { class ICommand {
public: public:
//이 생성자를 명시적으로 호출할 것. ICommand(dpp::snowflake botID, BumbleCeepp* Bot)
ICommand(std::shared_ptr<dpp::cluster> botCluster); {
this->botID = botID;
this->Bot = Bot;
}
virtual void operator()(const dpp::slashcommand_t &event) = 0; virtual void operator()(const dpp::slashcommand_t &event) = 0;
std::vector<dpp::slashcommand> commandObjectVector; std::vector<dpp::slashcommand> commandObjectVector;
protected: dpp::snowflake botID;
std::shared_ptr<dpp::cluster> botCluster; BumbleCeepp* Bot;
};
}
namespace commands {
class VCCommand : public ICommand {
public:
VCCommand(std::shared_ptr<dpp::cluster> botCluster) : ICommand(botCluster) {}
std::shared_ptr<MusicQueue> getQueue(const dpp::slashcommand_t& event);
protected:
std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap;
}; };
} }

View File

@@ -4,9 +4,9 @@
#include <memory> #include <memory>
namespace commands { namespace commands {
class Delete : public VCCommand { class Delete : public ICommand {
public: public:
Delete(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap); Delete(dpp::snowflake botID, BumbleCeepp* Bot);
void operator()(const dpp::slashcommand_t& event); void operator()(const dpp::slashcommand_t& event);
}; };

View File

@@ -4,9 +4,9 @@
#include <memory> #include <memory>
namespace commands { namespace commands {
class Leave : public VCCommand { class Leave : public ICommand {
public: public:
Leave(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap); Leave(dpp::snowflake botID, BumbleCeepp* Bot);
void operator()(const dpp::slashcommand_t& event); void operator()(const dpp::slashcommand_t& event);
}; };

View File

@@ -4,9 +4,9 @@
#include <memory> #include <memory>
namespace commands { namespace commands {
class Play : public VCCommand { class Play : public ICommand {
public: public:
Play(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap); Play(dpp::snowflake botID, BumbleCeepp* Bot);
void operator()(const dpp::slashcommand_t& event); void operator()(const dpp::slashcommand_t& event);
}; };

View File

@@ -4,9 +4,9 @@
#include <memory> #include <memory>
namespace commands { namespace commands {
class Queue : public VCCommand { class Queue : public ICommand {
public: public:
Queue(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap); Queue(dpp::snowflake botID, BumbleCeepp* Bot);
void operator()(const dpp::slashcommand_t& event); void operator()(const dpp::slashcommand_t& event);
}; };

View File

@@ -4,9 +4,9 @@
#include <memory> #include <memory>
namespace commands { namespace commands {
class Repeat : public VCCommand { class Repeat : public ICommand {
public: public:
Repeat(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap); Repeat(dpp::snowflake botID, BumbleCeepp* Bot);
void operator()(const dpp::slashcommand_t& event); void operator()(const dpp::slashcommand_t& event);
}; };

View File

@@ -4,9 +4,9 @@
#include <memory> #include <memory>
namespace commands { namespace commands {
class Skip : public VCCommand { class Skip : public ICommand {
public: public:
Skip(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap); Skip(dpp::snowflake botID, BumbleCeepp* Bot);
void operator()(const dpp::slashcommand_t& event); void operator()(const dpp::slashcommand_t& event);
}; };

View File

@@ -3,11 +3,7 @@
#include <dpp/dpp.h> #include <dpp/dpp.h>
struct FQueueElement { struct FQueueElement {
std::string URL; std::string ID;
std::string title;
std::string description;
std::string fileName;
std::string thumbnail;
std::string duration;
dpp::embed embed; dpp::embed embed;
bool skip = false;
}; };

View File

@@ -1,33 +0,0 @@
#pragma once
#include <list>
#include <FQueueElement.hpp>
struct FMusicQueueID {
dpp::snowflake guild_id;
uint32_t shard_id;
};
class MusicQueue {
public:
MusicQueue(FMusicQueueID id, std::shared_ptr<dpp::cluster> botCluster);
void operator+=(FQueueElement operand);
FQueueElement pop(int index);
FQueueElement peek(int index);
bool empty();
void clear();
std::list<struct FQueueElement>::iterator begin();
std::list<struct FQueueElement>::iterator end();
std::size_t size();
FMusicQueueID getId();
void play();
void markerCallback();
bool repeat;
private:
std::list<struct FQueueElement> queue;
std::mutex mutex;
std::mutex playMutex;
FMusicQueueID id;
std::shared_ptr<dpp::cluster> botCluster;
};

1
out Normal file

File diff suppressed because one or more lines are too long

View File

@@ -1,25 +1,34 @@
#include <Bot.hpp> #include <Bot.hpp>
#include <Commands/CommandType.hpp> #include <Commands/CommandType.hpp>
IBot::IBot(std::string token, int totalShard) IBot::IBot(std::string token, int clusterCount, std::string DBURL, std::string DBID, std::string DBPassword)
{ {
botCluster = std::make_shared<dpp::cluster>(token, dpp::i_default_intents, totalShard); this->DBURL = std::make_shared<sql::SQLString>(DBURL);
botCluster->on_log(dpp::utility::cout_logger()); sql::Properties pro({
{"user", DBID},
{"password", DBPassword}
});
this->DBProperties = std::make_shared<sql::Properties>(pro);
DBDriver = sql::mariadb::get_driver_instance();
botCluster->on_slashcommand([this](const dpp::slashcommand_t& event){onCommand(event);}); for (int i = 0; i<clusterCount; i++)
botCluster->on_ready([this](const dpp::ready_t &event){onReady(event);}); {
std::shared_ptr<dpp::cluster> cluster = std::make_shared<dpp::cluster>(token, dpp::i_default_intents);
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);
}
} }
void IBot::onCommand(const dpp::slashcommand_t &event) void IBot::onCommand(const dpp::slashcommand_t &event)
{ {
auto _event = event; auto _event = event;
for (int i = 0; i < commandsArray.size(); i++) { for (auto command : commandsArray)
for (auto alias : commandsArray[i]->commandObjectVector) { for (auto alias : command->commandObjectVector)
if (event.command.get_command_name() == alias.name) { if (event.command.get_command_name() == alias.name)
(*commandsArray[i])(_event); (*command)(_event);
}
}
}
} }
void IBot::onReady(const dpp::ready_t &event) void IBot::onReady(const dpp::ready_t &event)
@@ -29,14 +38,25 @@ void IBot::onReady(const dpp::ready_t &event)
//BotCluster->global_bulk_command_delete(); //BotCluster->global_bulk_command_delete();
for (int i = 0; i < commandsArray.size(); i++) { for (auto command : commandsArray)
for (auto Alias : commandsArray[i]->commandObjectVector) { for (auto alias : command->commandObjectVector)
botCluster->global_command_create(Alias); for (auto cluster : botClusters)
} cluster->global_command_create(alias);
}
botClusters[0]->log(dpp::loglevel::ll_info, "Command added to all clusters.");
} }
void IBot::start() void IBot::start()
{ {
botCluster->start(dpp::st_wait); 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);
} }

View File

@@ -2,16 +2,174 @@
#include <string> #include <string>
#include <FQueueElement.hpp> #include <FQueueElement.hpp>
#include <Commands/Commands.hpp> #include <Commands/Commands.hpp>
#include <oggz/oggz.h>
BumbleCeepp::BumbleCeepp(std::string token, int totalShard) BumbleCeepp::BumbleCeepp(std::string token, int clusterCount, std::string DBURL, std::string DBID, std::string DBPassword)
: IBot(token, totalShard) : IBot(token, clusterCount, DBURL, DBID, DBPassword)
{ {
commandsArray.push_back(std::make_shared<commands::Play>(botCluster, &queueMap));
commandsArray.push_back(std::make_shared<commands::Repeat>(botCluster, &queueMap)); commandsArray.push_back(std::make_shared<commands::Play>(botClusters[0]->me.id, this));
commandsArray.push_back(std::make_shared<commands::Queue>(botCluster, &queueMap)); commandsArray.push_back(std::make_shared<commands::Repeat>(botClusters[0]->me.id, this));
commandsArray.push_back(std::make_shared<commands::Skip>(botCluster, &queueMap)); commandsArray.push_back(std::make_shared<commands::Queue>(botClusters[0]->me.id, this));
commandsArray.push_back(std::make_shared<commands::Leave>(botCluster, &queueMap)); commandsArray.push_back(std::make_shared<commands::Skip>(botClusters[0]->me.id, this));
commandsArray.push_back(std::make_shared<commands::Delete>(botCluster, &queueMap)); commandsArray.push_back(std::make_shared<commands::Leave>(botClusters[0]->me.id, this));
commandsArray.push_back(std::make_shared<commands::Delete>(botClusters[0]->me.id, this));
for (auto cluster : botClusters)
{
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++;
std::cout << "Command added.\n"; if (!memberCount)
} {
auto joinedShard = marker.from;
std::cout << "voicechat is empty.";
marker.voice_client->stop_audio();
joinedShard->disconnect_voice(marker.voice_client->server_id);
return;
}
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(); });
}
}
void BumbleCeepp::enqueueMusic(FQueueElement item, dpp::discord_voice_client* vc)
{
vc->insert_marker(item.ID);
vc->log(dpp::loglevel::ll_debug, "Enqueueuing " + item.ID + "on channel id " + vc->channel_id.str() + ".");
std::lock_guard<std::mutex> lock(enqueuingMutexMap[vc->server_id]);
OGGZ *track_og = oggz_open(("Music/" + item.ID + ".ogg").c_str(), OGGZ_READ);
/* If there was an issue reading the file, tell the user and stop */
if (!track_og) {
fprintf(stderr, "Error opening file\n");
return;
}
/* 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::discord_voice_client *vc = (dpp::discord_voice_client *)user_data;
/* send the audio */
vc->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 *)vc
);
// read loop
while (vc && !vc->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;
}
}
/* Don't forget to free the memory */
oggz_close(track_og);
vc->log(dpp::loglevel::ll_debug, "Enqueued " + item.ID + "on channel id " + vc->channel_id.str() + ".");
}
dpp::embed BumbleCeepp::findEmbed(std::string musicID)
{
auto iter = musicEmbedMap.find(musicID);
if (iter != musicEmbedMap.end())
return iter->second;
std::unique_ptr<sql::Connection> conn(DBDriver->connect(*this->DBURL, *DBProperties));
std::unique_ptr<sql::PreparedStatement> stmnt(conn->prepareStatement("SELECT embed FROM songs_info WHERE ID = ?"));
stmnt->setString(1, musicID);
std::unique_ptr<sql::ResultSet> res(stmnt->executeQuery());
dpp::embed returnValue = makeEmbed(
res->getString("webpage_url").c_str(),
res->getString("title").c_str(),
res->getString("uploader").c_str(),
musicID,
res->getString("thumbnail").c_str(),
res->getInt("duration"));
musicEmbedMap[musicID] = returnValue;
return returnValue;
}
dpp::embed BumbleCeepp::makeEmbed(std::string webpage_url, std::string title, std::string uploader, std::string id, std::string thumbnail, time_t duration)
{
char SongLengthStr[10];
tm t;
t.tm_mday = duration / 86400;
t.tm_hour = (duration % 86400)/3600;
t.tm_min = (duration % 3600)/60;
t.tm_sec = duration%60;
strftime(SongLengthStr, sizeof(SongLengthStr), "%X", &t);
dpp::embed returnValue = dpp::embed()
.set_color(dpp::colors::sti_blue)
.set_title(title)
.set_description(uploader)
.set_url(webpage_url)
.set_image(thumbnail)
.add_field(
"길이",
SongLengthStr,
true
);
std::unique_ptr<sql::Connection> conn(DBDriver->connect(*this->DBURL, *DBProperties));
std::unique_ptr<sql::PreparedStatement> stmnt(
conn->prepareStatement("REPLACE INTO songs_info (ID, webpage_url, title, uploader, thumbnail, duration) VALUE (?, ?, ?, ?, ?, ?)"));
stmnt->setString(1, id);
stmnt->setString(2, webpage_url);
stmnt->setString(3, title);
stmnt->setString(4, uploader);
stmnt->setString(5, thumbnail);
stmnt->setInt(6, duration);
stmnt->executeQuery();
return returnValue;
}

View File

@@ -1,19 +0,0 @@
#include <Commands/CommandType.hpp>
commands::ICommand::ICommand(std::shared_ptr<dpp::cluster> botCluster)
{
this->botCluster = botCluster;
}
std::shared_ptr<MusicQueue> 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<MusicQueue>(queueID, botCluster);
}
return queueMap->find(event.command.guild_id)->second;
}

View File

@@ -1,14 +1,13 @@
#include <Commands/Delete.hpp> #include <Commands/Delete.hpp>
#include <iostream> #include <iostream>
commands::Delete::Delete(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap) commands::Delete::Delete(dpp::snowflake botID, BumbleCeepp* Bot)
: VCCommand(botCluster) : ICommand(botID, Bot)
{ {
this->queueMap = queueMap; dpp::slashcommand Command = dpp::slashcommand("d", "큐의 해당하는 번호의 노래를 지웁니다", botID);
dpp::slashcommand Command = dpp::slashcommand("d", "큐의 해당하는 번호의 노래를 지웁니다", botCluster->me.id);
Command.add_option( Command.add_option(
dpp::command_option(dpp::co_string, "pos", "큐 번호", botCluster->me.id) dpp::command_option(dpp::co_string, "pos", "큐 번호", botID)
); );
commandObjectVector.push_back(Command); commandObjectVector.push_back(Command);
@@ -23,37 +22,34 @@ void commands::Delete::operator()(const dpp::slashcommand_t& event)
std::string Pos = std::get<std::string>(event.get_parameter("pos")); std::string Pos = std::get<std::string>(event.get_parameter("pos"));
event.thinking(); event.thinking();
std::shared_ptr<MusicQueue> queue = getQueue(event);
auto index = atoi(Pos.c_str()); auto index = atoi(Pos.c_str());
int queueSize = queue->size(); auto vc = event.from->connecting_voice_channels.find(event.command.guild_id)->second->voiceclient;
int remainingSongsCount = vc->get_tracks_remaining() - 1;
std::vector<std::string> queuedSongs = vc->get_marker_metadata();
std::cout << "queue size : " << queueSize << "\n"; vc->log(dpp::loglevel::ll_trace, "Queue size : " + remainingSongsCount);
if (index < 0 || (queueSize - 1) < index) { if (index < 0 || remainingSongsCount+1 < index || (!vc->is_playing() && index == 0)) {
std::cout << "invalid index : " << index << ", " + Pos + "\n"; std::cout << "invalid index : " << index << ", " + Pos + "\n";
event.edit_original_response(dpp::message(event.command.channel_id, "이상한 인덱스 위치. Pos : " + Pos)); event.edit_original_response(dpp::message(event.command.channel_id, "이상한 인덱스 위치. Pos : " + Pos));
return; return;
} }
auto PopedElement = queue->pop(index); dpp::embed embed = Bot->findEmbed(queuedSongs[index - 1])
dpp::embed embed = PopedElement.embed
.set_timestamp(time(0)); .set_timestamp(time(0));
dpp::message msg(event.command.channel_id, "다음 항목을 큐에서 삭제했습니다!:"); dpp::message msg(event.command.channel_id, "다음 항목을 큐에서 삭제했습니다!:");
if (atoi(Pos.c_str()) == 0) { if (index == 0) {
dpp::voiceconn* v = event.from->get_voice(event.command.guild_id); dpp::voiceconn* v = event.from->get_voice(event.command.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) { if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
return; return;
} }
v->voiceclient->stop_audio(); v->voiceclient->skip_to_next_marker();
v->voiceclient->insert_marker("end of music");
} }
msg.add_embed(embed); msg.add_embed(embed);

View File

@@ -1,11 +1,10 @@
#include <Commands/Leave.hpp> #include <Commands/Leave.hpp>
#include <iostream> #include <iostream>
commands::Leave::Leave(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap) commands::Leave::Leave(dpp::snowflake botID, BumbleCeepp* Bot)
: VCCommand(botCluster) : ICommand(botID, Bot)
{ {
this->queueMap = queueMap; dpp::slashcommand command = dpp::slashcommand("l", "음챗을 떠납니다", botID);
dpp::slashcommand command = dpp::slashcommand("l", "음챗을 떠납니다", botCluster->me.id);
commandObjectVector.push_back(command); commandObjectVector.push_back(command);
} }
@@ -18,10 +17,6 @@ void commands::Leave::operator()(const dpp::slashcommand_t& event)
return; return;
} }
v->voiceclient->stop_audio(); v->voiceclient->stop_audio();
std::shared_ptr<MusicQueue> queue = getQueue(event);
queue->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

@@ -7,14 +7,13 @@
using json = nlohmann::json; using json = nlohmann::json;
commands::Play::Play(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap) commands::Play::Play(dpp::snowflake botID, BumbleCeepp* Bot)
: VCCommand(botCluster) : ICommand(botID, Bot)
{ {
this->queueMap = queueMap; dpp::slashcommand command = dpp::slashcommand("p", "노래 재생", botID);
dpp::slashcommand command = dpp::slashcommand("p", "노래 재생", botCluster->me.id);
command.add_option( command.add_option(
dpp::command_option(dpp::co_string, "query", "링크 또는 검색어", botCluster->me.id) dpp::command_option(dpp::co_string, "query", "링크 또는 검색어", botID)
); );
commandObjectVector.push_back(command); commandObjectVector.push_back(command);
@@ -41,13 +40,9 @@ void commands::Play::operator()(const dpp::slashcommand_t& event)
event.thinking(); event.thinking();
std::shared_ptr<MusicQueue> queue = getQueue(event); event.from->log(dpp::loglevel::ll_trace, "음악 다운로드 시작");
std::cout << "다운로드 시작" << "\n";
std::system(("python3 yt-download.py \"" + Query + "\" & wait").c_str()); std::system(("python3 yt-download.py \"" + Query + "\" & wait").c_str());
std::cout << "다운로드 완료" << "\n"; event.from->log(dpp::loglevel::ll_trace, "음악 다운로드 완료");
dpp::message msg(event.command.channel_id, "큐에 다음 곡을 추가했습니다:");
std::ifstream infofile, idfile; std::ifstream infofile, idfile;
json document; json document;
@@ -56,48 +51,54 @@ void commands::Play::operator()(const dpp::slashcommand_t& event)
idfile.open("Temp/CurMusic"); idfile.open("Temp/CurMusic");
while (std::getline(idfile, ID)) while (std::getline(idfile, ID))
{ {
std::cout << ID << "\n"; event.from->log(dpp::loglevel::ll_trace, "Red ID : " + ID);
infofile.open("Music/" + ID + ".info.json"); infofile.open("Music/" + ID + ".info.json");
infofile >> document; infofile >> document;
infofile.close(); infofile.close();
time_t SongLength = int(document["duration"]);
char SongLengthStr[10];
tm t;
t.tm_mday = SongLength / 86400;
t.tm_hour = (SongLength % 86400)/3600;
t.tm_min = (SongLength % 3600)/60;
t.tm_sec = SongLength%60;
strftime(SongLengthStr, sizeof(SongLengthStr), "%X", &t);
FQueueElement Data = { FQueueElement Data = {
std::string(document["webpage_url"]), ID,
std::string(document["title"]), Bot->makeEmbed(
std::string(document["uploader"]), document["webpage_url"],
std::string(document["id"]), document["title"],
std::string(document["thumbnail"]), document["uploader"],
to_string(document["duration"]), document["id"],
dpp::embed() document["thumbnail"],
.set_color(dpp::colors::sti_blue) int(document["duration"]))
.set_title(Data.title)
.set_description(Data.description)
.set_url(Data.URL)
.set_image(Data.thumbnail)
.add_field(
"길이",
SongLengthStr,
true
)
}; };
(*queue) += Data;
RequestedMusic.push(Data); RequestedMusic.push(Data);
} }
idfile.close(); idfile.close();
std::system("rm -f Temp/CurMusic"); std::system("rm -f Temp/CurMusic");
std::cout << "queued\n";
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())
{
auto _copiedQueue = RequestedMusic;
while (!_copiedQueue.empty())
{
Bot->enqueueMusic(_copiedQueue.front(), v->voiceclient);
_copiedQueue.pop();
}
}
else
{
auto _copiedQueue = RequestedMusic;
event.from->creator->on_voice_ready([&](const dpp::voice_ready_t& Voice)
{
while (!_copiedQueue.empty())
{
auto item = _copiedQueue.front();
Bot->enqueueMusic(item, Voice.voice_client);
_copiedQueue.pop();
}
});
}
dpp::message msg(event.command.channel_id, "큐에 다음 곡을 추가했습니다:");
msg.add_embed(RequestedMusic.front().embed); msg.add_embed(RequestedMusic.front().embed);
RequestedMusic.pop(); RequestedMusic.pop();
event.edit_original_response(msg); event.edit_original_response(msg);
@@ -109,18 +110,8 @@ void commands::Play::operator()(const dpp::slashcommand_t& event)
followMsg.add_embed(RequestedMusic.front().embed); followMsg.add_embed(RequestedMusic.front().embed);
RequestedMusic.pop(); RequestedMusic.pop();
botCluster->message_create(followMsg); event.from->creator->message_create(followMsg);
}
std::cout << "replied\n";
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->on_voice_ready([this, queue](const dpp::voice_ready_t& Voice){ queue->play(); });
return; return;
} }

View File

@@ -3,61 +3,10 @@
#include <sstream> #include <sstream>
#include <cmath> #include <cmath>
namespace commands { commands::Queue::Queue(dpp::snowflake botID, BumbleCeepp* Bot)
dpp::embed makeEmbed(std::list<FQueueElement>::iterator& iter, std::list<FQueueElement>::iterator end, bool Repeat = false, int Index = 0) : ICommand(botID, Bot)
{ {
dpp::embed embed = dpp::embed() dpp::slashcommand command = dpp::slashcommand("q", "노래 예약 큐 확인", botID);
.set_color(dpp::colors::sti_blue);
if (iter == end) {
embed
.set_title("큐가 비었습니다!")
.set_timestamp(time(0));
if (Repeat)
embed.add_field(":repeat:","");
return embed;
}
std::ostringstream Number;
int Start = Index;
for (; (Index < Start + 5) && (iter != end); iter++, Index++) {
Number.clear();
Number.str("");
Number << Index;
embed.add_field(
Number.str(),
"",
true
)
.add_field(
iter->title,
iter->description,
true
)
.add_field(
"",
""
);
}
if (iter == end) {
embed.set_timestamp(time(0));
if (Repeat)
embed.add_field(":repeat:","");
}
return embed;
}
}
commands::Queue::Queue(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap)
: VCCommand(botCluster)
{
this->queueMap = queueMap;
dpp::slashcommand command = dpp::slashcommand("q", "노래 예약 큐 확인", botCluster->me.id);
commandObjectVector.push_back(command); commandObjectVector.push_back(command);
} }
@@ -65,33 +14,65 @@ commands::Queue::Queue(std::shared_ptr<dpp::cluster> botCluster, std::unordered_
void commands::Queue::operator()(const dpp::slashcommand_t& event) { void commands::Queue::operator()(const dpp::slashcommand_t& event) {
dpp::message msg; dpp::message msg;
msg.set_channel_id(event.command.channel_id); msg.set_channel_id(event.command.channel_id);
std::shared_ptr<MusicQueue> queue = getQueue(event);
if (queue->size() < 1) { auto vc = event.from->connecting_voice_channels.find(event.command.guild_id)->second->voiceclient;
auto iter = queue->begin(); std::vector<std::string> queuedSongs = vc->get_marker_metadata();
msg.add_embed(makeEmbed(iter, queue->end(), queue->repeat));
int remainingSongsCount = vc->get_tracks_remaining() - 1;
if (remainingSongsCount <= 0 && !vc->is_playing()) {
//재생 중인 노래가 없고 큐에 노래가 없는 상황
dpp::embed embed = dpp::embed()
.set_color(dpp::colors::sti_blue)
.set_title("큐가 비었습니다!")
.set_timestamp(time(0));
if (Bot->repeat)
embed.add_field(":repeat:","");
msg.add_embed(embed);
} }
else { else {
msg.set_content("지금 재생 중:"); msg.set_content("지금 재생 중:");
msg.add_embed(queue->peek(0).embed); dpp::embed curMusicEmbed = Bot->findEmbed(Bot->nowPlayingMusic);
msg.add_embed(curMusicEmbed);
} }
event.reply(msg, [this, queue, event](const dpp::confirmation_callback_t &_event) { event.reply(msg, [&](const dpp::confirmation_callback_t &_event) {
auto iter = queue->begin(); for (int i = 0; i < (queuedSongs.size()+4) / 5; i++)
int queueSize = queue->size(); {
iter++; dpp::embed followEmbed = dpp::embed();
for (int i = 0; i < ceil(queueSize / 5.0); i++) { int j;
dpp::embed followEmbed = makeEmbed(iter, queue->end(), queue->repeat, i * 5 + 1); for (j = i; j < i + 5 && j < queuedSongs.size(); j++)
{
dpp::embed originalEmbed = Bot->findEmbed(queuedSongs[j]);
followEmbed.add_field(
std::to_string(j + 1),
"",
true
)
.add_field(
originalEmbed.title,
originalEmbed.description,
true
)
.add_field(
"",
""
);
}
if (j == queuedSongs.size())
{
followEmbed.set_timestamp(time(0));
if (Bot->repeat)
followEmbed.add_field(":repeat:","");
}
dpp::message followMsg; dpp::message followMsg;
followMsg.channel_id = event.command.channel_id; followMsg.channel_id = event.command.channel_id;
if (i == 0) {
followMsg.content = "현재 큐에 있는 항목:";
}
followMsg.add_embed(followEmbed); followMsg.add_embed(followEmbed);
event.from->creator->message_create(followMsg);
botCluster->message_create(followMsg);
} }
}); });
} }

View File

@@ -2,25 +2,23 @@
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <string> #include <string>
commands::Repeat::Repeat(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap) commands::Repeat::Repeat(dpp::snowflake botID, BumbleCeepp* Bot)
: VCCommand(botCluster) : ICommand(botID, Bot)
{ {
this->queueMap = queueMap; dpp::slashcommand command = dpp::slashcommand("r", "반복 켜기/끄기", botID);
dpp::slashcommand command = dpp::slashcommand("r", "반복 켜기/끄기", botCluster->me.id);
commandObjectVector.push_back(command); commandObjectVector.push_back(command);
} }
void commands::Repeat::operator()(const dpp::slashcommand_t& event) { void commands::Repeat::operator()(const dpp::slashcommand_t& event) {
std::shared_ptr<MusicQueue> queue = getQueue(event);
if (Bot->repeat) {
if (queue->repeat) {
event.reply("반복을 껐습니다."); event.reply("반복을 껐습니다.");
queue->repeat = false; Bot->repeat = false;
} }
else { else {
event.reply("반복을 켰습니다."); event.reply("반복을 켰습니다.");
queue->repeat = true; Bot->repeat = true;
} }
return; return;

View File

@@ -2,11 +2,10 @@
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <string> #include <string>
commands::Skip::Skip(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap) commands::Skip::Skip(dpp::snowflake botID, BumbleCeepp* Bot)
: VCCommand(botCluster) : ICommand(botID, Bot)
{ {
this->queueMap = queueMap; dpp::slashcommand command = dpp::slashcommand("s", "현재곡 스킵", botID);
dpp::slashcommand command = dpp::slashcommand("s", "현재곡 스킵", botCluster->me.id);
commandObjectVector.push_back(command); commandObjectVector.push_back(command);
} }
@@ -17,11 +16,7 @@ void commands::Skip::operator()(const dpp::slashcommand_t& event) {
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) { if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
return; return;
} }
v->voiceclient->skip_to_next_marker();
v->voiceclient->stop_audio();
v->voiceclient->insert_marker("next marker");
std::shared_ptr<MusicQueue> queue = getQueue(event);
event.reply("스킵했습니다!"); event.reply("스킵했습니다!");

View File

@@ -1 +0,0 @@
OSeStDEAZJQ

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@@ -1,227 +0,0 @@
#include <MusicQueue.hpp>
#include <ogg/ogg.h>
#include <oggz/oggz.h>
#include <opus/opusfile.h>
#include <thread>
MusicQueue::MusicQueue(FMusicQueueID id, std::shared_ptr<dpp::cluster> botCluster)
{
this->id = id;
repeat = false;
this->botCluster = botCluster;
botCluster->on_voice_track_marker([this](const dpp::voice_track_marker_t &marker)
{
std::cout << marker.track_meta << " Marker reached.\n";
if (empty())
{
std::cout << "Queue ended\n";
playMutex.unlock();
this->botCluster->get_shard(this->getId().shard_id)->disconnect_voice(this->getId().guild_id);
return;
}
auto music = pop(0);
if (repeat)
{
(*this) += music;
}
markerCallback();
});
}
void MusicQueue::operator+=(FQueueElement operand)
{
mutex.lock();
queue.push_back(operand);
mutex.unlock();
}
FQueueElement MusicQueue::pop(int index)
{
mutex.lock();
auto iter = queue.begin();
std::advance(iter, index);
auto returnValue = *iter;
queue.erase(iter);
mutex.unlock();
return returnValue;
}
FQueueElement MusicQueue::peek(int index)
{
mutex.lock();
auto iter = queue.begin();
std::advance(iter, index);
auto returnValue = *iter;
mutex.unlock();
return returnValue;
}
bool MusicQueue::empty()
{
mutex.lock();
bool empty = queue.empty();
mutex.unlock();
return empty;
}
void MusicQueue::clear()
{
mutex.lock();
queue.clear();
mutex.unlock();
}
std::list<struct FQueueElement>::iterator MusicQueue::begin()
{
mutex.lock();
auto returnValue = queue.begin();
mutex.unlock();
return returnValue;
}
std::list<struct FQueueElement>::iterator MusicQueue::end()
{
mutex.lock();
auto returnValue = queue.end();
mutex.unlock();
return returnValue;
}
std::size_t MusicQueue::size()
{
mutex.lock();
auto returnValue = queue.size();
mutex.unlock();
return returnValue;
}
FMusicQueueID MusicQueue::getId()
{
return id;
}
void MusicQueue::markerCallback()
{
if (!dpp::find_guild(id.guild_id)->voice_members.size())
{
std::cout << "voicechat is empty.";
playMutex.unlock();
return;
}
std::cout << "Music play started\n";
dpp::discord_client* joinedShard = botCluster->get_shard(id.shard_id);
if (!joinedShard)
{
std::cout << "No shard\n";
playMutex.unlock();
return;
}
if (empty())
{
std::cout << "Queue ended\n";
playMutex.unlock();
return;
}
FQueueElement music = peek(0);
dpp::voiceconn* v = joinedShard->get_voice(id.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready())
{
std::cout << "not in voicechat. quit musicplay";
playMutex.unlock();
return;
}
//v->voiceclient->error(4014);
/* load the audio file with oggz */
OGGZ *track_og = oggz_open(("Music/" + music.fileName + ".ogg").c_str(), OGGZ_READ);
/* If there was an issue reading the file, tell the user and stop */
if (!track_og) {
fprintf(stderr, "Error opening file\n");
return;
}
/* 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;
/* 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;
}
}
/* Don't forget to free the memory */
oggz_close(track_og);
v->voiceclient->insert_marker(music.fileName + " end");
std::cout << "audio sending complete\n";
playMutex.unlock();
}
void MusicQueue::play()
{
if (!playMutex.try_lock())
{
std::cout << "Already playing\n";
return;
}
dpp::discord_client* joinedShard = botCluster->get_shard(id.shard_id);
if (!joinedShard)
{
std::cout << "No shard\n";
playMutex.unlock();
return;
}
dpp::voiceconn* v = joinedShard->get_voice(id.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready())
{
std::cout << "not in voicechat. quit musicplay";
playMutex.unlock();
return;
}
markerCallback();
}

View File

@@ -10,9 +10,12 @@ int main()
std::ifstream configfile("config.json"); std::ifstream configfile("config.json");
configfile >> configdocument; configfile >> configdocument;
std::shared_ptr<BumbleCeepp> bumbleBee = std::make_shared<BumbleCeepp>(configdocument["token"], 1); std::shared_ptr<BumbleCeepp> bumbleBee = std::make_shared<BumbleCeepp>(
configdocument["token"], 1, configdocument["dbURL"], configdocument["user"], configdocument["password"]);
bumbleBee->start(); bumbleBee->start();
return 0; return 0;
} }

47
yt-download.py Normal file
View File

@@ -0,0 +1,47 @@
import yt_dlp
import sys
import os
import json
if len(sys.argv) != 2:
sys.exit()
ydl_opts = {
'quiet': True,
'clean_infojson': False,
'default_search': 'ytsearch',
'format': '251',
'outtmpl': {'default': 'Temp/%(id)s.temp'},
'overwrites': False,
'writeinfojson': True }
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(sys.argv[1], download=False)
id = list()
with open("out", "w") as f:
f.write(json.dumps(ydl.sanitize_info(info)))
with open("Music/Archive", "r") as f:
ArchiveList = f.read().split("\n")
if "entries" in info:
if len(info["entries"]) != 0:
for entry in info["entries"]:
if entry["id"] not in ArchiveList:
ydl.download(entry["webpage_url"])
os.system("echo " + entry["id"] + " >> Music/Archive")
os.system("yes n 2>/dev/null | ffmpeg -hide_banner -loglevel error -i \"" + "Temp/" + entry["id"] + ".temp" + "\" -c copy Music/" + entry["id"] + ".ogg")
os.system("mv Temp/" + entry["id"] + ".temp.info.json Music/" + entry["id"] + ".info.json")
id.append(entry["id"])
else:
if info["id"] not in ArchiveList:
ydl.download(info["webpage_url"])
os.system("echo " + info["id"] + " >> Music/Archive")
os.system("yes n 2>/dev/null | ffmpeg -hide_banner -loglevel error -i \"" + "Temp/" + info["id"] + ".temp" + "\" -c copy Music/" + info["id"] + ".ogg")
os.system("mv Temp/" + info["id"] + ".temp.info.json Music/" + info["id"] + ".info.json")
id.append(info["id"])
os.system("rm -f Temp/*.temp")
os.system("rm -f Temp/*.json")
with open("Temp/CurMusic", "w") as f:
for item in id:
f.write(item + "\n")