| 1 | /************************************************************************************ |
| 2 | * |
| 3 | * D++, A Lightweight C++ library for Discord |
| 4 | * |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * Copyright 2021 Craig Edwards and D++ contributors |
| 7 | * (https://github.com/brainboxdotcc/DPP/graphs/contributors) |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | * you may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | ************************************************************************************/ |
| 22 | #include <dpp/dpp.h> |
| 23 | #include <iostream> |
| 24 | #include <string> |
| 25 | |
| 26 | std::string get_testdata_dir() { |
| 27 | char *env_var = getenv("TEST_DATA_DIR"); |
| 28 | return (env_var ? env_var : "../../testdata/"); |
| 29 | } |
| 30 | |
| 31 | std::vector<uint8_t> load_test_audio() { |
| 32 | std::vector<uint8_t> testaudio; |
| 33 | std::string dir = get_testdata_dir(); |
| 34 | std::ifstream input (dir + "Robot.pcm", std::ios::in|std::ios::binary|std::ios::ate); |
| 35 | if (input.is_open()) { |
| 36 | size_t testaudio_size = input.tellg(); |
| 37 | testaudio.resize(testaudio_size); |
| 38 | input.seekg(0, std::ios::beg); |
| 39 | input.read((char*)testaudio.data(), testaudio_size); |
| 40 | input.close(); |
| 41 | } |
| 42 | else { |
| 43 | std::cout << "ERROR: Can't load " + dir + "Robot.pcm\n"; |
| 44 | exit(1); |
| 45 | } |
| 46 | return testaudio; |
| 47 | } |
| 48 | |
| 49 | int main() { |
| 50 | |
| 51 | using namespace std::chrono_literals; |
| 52 | char* t = getenv("DPP_UNIT_TEST_TOKEN"); |
| 53 | if (t == nullptr || getenv("TEST_GUILD_ID") == nullptr || getenv("TEST_VC_ID") == nullptr) { |
| 54 | std::cerr << "Missing unit test environment. Set DPP_UNIT_TEST_TOKEN, TEST_GUILD_ID, and TEST_VC_ID\n"; |
| 55 | exit(1); |
| 56 | } |
| 57 | dpp::snowflake TEST_GUILD_ID(std::string(getenv("TEST_GUILD_ID"))); |
| 58 | dpp::snowflake TEST_VC_ID(std::string(getenv("TEST_VC_ID"))); |
| 59 | std::cout << "Test Guild ID: " << TEST_GUILD_ID << " Test VC ID: " << TEST_VC_ID << "\n\n"; |
| 60 | dpp::cluster dave_test(t, dpp::i_default_intents, 1, 0, 1, false, dpp::cache_policy_t{ dpp::cp_none, dpp::cp_none, dpp::cp_none, dpp::cp_none, dpp::cp_none }); |
| 61 | |
| 62 | dave_test.on_log([&](const dpp::log_t& log) { |
| 63 | std::cout << "[" << dpp::utility::current_date_time() << "] " << dpp::utility::loglevel(log.severity) << ": " << log.message << std::endl; |
| 64 | }); |
| 65 | |
| 66 | std::vector<uint8_t> testaudio = load_test_audio(); |
| 67 | |
| 68 | dave_test.on_voice_ready([&](const dpp::voice_ready_t & event) { |
| 69 | dave_test.log(dpp::ll_info, "Voice channel ready, sending audio..."); |
| 70 | dpp::discord_voice_client* v = event.voice_client; |
| 71 | if (v && v->is_ready()) { |
| 72 | v->send_audio_raw((uint16_t*)testaudio.data(), testaudio.size()); |
| 73 | } |
| 74 | dave_test.start_timer([v, &testaudio](auto) { |
| 75 | v->send_audio_raw((uint16_t*)testaudio.data(), testaudio.size()); |
| 76 | }, 15); |
| 77 | }); |
| 78 | |
| 79 | |
| 80 | dave_test.on_guild_create([&](const dpp::guild_create_t & event) { |
| 81 | if (event.created.id == TEST_GUILD_ID) { |
| 82 | dpp::discord_client* s = event.from(); |
| 83 | bool muted = false, deaf = false; |
| 84 | s->connect_voice(TEST_GUILD_ID, TEST_VC_ID, muted, deaf); |
| 85 | } |
| 86 | }); |
| 87 | dave_test.start(dpp::st_wait); |
| 88 | } |