Skip to content

Commit

Permalink
refactor to use getters and setters. lazy load data from api
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkrissym committed Nov 1, 2023
1 parent 1a2e116 commit aff25df
Show file tree
Hide file tree
Showing 56 changed files with 1,079 additions and 789 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 0
7 changes: 7 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
Checks: 'clang-analyzer-*,cppcoreguidelines-*,modernize-*,bugprone-*,performance-*,readability-non-const-parameter,misc-const-correctness,misc-use-anonymous-namespace,google-explicit-constructor,-modernize-use-trailing-return-type,-bugprone-exception-escape,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-avoid-magic-numbers,-bugprone-easily-swappable-parameters'
WarningsAsErrors: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
FormatStyle: none
...
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.dockerignore
.cache/
.travis.yml
.git/
.git*
Expand Down
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EditorConfig is awesome: http://EditorConfig.org

root = true

[*]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
trim_trailing_whitespace = false
74 changes: 11 additions & 63 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,63 +1,11 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
# Set the default behavior for all files.
* text=auto eol=lf

# Normalized and converts to native line endings on checkout.
*.c text
*.cc text
*.cxx
*.cpp text
*.h text
*.hxx text
*.hpp text
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,4 @@ __pycache__/
# Custom
/test_bot/test_bot
docker-compose.yml
.cache/
51 changes: 41 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required( VERSION 3.16 )
cmake_minimum_required( VERSION 3.16.0...3.27.6 )

project( discord_cpp )

Expand All @@ -8,27 +8,60 @@ set( CMAKE_CXX_STANDARD_REQUIRED True )
set( JSON_ImplicitConversions OFF )

include(FetchContent)
Set(FETCHCONTENT_QUIET FALSE)

FetchContent_Declare(certify GIT_REPOSITORY https://github.com/djarek/certify)
FetchContent_GetProperties(certify)
if(NOT certify_POPULATED)
FetchContent_Populate(certify)
endif()

set(BOOST_INCLUDE_LIBRARIES thread filesystem system process asio endian logic static_string)
set(BOOST_ENABLE_CMAKE ON)

FetchContent_Declare(Boost
GIT_REPOSITORY https://github.com/boostorg/boost.git
GIT_TAG boost-1.83.0
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)

FetchContent_Declare(boost_beast
GIT_REPOSITORY https://github.com/boostorg/beast
GIT_TAG boost-1.82.0
GIT_REPOSITORY https://github.com/boostorg/beast
GIT_TAG boost-1.83.0
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)

FetchContent_Declare(nlohmann_json
GIT_REPOSITORY https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent
GIT_TAG v3.11.2
)

FetchContent_MakeAvailable(boost_beast nlohmann_json)
FetchContent_Declare(zlib
GIT_REPOSITORY https://github.com/madler/zlib.git
GIT_TAG v1.3
)

FetchContent_Declare(sodium
GIT_REPOSITORY https://github.com/jedisct1/libsodium.git
GIT_TAG 1.0.19
)


FetchContent_MakeAvailable(boost_beast nlohmann_json Boost zlib sodium)

include_directories( ${CMAKE_STAGING_PREFIX}/usr/include ${certify_SOURCE_DIR}/include)
link_directories( ${CMAKE_STAGING_PREFIX}/usr/lib )
include_directories( ${CMAKE_STAGING_PREFIX}/usr/include
${certify_SOURCE_DIR}/include
${Boost_SOURCE_DIR}
${zlib_SOURCE_DIR}
${sodium_SOURCE_DIR}
)
link_directories( ${CMAKE_STAGING_PREFIX}/usr/lib
${Boost_BINARY_DIR}
${zlib_BINARY_DIR}
${sodium_BINARY_DIR}
)

FILE(GLOB PROJECT_SOURCES Discord.C++/*.cpp)
FILE(GLOB PROJECT_HEADERS Discord.C++/*.h)
Expand All @@ -43,9 +76,7 @@ else(MSVC)
set( CMAKE_CXX_FLAGS " -Wall -Wextra -Ofast " )
endif(MSVC)

find_package(Boost COMPONENTS system filesystem REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(ZLIB REQUIRED)

if(VCPKG_TOOLCHAIN)
find_package(unofficial-sodium CONFIG REQUIRED)
Expand All @@ -60,9 +91,9 @@ if(VCPKG_TOOLCHAIN)
OpenSSL::SSL
OpenSSL::Crypto
crypt32
ZLIB::ZLIB)
zlib)
else(VCPKG_TOOLCHAIN)
target_link_libraries(${PROJECT_NAME} sodium opus Boost::system Boost::filesystem nlohmann_json::nlohmann_json ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto)
target_link_libraries(${PROJECT_NAME} sodium opus boost_beast Boost::system Boost::filesystem Boost::process Boost::asio nlohmann_json::nlohmann_json zlib OpenSSL::SSL OpenSSL::Crypto)
endif(VCPKG_TOOLCHAIN)

install(TARGETS discord_cpp DESTINATION lib)
Expand Down
12 changes: 10 additions & 2 deletions Discord.C++/Activity.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace DiscordCPP {

//move this to Presence.h when implemented?
// move this to Presence.h when implemented?
namespace DiscordStatus {
const std::string Online = "online";
const std::string DoNotDisturb = "dnd";
Expand All @@ -23,15 +23,23 @@ enum ActivityTypes {
}

class Activity {
public:
private:
std::string name;
int type;
std::string url;

public:
DLL_EXPORT Activity();
DLL_EXPORT Activity(const std::string& name, const int type, const std::string& url = "");

DLL_EXPORT json to_json();

/// @return activity name
std::string get_name() { return name; }
/// @return activity type
ActivityTypes::ActivityTypes get_type() { return ActivityTypes::ActivityTypes(type); }
/// @return activity url
std::string get_url() { return url; }
};

} // namespace DiscordCPP
6 changes: 2 additions & 4 deletions Discord.C++/ApplicationCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "ApplicationCommand.h"

DiscordCPP::ApplicationCommand::ApplicationCommand(const json& data, const std::string& token)
: DiscordCPP::DiscordObject(token) {
id = data.at("id").get<std::string>();
: DiscordCPP::DiscordObject(token, data.at("id").get<std::string>()) {
type = static_cast<DiscordCPP::ApplicationCommand::Type>(get_or_else<int>(data, "type", 1));
application_id = data.at("application_id").get<std::string>();

Expand All @@ -24,7 +23,6 @@ DiscordCPP::ApplicationCommand::ApplicationCommand(const json& data, const std::

DiscordCPP::ApplicationCommand::ApplicationCommand(const ApplicationCommand& other)
: DiscordCPP::DiscordObject(other) {
id = other.id;
type = other.type;
application_id = other.application_id;
guild_id = other.guild_id;
Expand Down Expand Up @@ -63,7 +61,7 @@ void DiscordCPP::ApplicationCommand::delete_command() {
if (guild_id.has_value()) {
url += "/guilds/" + guild_id.value();
}
url += "/commands/" + id;
url += "/commands/" + get_id();

api_call(url, "DELETE");
}
43 changes: 40 additions & 3 deletions Discord.C++/ApplicationCommand.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once
#include <cmath>
#include <optional>
#include <utility>
#include <vector>

#include "ApplicationCommandOption.h"
#include "Channel.h"
#include "DiscordObject.h"
Expand All @@ -11,12 +14,15 @@ namespace DiscordCPP {

class ApplicationCommand : public DiscordObject {
public:
/// The command type.
enum Type {
CHAT_INPUT = 1,
USER = 2,
MESSAGE = 3
} type;
};

private:
/// The command type.
Type type;
/// The application id.
std::string application_id;
/// The guild of the command.
Expand All @@ -35,7 +41,8 @@ class ApplicationCommand : public DiscordObject {
/// Autoincrementing version identifier updated during substantial record changes.
std::string version;

DLL_EXPORT ApplicationCommand() {}
public:
DLL_EXPORT ApplicationCommand() = default;
DLL_EXPORT ApplicationCommand(const json& data, const std::string& token);
DLL_EXPORT ApplicationCommand(const ApplicationCommand& other);
DLL_EXPORT ~ApplicationCommand();
Expand All @@ -44,5 +51,35 @@ class ApplicationCommand : public DiscordObject {

/// Delete this command.
DLL_EXPORT void delete_command();

/// @return The application id.
DLL_EXPORT std::string get_application_id() { return application_id; }
/// @return The guild of the command.
DLL_EXPORT std::optional<std::string> get_guild_id() { return guild_id; }
/// @return The command's name.
DLL_EXPORT std::string get_name() { return name; }
/// @return The command's description. Empty string for USER and MESSAGE commands.
DLL_EXPORT std::string get_description() { return description; }
/// @return The command type.
DLL_EXPORT Type get_type() { return type; }
/// @return Parameters for the command, max of 25.
DLL_EXPORT std::vector<ApplicationCommandOption*> get_options() { return options; }
/// @return Indicates wether the command is enabled in DMs. Defaults to true.
DLL_EXPORT bool has_dm_permission() { return dm_permission; }
/// @return Autoincrementing version identifier updated during substantial record changes.
DLL_EXPORT std::string get_version() { return version; }

/// Set the guild of the command.
DLL_EXPORT void set_guild_id(std::string guild_id) { this->guild_id.emplace(guild_id); }
/// Set the command's name.
DLL_EXPORT void set_name(std::string name) { this->name = std::move(name); }
/// Set the command's description. Empty string for USER and MESSAGE commands.
DLL_EXPORT void set_description(std::string description) { this->description = std::move(description); }
/// Set the command type.
DLL_EXPORT void set_type(Type type) { this->type = type; }
/// Add parameters for the command, max of 25.
DLL_EXPORT void add_option(ApplicationCommandOption* option) { options.push_back(option); }
/// Set indicator wether the command is enabled in DMs.
DLL_EXPORT void set_dm_permission(bool dm_permission) { this->dm_permission = dm_permission; }
};
} // namespace DiscordCPP
3 changes: 2 additions & 1 deletion Discord.C++/ApplicationCommandOption.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <vector>

#include "Channel.h"
#include "static.h"

Expand Down Expand Up @@ -29,7 +30,7 @@ class ApplicationCommandOption {
/// If the parameter is required. Default is false.
bool required = false;

DLL_EXPORT ApplicationCommandOption(){};
DLL_EXPORT ApplicationCommandOption() = default;
DLL_EXPORT ApplicationCommandOption(const json& data);
DLL_EXPORT static ApplicationCommandOption* from_json(const json& data);

Expand Down
10 changes: 5 additions & 5 deletions Discord.C++/AudioSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
namespace DiscordCPP {
class AudioSource {
public:
DLL_EXPORT AudioSource(){};
DLL_EXPORT virtual ~AudioSource(){};
DLL_EXPORT AudioSource() = default;
DLL_EXPORT virtual ~AudioSource() = default;

/** @param[in] pcm_data an array to save the raw audio data
@param[in] length length of pcm_data
@return bool wether the opreation was succesfull or not
*/
@param[in] length length of pcm_data
@return bool wether the opreation was succesfull or not
*/
DLL_EXPORT virtual bool read(char* pcm_data, const int length) = 0;
};

Expand Down
Loading

0 comments on commit aff25df

Please sign in to comment.