큐 완전 개혁

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

@@ -35,6 +35,8 @@ 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}

View File

@@ -1,22 +1,16 @@
#ifndef _BOT_HPP_
#define _BOT_HPP_
#include <CommandType.hpp>
#pragma once
#include <Commands/CommandType.hpp>
#include <dpp/dpp.h>
#include <memory>
class IBot {
private:
public:
IBot(std::string Token, int TotalShard);
void AddCommand(ICommand &Command);
void Start();
IBot(std::string token, int totalShard);
void start();
void onCommand(const dpp::slashcommand_t &event);
void onReady(const dpp::ready_t &event);
std::shared_ptr<dpp::cluster> BotCluster;
std::shared_ptr<dpp::cluster> botCluster;
std::vector<std::shared_ptr<commands::ICommand>> commandsArray;
protected:
virtual void OnReady(const dpp::ready_t& event);
virtual void OnCommand(const dpp::slashcommand_t& event);
std::vector<ICommand*> CommandsArray;
};
#endif

View File

@@ -1,28 +1,15 @@
#ifndef _BUMBLECEEPP_HPP_
#define _BUMBLECEEPP_HPP_
#pragma once
#include <string>
#include <list>
#include <mutex>
#include <Bot.hpp>
#include <dpp/dpp.h>
#include <MusicQueue.hpp>
#include <memory>
#include <unordered_map>
class BumbleCeepp : public IBot {
public:
BumbleCeepp(std::string Token, int TotalShard);
void enqueue(struct FQueueElement Element);
struct FQueueElement QueueDelete(int Index);
void QueuePlay();
uint32_t VoiceJoinedShardId;
bool Repeat;
std::mutex YTDLMutex;
std::list<struct FQueueElement> MusicQueue;
protected:
BumbleCeepp(std::string token, int totalShard);
private:
void OnCommand(const dpp::slashcommand_t& Event);
std::mutex QueueMutex;
bool QueuePlaying;
};
#endif
//<guild_id, queue> 쌍임.
std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> queueMap;
};

View File

@@ -1,16 +0,0 @@
#ifndef _COMMANDTYPE_HPP_
#define _COMMANDTYPE_HPP_
#include <dpp/dpp.h>
#include <vector>
#include <list>
#include <FQueueElement.hpp>
class ICommand {
public:
virtual void operator() (const dpp::slashcommand_t& Event) = 0;
virtual void operator() (std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) = 0;
std::vector<dpp::slashcommand> CommandObjectVector;
};
#endif

View File

@@ -0,0 +1,18 @@
#pragma once
#include <dpp/dpp.h>
#include <vector>
#include <list>
#include <MusicQueue.hpp>
namespace commands {
class ICommand {
public:
//이 생성자를 명시적으로 호출할 것.
ICommand(std::shared_ptr<dpp::cluster> botCluster);
virtual void operator()(const dpp::slashcommand_t &event) = 0;
std::vector<dpp::slashcommand> commandObjectVector;
protected:
std::shared_ptr<dpp::cluster> botCluster;
};
}

View File

@@ -1,11 +1,7 @@
#ifndef _COMMANDS_HPP_
#define _COMMANDS_HPP_
#pragma once
#include <Commands/Play.hpp>
#include <Commands/Repeat.hpp>
#include <Commands/Queue.hpp>
#include <Commands/Skip.hpp>
#include <Commands/Leave.hpp>
#include <Commands/Delete.hpp>
#endif
#include <Commands/Delete.hpp>

View File

@@ -1,19 +1,15 @@
#ifndef _DELETE_HPP_
#define _DELETE_HPP_
#include <CommandType.hpp>
#pragma once
#include <Commands/CommandType.hpp>
#include <BumbleCeepp.hpp>
#include <memory>
namespace Commands {
class Delete : public ICommand {
public:
Delete(std::shared_ptr<BumbleCeepp> Bot);
namespace commands {
class Delete : public ICommand {
public:
Delete(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap);
void operator()(const dpp::slashcommand_t& Event) {}
void operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event);
private:
std::shared_ptr<BumbleCeepp> Bot;
};
}
#endif
void operator()(const dpp::slashcommand_t& event);
private:
std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap;
};
}

View File

@@ -1,19 +1,15 @@
#ifndef _LEAVE_HPP_
#define _LEAVE_HPP_
#include <CommandType.hpp>
#pragma once
#include <Commands/CommandType.hpp>
#include <BumbleCeepp.hpp>
#include <memory>
namespace Commands {
class Leave : public ICommand {
public:
Leave(std::shared_ptr<BumbleCeepp> Bot);
namespace commands {
class Leave : public ICommand {
public:
Leave(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap);
void operator()(const dpp::slashcommand_t& Event) {}
void operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event);
private:
std::shared_ptr<BumbleCeepp> Bot;
};
}
#endif
void operator()(const dpp::slashcommand_t& event);
private:
std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap;
};
}

View File

@@ -1,19 +1,15 @@
#ifndef _PLAY_HPP_
#define _PLAY_HPP_
#include <CommandType.hpp>
#pragma once
#include <Commands/CommandType.hpp>
#include <BumbleCeepp.hpp>
#include <memory>
namespace Commands {
class Play : public ICommand {
public:
Play(std::shared_ptr<BumbleCeepp> Bot);
namespace commands {
class Play : public ICommand {
public:
Play(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap);
void operator()(const dpp::slashcommand_t& Event) {}
void operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event);
private:
std::shared_ptr<BumbleCeepp> Bot;
};
}
#endif
void operator()(const dpp::slashcommand_t& event);
private:
std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap;
};
}

View File

@@ -1,19 +1,15 @@
#ifndef _QUEUE_HPP_
#define _QUEUE_HPP_
#include <CommandType.hpp>
#pragma once
#include <Commands/CommandType.hpp>
#include <BumbleCeepp.hpp>
#include <memory>
namespace Commands {
class Queue : public ICommand {
public:
Queue(std::shared_ptr<BumbleCeepp> Bot);
namespace commands {
class Queue : public ICommand {
public:
Queue(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap);
void operator()(const dpp::slashcommand_t& Event) {}
void operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event);
private:
std::shared_ptr<BumbleCeepp> Bot;
};
}
#endif
void operator()(const dpp::slashcommand_t& event);
private:
std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap;
};
}

View File

@@ -1,19 +1,15 @@
#ifndef _REPEAT_HPP_
#define _REPEAT_HPP_
#include <CommandType.hpp>
#pragma once
#include <Commands/CommandType.hpp>
#include <BumbleCeepp.hpp>
#include <memory>
namespace Commands {
class Repeat : public ICommand {
public:
Repeat(std::shared_ptr<BumbleCeepp> Bot);
namespace commands {
class Repeat : public ICommand {
public:
Repeat(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap);
void operator()(const dpp::slashcommand_t& Event) {}
void operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event);
private:
std::shared_ptr<BumbleCeepp> Bot;
};
}
#endif
void operator()(const dpp::slashcommand_t& event);
private:
std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap;
};
}

View File

@@ -1,19 +1,15 @@
#ifndef _SKIP_HPP_
#define _SKIP_HPP_
#include <CommandType.hpp>
#pragma once
#include <Commands/CommandType.hpp>
#include <BumbleCeepp.hpp>
#include <memory>
namespace Commands {
class Skip : public ICommand {
public:
Skip(std::shared_ptr<BumbleCeepp> Bot);
namespace commands {
class Skip : public ICommand {
public:
Skip(std::shared_ptr<dpp::cluster> botCluster, std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap);
void operator()(const dpp::slashcommand_t& Event) {}
void operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event);
private:
std::shared_ptr<BumbleCeepp> Bot;
};
}
#endif
void operator()(const dpp::slashcommand_t& event);
private:
std::unordered_map<dpp::snowflake, std::shared_ptr<MusicQueue>> *queueMap;
};
}

View File

@@ -1,5 +1,4 @@
#ifndef _FQUEUEELEMENT_HPP_
#define _FQUEUEELEMENT_HPP_
#pragma once
#include <string>
#include <dpp/dpp.h>
@@ -7,11 +6,8 @@ struct FQueueElement {
std::string URL;
std::string title;
std::string description;
std::string FileName;
std::string fileName;
std::string thumbnail;
std::string duration;
dpp::snowflake guild_id;
dpp::embed embed;
};
#endif
};

33
include/MusicQueue.hpp Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#include <list>
#include <FQueueElement.hpp>
struct FMusicQueueID {
dpp::snowflake guild_id;
uint32_t shard_id;
};
class MusicQueue {
public:
MusicQueue(FMusicQueueID id);
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(std::shared_ptr<dpp::cluster> botCluster);
void markerCallback(std::shared_ptr<dpp::cluster> botCluster);
bool repeat;
private:
std::list<struct FQueueElement> queue;
std::mutex mutex;
std::mutex playMutex;
FMusicQueueID id;
bool queuePlaying;
};

View File

@@ -1,40 +1,42 @@
#include <Bot.hpp>
#include <Commands/CommandType.hpp>
IBot::IBot(std::string Token, int TotalShard) {
BotCluster = std::make_shared<dpp::cluster>(Token, dpp::i_default_intents, TotalShard);
BotCluster->on_log(dpp::utility::cout_logger());
IBot::IBot(std::string token, int totalShard)
{
botCluster = std::make_shared<dpp::cluster>(token, dpp::i_default_intents, totalShard);
botCluster->on_log(dpp::utility::cout_logger());
BotCluster->on_slashcommand([this](const dpp::slashcommand_t& Event) {OnCommand(Event);});
BotCluster->on_ready([this](const dpp::ready_t& Event) {OnReady(Event);});
botCluster->on_slashcommand([this](const dpp::slashcommand_t& event){onCommand(event);});
botCluster->on_ready([this](const dpp::ready_t &event){onReady(event);});
}
void IBot::AddCommand(ICommand &Command) {
CommandsArray.push_back(&Command);
void IBot::onCommand(const dpp::slashcommand_t &event)
{
auto _event = event;
for (int i = 0; i < commandsArray.size(); i++) {
for (auto alias : commandsArray[i]->commandObjectVector) {
if (event.command.get_command_name() == alias.name) {
(*commandsArray[i])(_event);
}
}
}
}
void IBot::Start() {
BotCluster->start(dpp::st_wait);
}
void IBot::OnReady(const dpp::ready_t& Event) {
void IBot::onReady(const dpp::ready_t &event)
{
if (!dpp::run_once<struct register_bot_commands>())
return;
//BotCluster->global_bulk_command_delete();
for (auto command : CommandsArray) {
for (auto Alias : command->CommandObjectVector) {
BotCluster->global_command_create(Alias);
for (int i = 0; i < commandsArray.size(); i++) {
for (auto Alias : commandsArray[i]->commandObjectVector) {
botCluster->global_command_create(Alias);
}
}
}
void IBot::OnCommand(const dpp::slashcommand_t& Event) {
for (auto Command : CommandsArray) {
for (auto Alias : Command->CommandObjectVector) {
if (Event.command.get_command_name() == Alias.name) {
(*Command)(Event);
}
}
}
void IBot::start()
{
botCluster->start(dpp::st_wait);
}

View File

@@ -1,134 +1,17 @@
#include "BumbleCeepp.hpp"
#include <string>
#include <ogg/ogg.h>
#include <oggz/oggz.h>
#include <opus/opusfile.h>
#include <FQueueElement.hpp>
#include <thread>
#include <Commands/Commands.hpp>
BumbleCeepp::BumbleCeepp(std::string Token, int TotalShard) : IBot(Token, TotalShard) {
VoiceJoinedShardId = 0;
}
BumbleCeepp::BumbleCeepp(std::string token, int totalShard)
: IBot(token, totalShard)
{
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::Queue>(botCluster, &queueMap));
commandsArray.push_back(std::make_shared<commands::Skip>(botCluster, &queueMap));
commandsArray.push_back(std::make_shared<commands::Leave>(botCluster, &queueMap));
commandsArray.push_back(std::make_shared<commands::Delete>(botCluster, &queueMap));
void BumbleCeepp::enqueue(struct FQueueElement Element) {
QueueMutex.lock();
MusicQueue.push_back(Element);
QueueMutex.unlock();
}
struct FQueueElement BumbleCeepp::QueueDelete(int Index) {
QueueMutex.lock();
auto iter = MusicQueue.begin();
std::advance(iter, Index);
auto ReturnValue = *iter;
MusicQueue.erase(iter);
QueueMutex.unlock();
return ReturnValue;
}
void BumbleCeepp::QueuePlay(){
if (QueuePlaying) {
return;
}
QueuePlaying = true;
std::thread T1([this] (){
dpp::discord_client* JoinedShared = BotCluster->get_shard(VoiceJoinedShardId);
if (!JoinedShared) {
return;
}
while (!MusicQueue.empty()) {
QueueMutex.lock();
FQueueElement Music = MusicQueue.front();
QueueMutex.unlock();
dpp::voiceconn* v = JoinedShared->get_voice(Music.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
std::cout << "not in voicechat. quit musicplay";
return;
}
/* load the audio file with oggz */
OGGZ *track_og = oggz_open(("Music/" + std::string(Music.FileName.c_str()) + ".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);
std::cout << "audio sending complete\n";
int n = 0;
while(v->voiceclient->is_playing()) {}
QueueMutex.lock();
if (MusicQueue.empty()) {
QueueMutex.unlock();
break;
}
MusicQueue.pop_front();
QueueMutex.unlock();
if (Repeat) {
QueueMutex.lock();
MusicQueue.push_back(Music);
QueueMutex.unlock();
}
}
QueuePlaying = false;
std::cout << "Queue ended\n";
});
T1.detach();
}
void BumbleCeepp::OnCommand(const dpp::slashcommand_t& Event) {
for (auto Command : CommandsArray) {
for (auto Alias : Command->CommandObjectVector) {
if (Event.command.get_command_name() == Alias.name) {
(*Command)(Event);
(*Command)(MusicQueue, Event);
}
}
}
std::cout << "Command added.\n";
}

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;
}

1
src/Music/Archive Normal file
View File

@@ -0,0 +1 @@
OSeStDEAZJQ

File diff suppressed because one or more lines are too long

BIN
src/Music/OSeStDEAZJQ.ogg Normal file

Binary file not shown.

214
src/MusicQueue.cpp Normal file
View File

@@ -0,0 +1,214 @@
#include <MusicQueue.hpp>
#include <ogg/ogg.h>
#include <oggz/oggz.h>
#include <opus/opusfile.h>
#include <thread>
MusicQueue::MusicQueue(FMusicQueueID id)
{
this->id = id;
repeat = false;
queuePlaying = false;
}
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(std::shared_ptr<dpp::cluster> botCluster)
{
std::cout << "Music play started\n";
dpp::discord_client* joinedShard = botCluster->get_shard(id.shard_id);
if (!joinedShard)
{
std::cout << "No shard\n";
queuePlaying = false;
return;
}
if (empty())
{
std::cout << "Queue ended\n";
queuePlaying = false;
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";
queuePlaying = false;
return;
}
/* 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");
queuePlaying = false;
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";
}
void MusicQueue::play(std::shared_ptr<dpp::cluster> botCluster)
{
playMutex.lock();
if (queuePlaying)
{
std::cout << "Already Playing\n";
playMutex.unlock();
return;
}
queuePlaying = true;
playMutex.unlock();
dpp::discord_client* joinedShard = botCluster->get_shard(id.shard_id);
if (!joinedShard)
{
std::cout << "No shard\n";
queuePlaying = false;
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";
queuePlaying = false;
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);
}

View File

@@ -1,6 +1,6 @@
#include <BumbleCeepp.hpp>
#include <Commands.hpp>
#include <dpp/nlohmann/json.hpp>
#include <memory>
using json = nlohmann::json;
@@ -9,23 +9,9 @@ int main() {
std::ifstream configfile("config.json");
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);
Commands::Play Command1(BumbleBee);
Commands::Repeat Command2(BumbleBee);
Commands::Queue Command3(BumbleBee);
Commands::Skip Command4(BumbleBee);
Commands::Leave Command5(BumbleBee);
Commands::Delete Command6(BumbleBee);
BumbleBee->AddCommand(Command1);
BumbleBee->AddCommand(Command2);
BumbleBee->AddCommand(Command3);
BumbleBee->AddCommand(Command4);
BumbleBee->AddCommand(Command5);
BumbleBee->AddCommand(Command6);
BumbleBee->Start();
bumbleBee->start();
return 0;
}

1
src/out Normal file

File diff suppressed because one or more lines are too long

47
src/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")