Skip to content

Commit

Permalink
initial bot parts, user registration
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Dec 13, 2023
1 parent fb8828a commit 0fcedb1
Show file tree
Hide file tree
Showing 47 changed files with 12,974 additions and 2,108 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
config.json
.vscode
60 changes: 60 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
cmake_minimum_required (VERSION 3.16)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(MySQL)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(BOT_NAME "ssod")

project(${BOT_NAME})
aux_source_directory("src" coresrc)
aux_source_directory("src/commands" coresrc)
aux_source_directory("src/3rdparty" coresrc)
add_executable(${BOT_NAME} ${coresrc})

target_precompile_headers(${BOT_NAME} INTERFACE "src/3rdparty/httplib.h")

string(ASCII 27 Esc)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

set_target_properties(${BOT_NAME} PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON
)

set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
find_package(DPP REQUIRED)

if(APPLE)
set(OPENSSL_ROOT_DIR /usr/local/opt/openssl)
find_package(OpenSSL REQUIRED)
else()
find_package(OpenSSL REQUIRED)
endif()

set(CMAKE_CXX_FLAGS "-g -O2 -Wall -Wno-unused-private-field -Wno-psabi -Wempty-body -Wignored-qualifiers -Wimplicit-fallthrough -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -Wshift-negative-value")
target_compile_definitions(${BOT_NAME} PUBLIC DPP_CORO=ON CPPHTTPLIB_OPENSSL_SUPPORT)

target_include_directories(${BOT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${DPP_INCLUDE_DIR}
)

target_link_libraries(${BOT_NAME}
fmt
spdlog
mysqlclient
crypto
ssl
sentry
CxxUrl
${CMAKE_THREAD_LIBS_INIT}
${DPP_LIBRARIES}
)

#add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
# WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
# COMMAND "../sentry-symbols.sh"
#)
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Please see the contributing guidelines for [D++](https://github.com/brainboxdotcc/DPP) for details of how to contribute.
7 changes: 7 additions & 0 deletions cmake/FindDPP.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
find_path(DPP_INCLUDE_DIR NAMES dpp/dpp.h HINTS ${DPP_ROOT_DIR})

find_library(DPP_LIBRARIES NAMES dpp "libdpp.a" HINTS ${DPP_ROOT_DIR})

include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(DPP DEFAULT_MSG DPP_LIBRARIES DPP_INCLUDE_DIR)
48 changes: 48 additions & 0 deletions cmake/FindMySQL.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# - Find mysqlclient
# Find the native MySQL includes and library
#
# MYSQL_INCLUDE_DIR - where to find mysql.h, etc.
# MYSQL_LIBRARIES - List of libraries when using MySQL.
# MYSQL_FOUND - True if MySQL found.

IF (MYSQL_INCLUDE_DIR)
# Already in cache, be silent
SET(MYSQL_FIND_QUIETLY TRUE)
ENDIF (MYSQL_INCLUDE_DIR)

FIND_PATH(MYSQL_INCLUDE_DIR mysql.h
/usr/local/include/mysql
/usr/include/mysql
)

SET(MYSQL_NAMES mysqlclient mysqlclient_r)
FIND_LIBRARY(MYSQL_LIBRARY
NAMES ${MYSQL_NAMES}
PATHS /usr/lib /usr/local/lib
PATH_SUFFIXES mysql
)

IF (MYSQL_INCLUDE_DIR AND MYSQL_LIBRARY)
SET(MYSQL_FOUND TRUE)
SET( MYSQL_LIBRARIES ${MYSQL_LIBRARY} )
ELSE (MYSQL_INCLUDE_DIR AND MYSQL_LIBRARY)
SET(MYSQL_FOUND FALSE)
SET( MYSQL_LIBRARIES )
ENDIF (MYSQL_INCLUDE_DIR AND MYSQL_LIBRARY)

IF (MYSQL_FOUND)
IF (NOT MYSQL_FIND_QUIETLY)
MESSAGE(STATUS "Found MySQL: ${MYSQL_LIBRARY}")
ENDIF (NOT MYSQL_FIND_QUIETLY)
ELSE (MYSQL_FOUND)
IF (MYSQL_FIND_REQUIRED)
MESSAGE(STATUS "Looked for MySQL libraries named ${MYSQL_NAMES}.")
MESSAGE(FATAL_ERROR "Could NOT find MySQL library")
ENDIF (MYSQL_FIND_REQUIRED)
ENDIF (MYSQL_FOUND)

MARK_AS_ADVANCED(
MYSQL_LIBRARY
MYSQL_INCLUDE_DIR
)

107 changes: 107 additions & 0 deletions include/ssod/botlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/************************************************************************************
*
* The Seven Spells Of Destruction
*
* Copyright 1993,2001,2023 Craig Edwards <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
#pragma once
#include <dpp/dpp.h>
#include <ssod/ssod.h>

/**
* @brief Represents a bot list site.
* @note As this is all static constant data, these structs are never instantiated.
* They are simply called statically.
*/
struct botlist {
/**
* @brief The name of the bot list site. Used as the key under the "botlists"
* key in the configuration file.
*/
static constexpr std::string_view name{};

/**
* @brief URL format to use, {} will be replaced with the bot's application ID.
*/
static constexpr std::string_view url{};

/**
* @brief Field name to use in JSON POST data to hold the count of servers.
* If set to an empty string, no server count will be sent.
*/
static constexpr std::string_view server_count_field{};

/**
* @brief Field name to use in JSON POST data to hold the count of shards.
* If set to an empty string, no shard count will be sent.
*
*/
static constexpr std::string_view shard_count_field{};

/**
* @brief Handle posting an update to the bot list site
* @note There is no implementation of this base struct function.
* This is to prevent accidental instantiation of the base struct.
*
* @param bot reference to D++ cluster
*/
static void post(dpp::cluster& bot);

protected:
/**
* @brief Perform the POST to the bot list website asynchronously.
* Will complete in the background, this is fire-and-forget.
* On error, an error will be logged to the logger. Should be called
* by a botlist::post() implementation.
*
* @param bot cluster reference
* @param key configuration key name
* @param url bot list url formatter
* @param count_field count field in postdata
* @param shards_field shards field in postdata
*/
static void run(dpp::cluster& bot, const std::string_view key, const std::string_view url, const std::string_view count_field, const std::string_view shards_field);
};

/**
* @brief Represents the botlist::post() function
*/
using botlist_router = auto (*)(dpp::cluster&) -> void;

/**
* @brief Represents a list of registered botlists stored in an unordered_map
*/
using registered_botlist_list = std::unordered_map<std::string_view, botlist_router>;

registered_botlist_list& get_botlist_map();

/**
* @brief Register a botlist
*
* @tparam T botlist handler struct to register
*/
template <typename T> void register_botlist()
{
auto& botlist_map = get_botlist_map();
botlist_map[T::name] = T::post;
}

/**
* @brief Request that all botlists are updated
*
* @param bot cluster reference
*/
void post_botlists(dpp::cluster &bot);
36 changes: 36 additions & 0 deletions include/ssod/botlists/discordbotlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/************************************************************************************
*
* The Seven Spells Of Destruction
*
* Copyright 1993,2001,2023 Craig Edwards <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
#pragma once
#include <ssod/ssod.h>
#include <ssod/botlist.h>

/**
* @brief discordbotlist.com
*/
struct discordbotlist : public botlist {
static constexpr std::string_view name{"discordbotlist.com"};
static constexpr std::string_view url{"https://discordbotlist.com/api/v1/bots/{}/stats"};
static constexpr std::string_view server_count_field{"guilds"};
static constexpr std::string_view shard_count_field{""};

static void post(dpp::cluster& bot) {
botlist::run(bot, discordbotlist::name, discordbotlist::url, discordbotlist::server_count_field, discordbotlist::shard_count_field);
}
};
36 changes: 36 additions & 0 deletions include/ssod/botlists/infinitybots.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/************************************************************************************
*
* The Seven Spells Of Destruction
*
* Copyright 1993,2001,2023 Craig Edwards <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
#pragma once
#include <ssod/ssod.h>
#include <ssod/botlist.h>

/**
* @brief infinity bot list
*/
struct infinitybots : public botlist {
static constexpr std::string_view name{"infinitybots.gg"};
static constexpr std::string_view url{"https://spider.infinitybots.gg/bots/stats"};
static constexpr std::string_view server_count_field{"servers"};
static constexpr std::string_view shard_count_field{"shards"};

static void post(dpp::cluster& bot) {
botlist::run(bot, infinitybots::name, infinitybots::url, infinitybots::server_count_field, infinitybots::shard_count_field);
}
};
36 changes: 36 additions & 0 deletions include/ssod/botlists/topgg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/************************************************************************************
*
* The Seven Spells Of Destruction
*
* Copyright 1993,2001,2023 Craig Edwards <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
#pragma once
#include <ssod/ssod.h>
#include <ssod/botlist.h>

/**
* @brief top.gg botlist (formerly DBL)
*/
struct topgg : public botlist {
static constexpr std::string_view name{"top.gg"};
static constexpr std::string_view url{"https://top.gg/api/bots/{}/stats"};
static constexpr std::string_view server_count_field{"server_count"};
static constexpr std::string_view shard_count_field{"shard_count"};

static void post(dpp::cluster& bot) {
botlist::run(bot, topgg::name, topgg::url, topgg::server_count_field, topgg::shard_count_field);
}
};
Loading

0 comments on commit 0fcedb1

Please sign in to comment.