음악 큐/재생 구현 todo:음성 잔랙 없애기

This commit is contained in:
2023-12-14 02:39:12 +09:00
parent b2f40f21cf
commit 6503fd167b
21 changed files with 356 additions and 232 deletions

View File

@@ -69,6 +69,10 @@
"iomanip": "cpp",
"iosfwd": "cpp",
"limits": "cpp",
"numbers": "cpp"
"numbers": "cpp",
"cinttypes": "cpp",
"bitset": "cpp",
"set": "cpp",
"regex": "cpp"
}
}

View File

@@ -5,7 +5,8 @@ set(BOT_NAME "BumbleCee")
project(${BOT_NAME})
aux_source_directory("src" coresrc)
add_executable(${BOT_NAME} ${coresrc})
aux_source_directory("src/Commands" commands)
add_executable(${BOT_NAME} ${coresrc} ${commands})
string(ASCII 27 Esc)
@@ -74,3 +75,5 @@ else()
target_link_libraries(${BOT_NAME} dpp)
endif()
#set(CMAKE_CXX_FLAGS "-g")

Binary file not shown.

View File

@@ -1,5 +1,6 @@
#pragma once
#include "CommandType.hpp"
#ifndef _BOT_HPP_
#define _BOT_HPP_
#include <CommandType.hpp>
#include <dpp/dpp.h>
#include <memory>
@@ -10,10 +11,12 @@ public:
void AddCommand(ICommand &Command);
void Start();
std::shared_ptr<dpp::cluster> bot;
std::shared_ptr<dpp::cluster> BotCluster;
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,7 +1,10 @@
#pragma once
#include "Bot.hpp"
#ifndef _BUMBLECEEPP_HPP_
#define _BUMBLECEEPP_HPP_
#include <string>
#include <list>
#include <mutex>
#include <Bot.hpp>
#include <dpp/dpp.h>
class BumbleCeepp : public IBot {
public:
@@ -9,12 +12,19 @@ public:
static BumbleCeepp Instance(Token);
return &Instance;
}
void enqueue(std::string);
std::string dequeue();
void enqueue(struct FQueueElement Element);
void QueuePlay();
uint32_t VoiceJoinedShardId;
protected:
std::list<std::string> MusicQueue;
private:
BumbleCeepp(std::string Token);
void OnCommand(const dpp::slashcommand_t& Event);
};
std::list<struct FQueueElement> MusicQueue;
std::mutex QueueMutex;
};
#endif

View File

@@ -1,12 +1,16 @@
#pragma once
#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<std::string>& MusicQueue, 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

@@ -1,4 +1,5 @@
#pragma once
#include "Commands/Play.hpp"
#include "Commands/Queue.hpp"
#include "Commands/Join.hpp"
#ifndef _COMMANDS_HPP_
#define _COMMANDS_HPP_
#include <Commands/Play.hpp>
#endif

View File

@@ -1,10 +0,0 @@
#pragma once
#include "../CommandType.hpp"
class Join : public ICommand {
public:
Join(dpp::snowflake Id);
void operator()(const dpp::slashcommand_t& Event) {}
void operator()(std::list<std::string>& MusicQueue, const dpp::slashcommand_t& Event);
};

View File

@@ -1,10 +1,17 @@
#pragma once
#include "../CommandType.hpp"
#ifndef _PLAY_HPP_
#define _PLAY_HPP_
#include <CommandType.hpp>
#include <BumbleCeepp.hpp>
#include <memory>
class Play : public ICommand {
public:
Play(dpp::snowflake Id);
Play(std::shared_ptr<BumbleCeepp> Bot);
void operator()(const dpp::slashcommand_t& Event) {}
void operator()(std::list<std::string>& MusicQueue, const dpp::slashcommand_t& Event);
};
void operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event);
private:
std::shared_ptr<BumbleCeepp> Bot;
};
#endif

View File

@@ -1,10 +1,17 @@
#pragma once
#include "../CommandType.hpp"
#ifndef _QUEUE_HPP_
#define _QUEUE_HPP_
#include <CommandType.hpp>
#include <BumbleCeepp.hpp>
#include <memory>
class Queue : public ICommand {
public:
Queue(dpp::snowflake Id);
Queue(std::shared_ptr<BumbleCeepp> Bot);
void operator()(const dpp::slashcommand_t& Event) {}
void operator()(std::list<std::string>& MusicQueue, const dpp::slashcommand_t& Event);
};
void operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event);
private:
std::shared_ptr<BumbleCeepp> Bot;
};
#endif

15
include/FQueueElement.hpp Normal file
View File

@@ -0,0 +1,15 @@
#ifndef _FQUEUEELEMENT_HPP_
#define _FQUEUEELEMENT_HPP_
#include <string>
#include <dpp/dpp.h>
struct FQueueElement {
std::string title;
std::string description;
std::string FileName;
std::string thumbnail;
std::string duration;
dpp::snowflake guild_id;
};
#endif

View File

@@ -1,11 +1,11 @@
#include "Bot.hpp"
#include <Bot.hpp>
IBot::IBot(std::string Token) {
bot = std::make_shared<dpp::cluster>(Token);
bot->on_log(dpp::utility::cout_logger());
BotCluster = std::make_shared<dpp::cluster>(Token);
BotCluster->on_log(dpp::utility::cout_logger());
bot->on_slashcommand([this](const dpp::slashcommand_t& Event) {OnCommand(Event);});
bot->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) {
@@ -13,18 +13,18 @@ void IBot::AddCommand(ICommand &Command) {
}
void IBot::Start() {
bot->start(dpp::st_wait);
BotCluster->start(dpp::st_wait);
}
void IBot::OnReady(const dpp::ready_t& Event) {
if (!dpp::run_once<struct register_bot_commands>())
return;
bot->global_bulk_command_delete();
//bot->global_bulk_command_delete();
for (auto command : CommandsArray) {
for (auto Alias : command->CommandObjectVector) {
bot->global_command_create(Alias);
BotCluster->global_command_create(Alias);
}
}
}

View File

@@ -1,13 +1,158 @@
#include "BumbleCeepp.hpp"
#include <string>
#include <ogg/ogg.h>
#include <opus/opusfile.h>
#include <FQueueElement.hpp>
BumbleCeepp::BumbleCeepp(std::string Token) : IBot(Token) {
VoiceJoinedShardId = 0;
}
void BumbleCeepp::enqueue(struct FQueueElement Element) {
QueueMutex.lock();
MusicQueue.push_back(Element);
QueueMutex.unlock();
}
void BumbleCeepp::QueuePlay(){
dpp::discord_client* JoinedShared = BotCluster->get_shard(VoiceJoinedShardId);
if (!JoinedShared) {
return;
}
while (!MusicQueue.empty()) {
QueueMutex.lock();
FQueueElement Music = MusicQueue.front();
MusicQueue.pop_front();
QueueMutex.unlock();
dpp::voiceconn* v = JoinedShared->get_voice(Music.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
return;
}
ogg_sync_state oy;
ogg_stream_state os;
ogg_page og;
ogg_packet op;
OpusHead header;
char *buffer;
FILE *fd;
fd = fopen((std::string(Music.FileName.c_str()) + ".ogg").c_str(), "rb");
if (!fd) {
continue;
}
fseek(fd, 0L, SEEK_END);
size_t sz = ftell(fd);
rewind(fd);
ogg_sync_init(&oy);
buffer = ogg_sync_buffer(&oy, sz);
fread(buffer, 1, sz, fd);
ogg_sync_wrote(&oy, sz);
if (ogg_sync_pageout(&oy, &og) != 1) {
fprintf(stderr,"Does not appear to be ogg stream.\n");
exit(1);
}
ogg_stream_init(&os, ogg_page_serialno(&og));
if (ogg_stream_pagein(&os,&og) < 0) {
fprintf(stderr,"Error reading initial page of ogg stream.\n");
exit(1);
}
if (ogg_stream_packetout(&os,&op) != 1) {
fprintf(stderr,"Error reading header packet of ogg stream.\n");
exit(1);
}
/* We must ensure that the ogg stream actually contains opus data */
if (!(op.bytes > 8 && !memcmp("OpusHead", op.packet, 8))) {
fprintf(stderr,"Not an ogg opus stream.\n");
exit(1);
}
/* Parse the header to get stream info */
int err = opus_head_parse(&header, op.packet, op.bytes);
if (err) {
fprintf(stderr,"Not a ogg opus stream\n");
exit(1);
}
/* Now we ensure the encoding is correct for Discord */
if (header.channel_count != 2 && header.input_sample_rate != 48000) {
fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n");
exit(1);
}
std::list<ogg_packet> OpusList;
/* Now loop though all the pages and send the packets to the vc */
while (ogg_sync_pageout(&oy, &og) == 1) {
ogg_stream_init(&os, ogg_page_serialno(&og));
if(ogg_stream_pagein(&os,&og)<0) {
fprintf(stderr,"Error reading page of Ogg bitstream data.\n");
exit(1);
}
while (ogg_stream_packetout(&os,&op) != 0) {
/* Read remaining headers */
if (op.bytes > 8 && !memcmp("OpusHead", op.packet, 8)) {
int err = opus_head_parse(&header, op.packet, op.bytes);
if (err) {
fprintf(stderr,"Not a ogg opus stream\n");
exit(1);
}
if (header.channel_count != 2 && header.input_sample_rate != 48000) {
fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n");
exit(1);
}
continue;
}
/* Skip the opus tags */
if (op.bytes > 8 && !memcmp("OpusTags", op.packet, 8))
continue;
ogg_packet D;
memcpy(&D, &op, sizeof(ogg_packet));
OpusList.push_back(D);
//v->voiceclient->send_audio_opus(op.packet, op.bytes, samples / 48);
}
}
while (!OpusList.empty()) {
ogg_packet D = OpusList.front();
OpusList.pop_front();
v->voiceclient->send_audio_opus(D.packet, D.bytes);
}
/* Cleanup */
ogg_stream_clear(&os);
ogg_sync_clear(&oy);
system(("rm -f " + std::string(Music.FileName.c_str()) + ".ogg").c_str());
}
}
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);
}
}

86
src/Commands/Play.cpp Normal file
View File

@@ -0,0 +1,86 @@
#include <Commands/Play.hpp>
#include <dpp/dpp.h>
#include <dpp/nlohmann/json.hpp>
#include <stdlib.h>
using json = nlohmann::json;
Play::Play(std::shared_ptr<BumbleCeepp> Bot) {
this->Bot = Bot;
dpp::slashcommand Command = dpp::slashcommand("play", "노래 재생", Bot->BotCluster->me.id);
Command.add_option(
dpp::command_option(dpp::co_string, "query", "링크 또는 검색어", Bot->BotCluster->me.id)
);
dpp::slashcommand Alias = dpp::slashcommand("p", "노래 재생", Bot->BotCluster->me.id);
Alias.add_option(
dpp::command_option(dpp::co_string, "query", "링크 또는 검색어", Bot->BotCluster->me.id)
);
CommandObjectVector.push_back(Command);
CommandObjectVector.push_back(Alias);
}
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"));
system(("./yt-dlp -o temp --write-info-json -f 251 " + Query).c_str());
json document;
std::ifstream infofile("temp.info.json");
infofile >> document;
system("rm -f temp.info.json");
system(("ffmpeg -i temp -c copy " + std::string(to_string(document["id"])) + ".ogg").c_str());
system("rm -f temp");
FQueueElement Data = {std::string(document["title"]),
std::string(document["uploader"]),
std::string(document["id"]),
std::string(document["thumbnail"]),
to_string(document["duration"]),
Event.command.guild_id};
Bot->enqueue(Data);
dpp::message msg(Event.command.channel_id, "큐에 다음 곡을 추가했습니다:");
msg.add_embed(dpp::embed()
.set_color(dpp::colors::sti_blue)
.set_title(document["title"])
.set_description(document["uploader"])
.set_url(Query)
.set_image(document["thumbnail"])
.add_field(
"길이",
to_string(document["duration"]),
true
));
Event.reply(msg);
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(); });
return;
}

32
src/Commands/Queue.cpp Normal file
View File

@@ -0,0 +1,32 @@
#include <Commands/Queue.hpp>
#include <iostream>
Queue::Queue(std::shared_ptr<BumbleCeepp> Bot) {
this->Bot = Bot;
dpp::slashcommand Command = dpp::slashcommand("queue", "노래 예약 큐 확인", Bot->BotCluster->me.id);
dpp::slashcommand Alias = dpp::slashcommand("q", "노래 예약 큐 확인", Bot->BotCluster->me.id);
CommandObjectVector.push_back(Command);
CommandObjectVector.push_back(Alias);
}
void Queue::operator()(std::list<FQueueElement>& MusicQueue, const dpp::slashcommand_t& Event) {
dpp::embed embed = dpp::embed()
.set_color(dpp::colors::sti_blue)
.set_title("영상 제목&링크")
.set_url("https://dpp.dev/")
.set_description("영상 설명란")
.set_image("https://dpp.dev/DPP-Logo.png")
.set_footer(
dpp::embed_footer()
.set_text("업로더 이름")
.set_icon("https://dpp.dev/DPP-Logo.png")
)
.set_timestamp(time(0));
dpp::message msg(Event.command.channel_id, "some Text");
msg.add_embed(embed);
Event.reply(msg);
}

View File

@@ -1,25 +0,0 @@
#include <Commands/Join.hpp>
#include <iostream>
#include <dpp/dpp.h>
Join::Join(dpp::snowflake Id) {
dpp::slashcommand Command = dpp::slashcommand("join", "asdf", Id);
dpp::slashcommand Alias = dpp::slashcommand("j", "asdf", Id);
CommandObjectVector.push_back(Command);
CommandObjectVector.push_back(Alias);
}
void Join::operator()(std::list<std::string>& MusicQueue, const dpp::slashcommand_t& Event) {
dpp::guild* g = dpp::find_guild(Event.command.guild_id);
/* Attempt to connect to a voice channel, returns false if we fail to connect. */
if (!g->connect_member_voice(Event.command.get_issuing_user().id)) {
Event.reply("You don't seem to be in a voice channel!");
return;
}
/* Tell the user we joined their channel. */
Event.reply("Joined your channel!");
}

View File

@@ -1,138 +0,0 @@
#include <Commands/Play.hpp>
#include <iostream>
#include <dpp/dpp.h>
#include <oggz/oggz.h>
#include <ogg/ogg.h>
#include <opus/opusfile.h>
Play::Play(dpp::snowflake Id) {
dpp::slashcommand Command = dpp::slashcommand("play", "노래 재생", Id);
Command.add_option(
dpp::command_option(dpp::co_string, "query", "링크 또는 검색어", Id)
);
dpp::slashcommand Alias = dpp::slashcommand("p", "노래 재생", Id);
Alias.add_option(
dpp::command_option(dpp::co_string, "query", "링크 또는 검색어", Id)
);
CommandObjectVector.push_back(Command);
CommandObjectVector.push_back(Alias);
}
void Play::operator()(std::list<std::string>& MusicQueue, const dpp::slashcommand_t& Event) {
if (std::holds_alternative<std::monostate>(Event.get_parameter("query"))) {
/* Get the voice channel the bot is in, in this current guild. */
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()) {
Event.reply("There was an issue with getting the voice channel. Make sure I'm in a voice channel!");
return;
}
ogg_sync_state oy;
ogg_stream_state os;
ogg_page og;
ogg_packet op;
OpusHead header;
char *buffer;
FILE *fd;
fd = fopen("../audioplayback.ogg", "rb");
fseek(fd, 0L, SEEK_END);
size_t sz = ftell(fd);
rewind(fd);
ogg_sync_init(&oy);
buffer = ogg_sync_buffer(&oy, sz);
fread(buffer, 1, sz, fd);
ogg_sync_wrote(&oy, sz);
if (ogg_sync_pageout(&oy, &og) != 1) {
fprintf(stderr,"Does not appear to be ogg stream.\n");
exit(1);
}
ogg_stream_init(&os, ogg_page_serialno(&og));
if (ogg_stream_pagein(&os,&og) < 0) {
fprintf(stderr,"Error reading initial page of ogg stream.\n");
exit(1);
}
if (ogg_stream_packetout(&os,&op) != 1) {
fprintf(stderr,"Error reading header packet of ogg stream.\n");
exit(1);
}
/* We must ensure that the ogg stream actually contains opus data */
if (!(op.bytes > 8 && !memcmp("OpusHead", op.packet, 8))) {
fprintf(stderr,"Not an ogg opus stream.\n");
exit(1);
}
/* Parse the header to get stream info */
int err = opus_head_parse(&header, op.packet, op.bytes);
if (err) {
fprintf(stderr,"Not a ogg opus stream\n");
exit(1);
}
/* Now we ensure the encoding is correct for Discord */
if (header.channel_count != 2 && header.input_sample_rate != 48000) {
fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n");
exit(1);
}
/* Now loop though all the pages and send the packets to the vc */
while (ogg_sync_pageout(&oy, &og) == 1) {
ogg_stream_init(&os, ogg_page_serialno(&og));
if(ogg_stream_pagein(&os,&og)<0) {
fprintf(stderr,"Error reading page of Ogg bitstream data.\n");
exit(1);
}
while (ogg_stream_packetout(&os,&op) != 0) {
/* Read remaining headers */
if (op.bytes > 8 && !memcmp("OpusHead", op.packet, 8)) {
int err = opus_head_parse(&header, op.packet, op.bytes);
if (err) {
fprintf(stderr,"Not a ogg opus stream\n");
exit(1);
}
if (header.channel_count != 2 && header.input_sample_rate != 48000) {
fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n");
exit(1);
}
continue;
}
/* Skip the opus tags */
if (op.bytes > 8 && !memcmp("OpusTags", op.packet, 8))
continue;
/* Send the audio */
int samples = opus_packet_get_samples_per_frame(op.packet, 48000);
v->voiceclient->send_audio_opus(op.packet, op.bytes, samples / 48);
}
}
/* Cleanup */
ogg_stream_clear(&os);
ogg_sync_clear(&oy);
Event.reply("Finished playing the audio file!");
}
}

View File

@@ -1,14 +0,0 @@
#include <Commands/Queue.hpp>
#include <iostream>
Queue::Queue(dpp::snowflake Id) {
dpp::slashcommand Command = dpp::slashcommand("queue", "노래 예약 큐 확인", Id);
dpp::slashcommand Alias = dpp::slashcommand("q", "노래 예약 큐 확인", Id);
CommandObjectVector.push_back(Command);
CommandObjectVector.push_back(Alias);
}
void Queue::operator()(std::list<std::string>& MusicQueue, const dpp::slashcommand_t& Event) {
}

BIN
src/i7p39xR0BHQ.ogg Normal file

Binary file not shown.

View File

@@ -1,6 +1,4 @@
#include <BumbleCeepp.hpp>
#include <CommandType.hpp>
#include <iostream>
#include <Commands.hpp>
#include <dpp/nlohmann/json.hpp>
@@ -11,15 +9,11 @@ int main() {
std::ifstream configfile("../config.json");
configfile >> configdocument;
std::unique_ptr<BumbleCeepp> BumbleBee(BumbleCeepp::GetInstance(configdocument["token"]));
std::shared_ptr<BumbleCeepp> BumbleBee(BumbleCeepp::GetInstance(configdocument["token"]));
Play Command1(BumbleBee->bot->me.id);
Queue Command2(BumbleBee->bot->me.id);
Join Command3(BumbleBee->bot->me.id);
Play Command1(BumbleBee);
BumbleBee->AddCommand(Command1);
BumbleBee->AddCommand(Command2);
BumbleBee->AddCommand(Command3);
BumbleBee->Start();

BIN
src/yt-dlp Executable file

Binary file not shown.