join, play 구현

This commit is contained in:
2023-12-12 22:20:01 +09:00
parent 8b0d0bbff7
commit b2f40f21cf
26 changed files with 526 additions and 108 deletions

40
src/Bot.cpp Normal file
View File

@@ -0,0 +1,40 @@
#include "Bot.hpp"
IBot::IBot(std::string Token) {
bot = std::make_shared<dpp::cluster>(Token);
bot->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);});
}
void IBot::AddCommand(ICommand &Command) {
CommandsArray.push_back(&Command);
}
void IBot::Start() {
bot->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();
for (auto command : CommandsArray) {
for (auto Alias : command->CommandObjectVector) {
bot->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);
}
}
}
}

15
src/BumbleCeepp.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include "BumbleCeepp.hpp"
#include <string>
BumbleCeepp::BumbleCeepp(std::string Token) : IBot(Token) {
}
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)(MusicQueue, Event);
}
}
}
}

25
src/Join.cpp Normal file
View File

@@ -0,0 +1,25 @@
#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!");
}

138
src/Play.cpp Normal file
View File

@@ -0,0 +1,138 @@
#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!");
}
}

14
src/Queue.cpp Normal file
View File

@@ -0,0 +1,14 @@
#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) {
}

27
src/main.cpp Normal file
View File

@@ -0,0 +1,27 @@
#include <BumbleCeepp.hpp>
#include <CommandType.hpp>
#include <iostream>
#include <Commands.hpp>
#include <dpp/nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
json configdocument;
std::ifstream configfile("../config.json");
configfile >> configdocument;
std::unique_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);
BumbleBee->AddCommand(Command1);
BumbleBee->AddCommand(Command2);
BumbleBee->AddCommand(Command3);
BumbleBee->Start();
return 0;
}