큐 완전 개혁

This commit is contained in:
2024-01-02 14:41:24 +09:00
parent 70c70eee93
commit c996f290db
30 changed files with 771 additions and 576 deletions

View File

@@ -0,0 +1,6 @@
#include <Commands/CommandType.hpp>
commands::ICommand::ICommand(std::shared_ptr<dpp::cluster> botCluster)
{
this->botCluster = botCluster;
}

View File

@@ -1,35 +1,57 @@
#include <Commands/Delete.hpp>
#include <iostream>
namespace Commands {
Delete::Delete(std::shared_ptr<BumbleCeepp> Bot) {
this->Bot = Bot;
commands::Delete::Delete(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap)
: ICommand(botCluster)
{
this->queueMap = queueMap;
dpp::slashcommand Command = dpp::slashcommand("d", "큐의 해당하는 번호의 노래를 지웁니다", botCluster->me.id);
dpp::slashcommand Command = dpp::slashcommand("d", "큐의 해당하는 번호의 노래를 지웁니다", Bot->BotCluster->me.id);
Command.add_option(
dpp::command_option(dpp::co_string, "pos", "큐 번호", botCluster->me.id)
);
Command.add_option(
dpp::command_option(dpp::co_string, "pos", "큐 번호", Bot->BotCluster->me.id)
);
commandObjectVector.push_back(Command);
}
CommandObjectVector.push_back(Command);
void commands::Delete::operator()(const dpp::slashcommand_t& event)
{
if (std::holds_alternative<std::monostate>(event.get_parameter("pos"))) {
event.reply("삭제할 노래의 인덱스가 정확하지 않습니다.");
return;
}
std::string Pos = std::get<std::string>(event.get_parameter("pos"));
event.thinking();
void Delete::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
std::string Pos = std::get<std::string>(Event.get_parameter("pos"));
if (!atoi(Pos.c_str())) {
dpp::message msg(Event.command.channel_id, "현재 재생중인 곡은 삭제할 수 없습니다!");
Event.reply(msg);
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);
}
std::shared_ptr<MusicQueue> queue = queueMap->find(event.command.guild_id)->second;
auto PopedElement = queue->pop(atoi(Pos.c_str()));
dpp::embed embed = PopedElement.embed
.set_timestamp(time(0));
dpp::message msg(event.command.channel_id, "다음 항목을 큐에서 삭제했습니다!:");
if (!atoi(Pos.c_str())) {
dpp::voiceconn* v = event.from->get_voice(event.command.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
return;
}
auto PopedElement = Bot->QueueDelete(atoi(Pos.c_str()));
dpp::embed embed = PopedElement.embed
.set_timestamp(time(0));
dpp::message msg(Event.command.channel_id, "다음 항목을 큐에서 삭제했습니다!:");
msg.add_embed(embed);
Event.reply(msg);
v->voiceclient->stop_audio();
}
msg.add_embed(embed);
event.edit_original_response(msg);
}

View File

@@ -1,30 +1,39 @@
#include <Commands/Leave.hpp>
#include <iostream>
namespace Commands {
Leave::Leave(std::shared_ptr<BumbleCeepp> Bot) {
this->Bot = Bot;
commands::Leave::Leave(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap)
: ICommand(botCluster)
{
this->queueMap = queueMap;
dpp::slashcommand command = dpp::slashcommand("l", "음챗을 떠납니다", botCluster->me.id);
dpp::slashcommand Command = dpp::slashcommand("l", "음챗을 떠납니다", Bot->BotCluster->me.id);
commandObjectVector.push_back(command);
}
CommandObjectVector.push_back(Command);
void commands::Leave::operator()(const dpp::slashcommand_t& event)
{
dpp::voiceconn* v = event.from->get_voice(event.command.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
return;
}
v->voiceclient->stop_audio();
void Leave::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
std::cout << "disconnecting..\n";
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;
dpp::voiceconn* v = Event.from->get_voice(Event.command.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
return;
}
v->voiceclient->stop_audio();
MusicQueue.clear();
Event.from->disconnect_voice(Event.command.guild_id);
dpp::message msg(Event.command.channel_id, "음성 채팅방을 떠납니다!");
Event.reply(msg);
(*queueMap)[queueID.guild_id] = std::make_shared<MusicQueue>(queueID);
}
std::shared_ptr<MusicQueue> queue = queueMap->find(event.command.guild_id)->second;
queue->clear();
event.from->disconnect_voice(event.command.guild_id);
dpp::message msg(event.command.channel_id, "음성 채팅방을 떠납니다!");
event.reply(msg);
}

View File

@@ -7,114 +7,122 @@
using json = nlohmann::json;
namespace Commands {
Play::Play(std::shared_ptr<BumbleCeepp> Bot) {
this->Bot = Bot;
commands::Play::Play(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap)
: ICommand(botCluster)
{
this->queueMap = queueMap;
dpp::slashcommand command = dpp::slashcommand("p", "노래 재생", botCluster->me.id);
dpp::slashcommand Command = dpp::slashcommand("p", "노래 재생", Bot->BotCluster->me.id);
command.add_option(
dpp::command_option(dpp::co_string, "query", "링크 또는 검색어", botCluster->me.id)
);
Command.add_option(
dpp::command_option(dpp::co_string, "query", "링크 또는 검색어", Bot->BotCluster->me.id)
);
commandObjectVector.push_back(command);
}
CommandObjectVector.push_back(Command);
}
void Play::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
if (std::holds_alternative<std::monostate>(Event.get_parameter("query"))) {
Event.reply("노래를 재생하려면 좀 노래를 넣고 재생시켜라 게이야");
return;
}
/* Attempt to connect to a voice channel, returns false if we fail to connect. */
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<std::string>(Event.get_parameter("query"));
std::cout << "query: " << Query << "\n";
Event.thinking();
Bot->YTDLMutex.lock();
std::cout << "다운로드 시작" << "\n";
std::system(("python3 yt-download.py \"" + Query + "\" & wait").c_str());
std::cout << "다운로드 완료" << "\n";
Bot->YTDLMutex.unlock();
dpp::message msg(Event.command.channel_id, "큐에 다음 곡을 추가했습니다:");
std::ifstream infofile, idfile;
json document;
std::string ID;
std::queue<FQueueElement> RequestedMusic;
idfile.open("Temp/CurMusic");
while (std::getline(idfile, ID)) {
std::cout << ID << "\n";
infofile.open("Music/" + ID + ".info.json");
infofile >> document;
infofile.close();
time_t SongLength = int(document["duration"]);
char SongLengthStr[10];
tm t;
t.tm_sec = SongLength%60;
t.tm_min = SongLength/60;
t.tm_hour = SongLength/360;
strftime(SongLengthStr, sizeof(SongLengthStr), "%X", &t);
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);
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";
dpp::voiceconn* v = Event.from->get_voice(Event.command.guild_id);
Bot->VoiceJoinedShardId = Event.from->shard_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()) {
return Bot->QueuePlay();
}
Bot->BotCluster->on_voice_ready([this](const dpp::voice_ready_t& Voice){ Bot->QueuePlay(); });
void commands::Play::operator()(const dpp::slashcommand_t& event) {
if (std::holds_alternative<std::monostate>(event.get_parameter("query")))
{
event.reply("노래를 재생하려면 검색어 또는 링크를 입력해 주십시오.");
return;
}
/* 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))
{
return event.reply("노래를 재생할 음성 채팅방에 먼저 참가하고 신청해야 합니다!");
}
std::string Query = std::get<std::string>(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<MusicQueue>(queueID);
}
std::shared_ptr<MusicQueue> queue = queueMap->find(event.command.guild_id)->second;
std::cout << "다운로드 시작" << "\n";
std::system(("python3 yt-download.py \"" + Query + "\" & wait").c_str());
std::cout << "다운로드 완료" << "\n";
dpp::message msg(event.command.channel_id, "큐에 다음 곡을 추가했습니다:");
std::ifstream infofile, idfile;
json document;
std::string ID;
std::queue<FQueueElement> RequestedMusic;
idfile.open("Temp/CurMusic");
while (std::getline(idfile, ID))
{
std::cout << ID << "\n";
infofile.open("Music/" + ID + ".info.json");
infofile >> document;
infofile.close();
time_t SongLength = int(document["duration"]);
char SongLengthStr[10];
tm t;
t.tm_sec = SongLength%60;
t.tm_min = SongLength/60;
t.tm_hour = SongLength/360;
strftime(SongLengthStr, sizeof(SongLengthStr), "%X", &t);
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"]),
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
)
};
(*queue) += 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);
while (!RequestedMusic.empty()) {
dpp::message followMsg(event.command.channel_id, "");
followMsg.add_embed(RequestedMusic.front().embed);
RequestedMusic.pop();
botCluster->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);
}
botCluster->on_voice_ready([this, queue](const dpp::voice_ready_t& Voice){ queue->play(botCluster); });
return;
}

View File

@@ -3,89 +3,96 @@
#include <sstream>
#include <cmath>
namespace Commands {
dpp::embed MakeEmbed(std::list<FQueueElement>::iterator& iter, std::list<FQueueElement>::iterator end, bool Repeat = false, int Index = 0) {
dpp::embed embed = dpp::embed()
.set_color(dpp::colors::sti_blue);
namespace commands {
dpp::embed makeEmbed(std::list<FQueueElement>::iterator& iter, std::list<FQueueElement>::iterator end, bool Repeat = false, int Index = 0)
{
dpp::embed embed = dpp::embed()
.set_color(dpp::colors::sti_blue);
if (iter == end) {
embed
.set_title("큐가 비었습니다!")
.set_timestamp(time(0));
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("");
if (!Index) {
embed.add_field(
"현재 재생 중",
"",
true
);
}
else {
Number << Index;
embed.add_field(
Number.str(),
"",
true
);
}
embed.add_field(
iter->title,
iter->description,
true
)
.add_field(
"",
""
);
}
if (iter == end) {
embed.set_timestamp(time(0));
if (Repeat)
embed.add_field(":repeat:","");
}
if (Repeat)
embed.add_field(":repeat:","");
return embed;
}
Queue::Queue(std::shared_ptr<BumbleCeepp> Bot) {
this->Bot = Bot;
std::ostringstream Number;
int Start = Index;
dpp::slashcommand Command = dpp::slashcommand("q", "노래 예약 큐 확인", Bot->BotCluster->me.id);
CommandObjectVector.push_back(Command);
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(
"",
""
);
}
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, "현재 큐에 있는 항목:");
msg.add_embed(embed);
if (iter == end) {
embed.set_timestamp(time(0));
if (Repeat)
embed.add_field(":repeat:","");
}
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);
return embed;
}
}
Bot->BotCluster->message_create(FollowMsg);
commands::Queue::Queue(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap)
: ICommand(botCluster)
{
this->queueMap = queueMap;
dpp::slashcommand command = dpp::slashcommand("q", "노래 예약 큐 확인", botCluster->me.id);
commandObjectVector.push_back(command);
}
void commands::Queue::operator()(const dpp::slashcommand_t& event) {
dpp::message msg(event.command.channel_id, "지금 재생 중:");
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);
}
std::shared_ptr<MusicQueue> 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();
iter++;
for (int i = 0; i < ceil((queue->size() - 1) / 5.0); i++) {
dpp::embed followEmbed = makeEmbed(iter, queue->end(), queue->repeat, i * 5 + 1);
dpp::message followMsg;
followMsg.channel_id = event.command.channel_id;
if (i == 0) {
followMsg.content = "현재 큐에 있는 항목:";
}
});
}
followMsg.add_embed(followEmbed);
botCluster->message_create(followMsg);
}
});
}

View File

@@ -2,25 +2,35 @@
#include <dpp/dpp.h>
#include <string>
namespace Commands {
Repeat::Repeat(std::shared_ptr<BumbleCeepp> Bot) {
this->Bot = Bot;
commands::Repeat::Repeat(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap)
: ICommand(botCluster)
{
this->queueMap = queueMap;
dpp::slashcommand command = dpp::slashcommand("r", "반복 켜기/끄기", botCluster->me.id);
dpp::slashcommand Command = dpp::slashcommand("r", "반복 켜기/끄기", Bot->BotCluster->me.id);
commandObjectVector.push_back(command);
}
CommandObjectVector.push_back(Command);
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<MusicQueue>(queueID);
}
std::shared_ptr<MusicQueue> queue = queueMap->find(event.command.guild_id)->second;
if (queue->repeat) {
event.reply("반복을 껐습니다.");
queue->repeat = false;
}
else {
event.reply("반복을 켰습니다.");
queue->repeat = true;
}
void Repeat::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
if (Bot->Repeat) {
Event.reply("반복을 껐습니다.");
Bot->Repeat = false;
}
else {
Event.reply("반복을 켰습니다.");
Bot->Repeat = true;
}
return;
}
return;
}

View File

@@ -2,26 +2,38 @@
#include <dpp/dpp.h>
#include <string>
namespace Commands {
Skip::Skip(std::shared_ptr<BumbleCeepp> Bot) {
this->Bot = Bot;
commands::Skip::Skip(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap)
: ICommand(botCluster)
{
this->queueMap = queueMap;
dpp::slashcommand command = dpp::slashcommand("s", "현재곡 스킵", botCluster->me.id);
dpp::slashcommand Command = dpp::slashcommand("s", "현재곡 스킵", Bot->BotCluster->me.id);
commandObjectVector.push_back(command);
}
CommandObjectVector.push_back(Command);
}
void Skip::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
dpp::voiceconn* v = Event.from->get_voice(Event.command.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
return;
}
v->voiceclient->stop_audio();
Event.reply("스킵했습니다!");
void commands::Skip::operator()(const dpp::slashcommand_t& event) {
dpp::voiceconn* v = event.from->get_voice(event.command.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
return;
}
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<MusicQueue>(queueID);
}
std::shared_ptr<MusicQueue> queue = queueMap->find(event.command.guild_id)->second;
queue->play(botCluster);
event.reply("스킵했습니다!");
return;
}