Skip to content

Commit

Permalink
[Fix]: Database issue
Browse files Browse the repository at this point in the history
  • Loading branch information
harshfeudal committed Sep 23, 2023
1 parent 3326637 commit 3a9a8e5
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ aux_source_directory(src/database DATABASE)
aux_source_directory(src/commands COMMANDS)
aux_source_directory(src/commands/slash/cmd_common COMMON_CMDS)
aux_source_directory(src/commands/slash/cmd_moderation MODERATION_CMDS)
aux_source_directory(src/commands/slash/cmd_configure CONFIG_CMDS)

add_executable(${PROJECT_NAME}
${MAINSRC}
${COMMANDS}
${COMMON_CMDS}
${MODERATION_CMDS}
${CONFIG_CMDS}
${CORES}
${DATABASE}
)
Expand Down
11 changes: 9 additions & 2 deletions languages.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
{
"en-us":
{
"title": "",
"description": ""
"title": "Latency result",
"websocket": "Average Websocket Ping",
"roundtrip": "Roundtrip latency"
},
"ja-jp":
{
"title": "レイテンシの結果",
"websocket": "平均 Websocket Ping",
"roundtrip": "往復遅延"
}
}
}
1 change: 1 addition & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ project "Harshie"
"src/**.cpp",
"src/commands/slash/**.h",
"src/commands/slash/cmd_common/**.cpp",
"src/commands/slash/cmd_configure/**.cpp",
"src/cores/**.h",
"src/cores/**.cpp",
"src/database/**.h",
Expand Down
31 changes: 27 additions & 4 deletions src/commands/slash/cmd_common/ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,35 @@

void ping(dpp::cluster& client, const dpp::slashcommand_t& event)
{
const auto ws_ping = fmt::format("`{0:.02f} ms`", event.from->websocket_ping * 1000);
const auto rest_ping = fmt::format("`{0:.02f} ms`", event.from->creator->rest_ping * 1000);
const auto wsPing = fmt::format("`{0:.02f} ms`", event.from->websocket_ping * 1000);
const auto restPing = fmt::format("`{0:.02f} ms`", event.from->creator->rest_ping * 1000);

const std::string message = fmt::format("WebSocket ping: {}\nBot latency: {}", ws_ping, rest_ping);
HarshieDatabase& database = HarshieDatabase::getInstance();

auto recordUserID = fmt::format("'{}'", event.command.usr.id);
auto searchUser = database.findRecord("language_config", "id=" + recordUserID);

std::string selectLanguage = "en-us";
if (searchUser)
selectLanguage = database.exportData("language", "language_config", "id=" + recordUserID);

json& languagesJSON = HarshieLanguages::getInstance().getLanguagesJSON();
auto findDetails = languagesJSON["PING"][selectLanguage];

auto create_embed = dpp::embed()
.set_title(findDetails["title"])
.set_color(0xabf2d3)
.add_field(findDetails["websocket"], wsPing, true)
.add_field(findDetails["roundtrip"], restPing, true)
.set_thumbnail(client.me.get_avatar_url(1024, dpp::i_webp))
.set_footer(
dpp::embed_footer()
.set_text(client.me.format_username())
.set_icon(client.me.get_avatar_url())
)
.set_timestamp(time(0));

event.reply(
dpp::message(message).set_flags(dpp::m_ephemeral)
dpp::message().add_embed(create_embed)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,37 @@

#include "../cmd_lists.h"

using json = nlohmann::json;

void config_language(dpp::cluster& client, const dpp::slashcommand_t& event)
{
const auto selectLanguage = std::get<std::string>(event.get_parameter("language"));
HarshieDatabase& database = HarshieDatabase::getInstance();

auto recordUser = fmt::format("'{}'", event.command.usr.id);
auto searchUser = database.findRecord("language_config", "id=" + recordUser);
auto recordUserID = fmt::format("'{}'", event.command.usr.id);
auto searchUser = database.findRecord("language_config", "id=" + recordUserID);

std::string setLanguage = "en-us";

if (selectLanguage == "english")
{
if (searchUser)
database.deleteData("language_config", "id=" + searchUser);
database.deleteData("language_config", "id=" + recordUserID);
}
else if (selectLanguage == "japanese")
{
if (searchUser)
database.deleteData("language_config", "id=" + searchUser);
database.deleteData("language_config", "id=" + recordUserID);

setLanguage = "ja-jp";
database.insertData("language_config", searchUser + ", '" + setLanguage + "'");
database.insertData("language_config", recordUserID + ", '" + setLanguage + "'");
}

json& languagesJSON = HarshieLanguages::getInstance().getLanguagesJSON();
auto embedDescription = languagesJSON["CONFIG_LANGUAGE"][setLanguage];
auto findDetails = languagesJSON["CONFIG_LANGUAGE"][setLanguage];

auto create_embed = dpp::embed()
.set_title(embedDescription["title"])
.set_title(findDetails["title"])
.set_color(0xabf2d3)
.set_description(embedDescription["description"])
.set_description(findDetails["description"])
.set_timestamp(time(0));

event.reply(
Expand Down
4 changes: 3 additions & 1 deletion src/cores/harshie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "harshie.h"

#include "../commands/slash/cmd_register.h"
#include "../database/db_create.h"

Harshie::Harshie()
{
Expand Down Expand Up @@ -108,5 +109,6 @@ void Harshie::HarshieOnDatabaseConnect()

void Harshie::HarshieOnDatabaseCreate()
{

HarshieCreateDatabase createDatabase;
createDatabase.languageData();
}
50 changes: 34 additions & 16 deletions src/database/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,52 +47,70 @@ 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);

return status == PGRES_COMMAND_OK;
if (PQresultStatus(result) != PGRES_COMMAND_OK)
{
PQclear(result);
return false;
}

PQclear(result);
return true;
}

bool HarshieDatabase::insertData(const std::string& tableName, const std::string& values)
{
std::string query = "INSERT INTO " + tableName + " VALUES (" + values + ");";
PGresult* result = PQexec(connection, query.c_str());
ExecStatusType status = PQresultStatus(result);
PQclear(result);

return status == PGRES_COMMAND_OK;
if (PQresultStatus(result) != PGRES_COMMAND_OK)
{
PQclear(result);
return false;
}

PQclear(result);
return true;
}

bool HarshieDatabase::deleteData(const std::string& tableName, const std::string& condition)
{
std::string query = "DELETE FROM " + tableName + " WHERE " + condition + ";";
PGresult* result = PQexec(connection, query.c_str());
ExecStatusType status = PQresultStatus(result);
PQclear(result);

return status == PGRES_COMMAND_OK;
if (PQresultStatus(result) != PGRES_COMMAND_OK)
{
PQclear(result);
return false;
}

PQclear(result);
return true;
}

bool HarshieDatabase::findRecord(const std::string& tableName, const std::string& searchCondition)
{
std::string query = "SELECT * FROM " + tableName + " WHERE " + searchCondition + ";";
PGresult* result = PQexec(connection, query.c_str());

if (PQresultStatus(result) != PGRES_TUPLES_OK)
if (PQresultStatus(result) != PGRES_TUPLES_OK)
{
std::cerr << "Query failed: " << PQerrorMessage(connection) << std::endl;
PQclear(result);

PQfinish(connection);
return false;
}

return true;
int numRows = PQntuples(result);
PQclear(result);

if (numRows > 0)
return true;
else
return false;
}

std::string HarshieDatabase::selectData(const std::string& targetColumn, const std::string& tableName, const std::string& searchCondition)
std::string HarshieDatabase::exportData(const std::string& targetColumn, const std::string& tableName, const std::string& searchCondition)
{
std::string query = "SELECT " + targetColumn + " FROM " + tableName + " WHERE " + searchCondition + ";";
PGresult* result = PQexec(connection, query.c_str());
Expand Down
2 changes: 1 addition & 1 deletion src/database/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class HarshieDatabase
bool insertData(const std::string& tableName, const std::string& values);
bool deleteData(const std::string& tableName, const std::string& condition);
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);
std::string exportData(const std::string& targetColumn, const std::string& tableName, const std::string& searchCondition);

HarshieDatabase(const HarshieDatabase&) = delete;
HarshieDatabase(HarshieDatabase&&) = delete;
Expand Down
2 changes: 0 additions & 2 deletions src/database/db_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ void HarshieCreateDatabase::languageData()
fmt::print("[{}] Failed to create language_config table\n", dpp::utility::current_date_time());
}
}

// To be continue ...

0 comments on commit 3a9a8e5

Please sign in to comment.