diff --git a/.vscode/settings.json b/.vscode/settings.json index c5d5a2e..7719b5c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -84,6 +84,7 @@ "xstddef": "cpp", "xtr1common": "cpp", "xtree": "cpp", - "xutility": "cpp" + "xutility": "cpp", + "*.rh": "cpp" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index afc056c..f8b15df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ project( aux_source_directory(src MAINSRC) aux_source_directory(src/cores CORES) +aux_source_directory(src/database DATABASE) aux_source_directory(src/commands COMMANDS) aux_source_directory(src/commands/slash/cmd_common COMMON_CMDS) @@ -45,6 +46,7 @@ add_executable(${PROJECT_NAME} ${COMMON_CMDS} ${MODERATION_CMDS} ${CORES} + ${DATABASE} ) set_target_properties(${PROJECT_NAME} PROPERTIES diff --git a/premake5.lua b/premake5.lua index 8e46af0..c1a1b11 100644 --- a/premake5.lua +++ b/premake5.lua @@ -42,6 +42,8 @@ project "Harshie" "src/commands/slash/cmd_common/**.cpp", "src/cores/**.h", "src/cores/**.cpp", + "src/database/**.h", + "src/database/**.cpp", "resources/*.h", "resources/*.rc", } diff --git a/src/commands/slash/cmd_common/config-language.cpp b/src/commands/slash/cmd_common/config-language.cpp new file mode 100644 index 0000000..aee95cd --- /dev/null +++ b/src/commands/slash/cmd_common/config-language.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2023 harshfeudal + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + */ + +#include "../cmd_lists.h" + +void config_language(dpp::cluster& client, const dpp::slashcommand_t& event) +{ + const auto selectLanguage = std::get(event.get_parameter("language")); + HarshieDatabase& database = HarshieDatabase::getInstance(); + + auto recordUser = fmt::format("'{}'", event.command.usr.id); + auto searchUser = database.findRecord("language_config", "id=" + recordUser); + + std::string setLanguage = "en-us"; + + if (selectLanguage == "english") + { + if (searchUser) + database.deleteData("language_config", "id=" + searchUser); + } + else if (selectLanguage == "japanese") + { + if (searchUser) + database.deleteData("language_config", "id=" + searchUser); + + setLanguage = "ja-jp"; + database.insertData("language_config", searchUser + ", '" + setLanguage + "'"); + } +} diff --git a/src/commands/slash/cmd_layout.h b/src/commands/slash/cmd_layout.h index 41ca608..1f7b22b 100644 --- a/src/commands/slash/cmd_layout.h +++ b/src/commands/slash/cmd_layout.h @@ -33,4 +33,15 @@ inline std::map commands "Check Harshie latency", ping } }, + { + "config-language", + { + "Language configuration", config_language, + { + dpp::command_option(dpp::co_string, "language", "Your prefer language", true) + .add_choice(dpp::command_option_choice("English", std::string("english"))) + .add_choice(dpp::command_option_choice("日本語", std::string("japanese"))) + } + } + } }; diff --git a/src/commands/slash/cmd_lists.h b/src/commands/slash/cmd_lists.h index f7112e1..e5dee83 100644 --- a/src/commands/slash/cmd_lists.h +++ b/src/commands/slash/cmd_lists.h @@ -20,5 +20,8 @@ #include #include +#include "../../database/database.h" + // Common commands void ping(dpp::cluster& client, const dpp::slashcommand_t& event); +void config_language(dpp::cluster& client, const dpp::slashcommand_t& event); diff --git a/src/cores/harshie.cpp b/src/cores/harshie.cpp index c00c172..0c1c653 100644 --- a/src/cores/harshie.cpp +++ b/src/cores/harshie.cpp @@ -34,7 +34,9 @@ void Harshie::HarshieStart() HarshieOnReady(); HarshieOnSlashCmnd(); + HarshieOnDatabaseConnect(); + HarshieOnDatabaseCreate(); client->start(dpp::st_wait); } @@ -51,7 +53,7 @@ void Harshie::HarshieOnReady() void Harshie::HarshieActivites(const dpp::ready_t& event) { client->set_presence(dpp::presence(dpp::ps_dnd, dpp::at_game, "Looking for moderation")); - fmt::print("[{}]: {} is online!\n", dpp::utility::current_date_time(), client->me.format_username()); + fmt::print("[{}] {} is online!\n", dpp::utility::current_date_time(), client->me.format_username()); } void Harshie::HarshieRegisterSlashCmnd() @@ -61,14 +63,14 @@ void Harshie::HarshieRegisterSlashCmnd() registerSlashCommand(*client); fmt::print( - "[{}]: Successfully registered application (/) commands!\n", + "[{}] Successfully registered application (/) commands!\n", dpp::utility::current_date_time() ); } catch (...) { fmt::print( - "[{}]: Fail to register application (/) commands!\n", + "[{}] Fail to register application (/) commands!\n", dpp::utility::current_date_time() ); } @@ -100,5 +102,11 @@ void Harshie::HarshieOnDatabaseConnect() databaseName, databaseHost, databasePort, databaseUser, databasePass, databaseAppName ); - HarshieDatabase database(databaseConnectStr); + HarshieDatabase& database = HarshieDatabase::getInstance(); + database.connectDatabase(databaseConnectStr); +} + +void Harshie::HarshieOnDatabaseCreate() +{ + } diff --git a/src/cores/harshie.h b/src/cores/harshie.h index d96e36c..9a3b7b5 100644 --- a/src/cores/harshie.h +++ b/src/cores/harshie.h @@ -29,7 +29,7 @@ #include "dotenv.h" #include "decoder.h" -#include "database.h" +#include "../database/database.h" class Harshie { public: @@ -44,6 +44,7 @@ class Harshie { void HarshieOnReady(); void HarshieOnSlashCmnd(); void HarshieOnDatabaseConnect(); + void HarshieOnDatabaseCreate(); void HarshieActivites(const dpp::ready_t& event); void HarshieRegisterSlashCmnd(); diff --git a/src/cores/database.cpp b/src/database/database.cpp similarity index 75% rename from src/cores/database.cpp rename to src/database/database.cpp index bb76e6d..2326b08 100644 --- a/src/cores/database.cpp +++ b/src/database/database.cpp @@ -23,19 +23,21 @@ #include "database.h" -HarshieDatabase::HarshieDatabase(const std::string& connectionString) +HarshieDatabase::HarshieDatabase() { } + +void HarshieDatabase::connectDatabase(const std::string& connectionString) { connection = PQconnectdb(connectionString.c_str()); if (PQstatus(connection) != CONNECTION_OK) { - const auto errorLog = fmt::format("[{}]: Failed to connect to the database!", dpp::utility::current_date_time()); + const auto errorLog = fmt::format("[{}] Failed to connect to the database!\n", dpp::utility::current_date_time()); fmt::print(errorLog); throw std::runtime_error("Connection to the database failed: " + std::string(PQerrorMessage(connection))); } else - fmt::print("[{}]: Connected to database!", dpp::utility::current_date_time()); + fmt::print("[{}] Connected to database!\n", dpp::utility::current_date_time()); } HarshieDatabase::~HarshieDatabase() @@ -45,7 +47,7 @@ HarshieDatabase::~HarshieDatabase() bool HarshieDatabase::createTable(const std::string& tableName, const std::string& columns) { - std::string query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + columns + ");"; + std::string query = "CREATE TABLE IF NOT EXISTS \"" + tableName + "\" (" + columns + ");"; PGresult* result = PQexec(connection, query.c_str()); ExecStatusType status = PQresultStatus(result); PQclear(result); @@ -73,22 +75,21 @@ bool HarshieDatabase::deleteData(const std::string& tableName, const std::string return status == PGRES_COMMAND_OK; } -int HarshieDatabase::getCount(const std::string& tableName, const std::string& searchCondition) +bool HarshieDatabase::findRecord(const std::string& tableName, const std::string& searchCondition) { - std::string query = "SELECT COUNT(*) FROM " + tableName + " WHERE " + searchCondition + ";"; + std::string query = "SELECT * FROM " + tableName + " WHERE " + searchCondition + ";"; PGresult* result = PQexec(connection, query.c_str()); - if (PQresultStatus(result) == PGRES_TUPLES_OK) - { - int count = atoi(PQgetvalue(result, 0, 0)); - PQclear(result); - return count; - } - else + if (PQresultStatus(result) != PGRES_TUPLES_OK) { + std::cerr << "Query failed: " << PQerrorMessage(connection) << std::endl; PQclear(result); - throw std::runtime_error("Error executing query: " + query); + + PQfinish(connection); + return false; } + + return true; } std::string HarshieDatabase::selectData(const std::string& targetColumn, const std::string& tableName, const std::string& searchCondition) @@ -108,3 +109,9 @@ std::string HarshieDatabase::selectData(const std::string& targetColumn, const s throw std::runtime_error("Error executing query: " + query); } } + +HarshieDatabase& HarshieDatabase::getInstance() +{ + static HarshieDatabase instance; + return instance; +} diff --git a/src/cores/database.h b/src/database/database.h similarity index 73% rename from src/cores/database.h rename to src/database/database.h index d8a3fe1..b946fa9 100644 --- a/src/cores/database.h +++ b/src/database/database.h @@ -24,14 +24,22 @@ class HarshieDatabase { private: PGconn* connection; + HarshieDatabase(); public: - HarshieDatabase(const std::string& connectionString); ~HarshieDatabase(); + void connectDatabase(const std::string& connectionString); + static HarshieDatabase& getInstance(); + bool createTable(const std::string& tableName, const std::string& columns); bool insertData(const std::string& tableName, const std::string& values); bool deleteData(const std::string& tableName, const std::string& condition); - int getCount(const std::string& tableName, const std::string& searchCondition); + bool findRecord(const std::string& tableName, const std::string& searchCondition); std::string selectData(const std::string& targetColumn, const std::string& tableName, const std::string& searchCondition); + + HarshieDatabase(const HarshieDatabase&) = delete; + HarshieDatabase(HarshieDatabase&&) = delete; + HarshieDatabase& operator=(const HarshieDatabase&) = delete; + HarshieDatabase& operator=(HarshieDatabase&&) = delete; }; diff --git a/src/database/db_create.cpp b/src/database/db_create.cpp new file mode 100644 index 0000000..667026e --- /dev/null +++ b/src/database/db_create.cpp @@ -0,0 +1,14 @@ +#include "db_create.h" + +void HarshieCreateDatabase::languageData() +{ + try + { + database.createTable("language_config", "id TEXT, language TEXT"); + fmt::print("[{}] language_config table created!\n", dpp::utility::current_date_time()); + } + catch(...) + { + fmt::print("[{}] Failed to create language_config table\n", dpp::utility::current_date_time()); + } +} diff --git a/src/database/db_create.h b/src/database/db_create.h new file mode 100644 index 0000000..1949064 --- /dev/null +++ b/src/database/db_create.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +#include "database.h" + +class HarshieCreateDatabase +{ +public: + void languageData(); + +private: + HarshieDatabase& database = HarshieDatabase::getInstance(); +};