Skip to content

Commit

Permalink
[Constructing]: /ban using server language
Browse files Browse the repository at this point in the history
- It is currently bug, I'm investigating
  • Loading branch information
harshfeudal committed Oct 18, 2023
1 parent b4a98b6 commit f45c67b
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 70 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ endif()
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/.env $<TARGET_FILE_DIR:${PROJECT_NAME}>
)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/languages.json $<TARGET_FILE_DIR:${PROJECT_NAME}>
)
111 changes: 90 additions & 21 deletions languages.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"PING":
"PING":
{
"en-us":
{
"title": "Latency result",
"websocket": "Average Websocket Ping",
"websocket": "Average WebSocket Ping",
"roundtrip": "Roundtrip latency"
},
"ja-jp":
Expand All @@ -13,43 +13,48 @@
"websocket": "平均 WebSocket ピング",
"roundtrip": "ラウンドトリップ遅延"
},
"zn-cn":
"zn-cn":
{
"title": "延迟结果",
"websocket": "平均 WebSocket 延迟",
"roundtrip": "往返延迟"
}
},
"CONFIG_LANGUAGE":
"CONFIG_LANGUAGE":
{
"en-us":
{
"title": "Success!",
"description": "Language has been set to `English - United State`."
"description": "Language has been set to `English - United States`."
},
"ja-jp":
"ja-jp":
{
"title": "成功しました!",
"description": "言語が `日本語` に設定されました。"
},
"zn-cn":
{
"title": "成功!",
"description": "语言已设置为 `简体中文`。"
}
},
"CONFIG_SERVER":
"CONFIG_SERVER":
{
"en-us":
{
"title": "Server Configuration",
"name-langs": "Server Language",
"description-langs": "Select server language for the bot when sending.",
"select-menu":
"select-menu":
{
"languages":
"languages":
{
"title": "Select language",
"description": "Please select your server prefer language to let the bot send.",
"language-menu":
"description": "Please select your server's preferred language to let the bot send.",
"language-menu":
{
"title": "Success!",
"description": "Server language has been set to `English - United State`."
"description": "Server language has been set to `English - United States`."
}
}
}
Expand All @@ -59,40 +64,104 @@
"title": "サーバー設定",
"name-langs": "サーバー言語",
"description-langs": "送信時のボットのサーバー言語を選択してください。",
"select-menu":
"select-menu":
{
"languages":
"languages":
{
"title": "言語を選択してください。",
"description": "ボットが送信するためのサーバーの優先言語を選択してください。",
"language-menu":
"language-menu":
{
"title": "成功しました!",
"title": "Success!",
"description": "サーバーの言語が日本語に設定されました。"
}
}
}
},
"zn-cn":
{
"title": "服务器配置",
"name-langs": "服务器语言",
"description-langs": "发送时选择机器人的服务器语言。",
"select-menu":
{
"languages":
{
"title": "选择语言",
"description": "请选择您服务器的首选语言,以便让机器人发送。",
"language-menu":
{
"title": "成功!",
"description": "服务器语言已设置为 `简体中文`。"
}
}
}
}
},
"BAN":
"BAN":
{
"en-us":
{
"ban": "ban"
"server-language":
{
"ban": "Ban",
"no-reason": "No reason provided",
"is-banned-before": "User `{}` has been banned before",
"not-in-server": "Cannot find `{}` in this server, but has been banned anyway.",
"description": "User `{}` has been banned!"
}
},
"zn-cn":
{
"server-language":
{
"ban": "封禁",
"no-reason": "未提供理由"
}
}
},
"VALIDATION":
"VALIDATION":
{
"en-us":
{
"self-permission": "You have no permission to {} members.",
"client-permission": "I have no permission to {} members.",
"owner-restriction": "Owner is immune!",
"action-failed": "Action failed!",
"client-immuned": "I am immuned with myself.",
"client-no-admin": "I have no administration permision to {} that user.",
"client-immuned": "I am immune to myself.",
"client-no-admin": "I have no administration permission to {} that user.",
"user-no-admin": "You cannot {} a user that has administration permission.",
"cannot-ban": "Cannot {} `{}` because their role is higher."
},
"ja-jp":
{
"self-permission": "メンバーを{}する権限がありません。",
"client-permission": "私はメンバーを{}する権限がありません。",
"owner-restriction": "オーナーは免疫です!",
"action-failed": "アクションに失敗しました!",
"client-immuned": "私は自分自身に免疫があります。",
"client-no-admin": "そのユーザーに{}するための管理権限がありません。",
"user-no-admin": "管理権限を持つユーザーを{}することはできません。",
"cannot-ban": "役職が上位であるため、`{}`を{}できません。"
},
"zn-cn":
{
"self-permission": "您没有权限{}成员。",
"client-permission": "我没有权限{}成员。",
"owner-restriction": "所有者免疫!",
"action-failed": "操作失败!",
"client-immuned": "我对自己免疫。",
"client-no-admin": "我没有{}该用户的管理权限。",
"user-no-admin": "您不能{}具有管理权限的用户。",
"cannot-ban": "无法{} `{}`,因为其角色较高。"
}
},
"DECORATION":
{
"en-us":
{
"success": "Success!",
"error": "Error!"
}
}
}
48 changes: 24 additions & 24 deletions src/commands/slash/cmd_configure/config-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,33 @@ void config_server(dpp::cluster& client, const dpp::slashcommand_t& event)
auto findDetails = languagesJSON["CONFIG_SERVER"][setLanguage];

auto create_embed = dpp::embed()
.set_title(findDetails["title"])
.set_color(0xabf2d3)
.set_timestamp(time(0))
.set_title(findDetails["title"])
.set_color(0xabf2d3)
.set_timestamp(time(0))
.add_field(findDetails["name-langs"], findDetails["description-langs"]);

event.reply(
dpp::message().add_embed(create_embed).add_component(
dpp::component().add_component(
dpp::component()
.set_type(dpp::cot_selectmenu)
.set_placeholder("Server Configuration")
.add_select_option(
dpp::select_option(
"Server Languages",
"languages",
"Select server language for the bot when sending."
)
)
.add_select_option(
dpp::select_option(
"Moderation",
"moderation",
"Moderation configuration."
)
)
.set_id("server_config")
auto serverConfigSelection = dpp::component().add_component(
dpp::component()
.set_type(dpp::cot_selectmenu)
.set_placeholder("Server Configuration")
.add_select_option(
dpp::select_option(
"Server Languages",
"languages",
"Select server language for the bot when sending."
)
)
.add_select_option(
dpp::select_option(
"Moderation",
"moderation",
"Moderation configuration."
)
)
.set_id("server_config")
);

event.reply(
dpp::message().add_embed(create_embed).add_component(serverConfigSelection)
);
}
53 changes: 53 additions & 0 deletions src/commands/slash/cmd_decoration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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/.
*/

#pragma once

#include <string>

#include <dpp/dpp.h>
#include <nlohmann/json.hpp>

#include "../../cores/languages.h"

inline void sendSucessfulEmbed(std::string selectLanguage, std::string description, const dpp::slashcommand_t& event)
{
json& languagesJSON = HarshieLanguages::getInstance().getLanguagesJSON();
auto findDetails = languagesJSON["DECORATION"][selectLanguage];

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

const auto message = dpp::message().add_embed(create_embed);
event.reply(message);
}

inline void sendErrorEmbed(std::string selectLanguage, std::string description, const dpp::slashcommand_t& event)
{
json& languagesJSON = HarshieLanguages::getInstance().getLanguagesJSON();
auto findDetails = languagesJSON["DECORATION"][selectLanguage];

auto create_embed = dpp::embed()
.set_color(0x8c7cff)
.set_title(findDetails["error"])
.set_description(description);

const auto message = dpp::message().add_embed(create_embed).set_flags(dpp::m_ephemeral);
event.reply(message);
}
27 changes: 20 additions & 7 deletions src/commands/slash/cmd_moderation/ban.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

#include "../cmd_lists.h"
#include "../cmd_validation.h"

// Under-construction!
#include "../cmd_decoration.h"

void ban(dpp::cluster& client, const dpp::slashcommand_t& event)
{
Expand All @@ -28,20 +27,26 @@ void ban(dpp::cluster& client, const dpp::slashcommand_t& event)
const auto getGuild = dpp::find_guild(guildID);
const auto getUser = getGuild->members.find(userID);

std::string selectLanguage = "en-us";
HarshieDatabase& database = HarshieDatabase::getInstance();
std::string selectLanguage = database.getSelectLanguage(event.command.usr.id, "language_config", "language");

auto recordServerID = fmt::format("'{}'", event.command.guild_id);
auto searchServer = database.findRecord("server_config", "id=" + recordServerID);

if (searchServer)
selectLanguage = database.exportData("server_language", "server_config", "id=" + recordServerID);

json& languagesJSON = HarshieLanguages::getInstance().getLanguagesJSON();
auto findDetails = languagesJSON["BAN"][selectLanguage];
auto findBanDetails = languagesJSON["BAN"][selectLanguage]["server-language"];

const auto isValidate = cmndValidation(client, event, userID, dpp::p_ban_members, findDetails["ban"]);
const auto isValidate = cmndValidation(client, event, userID, dpp::p_ban_members, findBanDetails["ban"]);

if (!std::get<bool>(isValidate))
{
const auto reasonParam = event.get_parameter("reason");
const auto reason = std::holds_alternative<std::string>(reasonParam)
? std::get<std::string>(reasonParam)
: "No reason provided";
: findBanDetails["no-reason"];

dpp::user user = client.user_get_sync(userID);
bool isBannedBefore = false;
Expand All @@ -50,6 +55,9 @@ void ban(dpp::cluster& client, const dpp::slashcommand_t& event)
{
client.guild_get_ban_sync(guildID, userID);
isBannedBefore = true;

std::string description = fmt::format(findBanDetails["is-banned-before"], user.format_username());
sendErrorEmbed(selectLanguage, description, event);
}
catch (...) {}

Expand All @@ -58,7 +66,12 @@ void ban(dpp::cluster& client, const dpp::slashcommand_t& event)
client.set_audit_reason(reason);
client.guild_ban_add(guildID, userID);

if (getUser == getGuild->members.end()) {}
std::string description = fmt::format(findBanDetails["description"], user.format_username());

if (getUser == getGuild->members.end())
description = fmt::format(findBanDetails["not-in-server"], user.format_username());

sendSucessfulEmbed(selectLanguage, description, event);
}
}
}
4 changes: 2 additions & 2 deletions src/cores/harshie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

#include "harshie.h"

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

Harshie::Harshie()
{
Expand Down Expand Up @@ -121,7 +121,7 @@ void Harshie::HarshieOnSelectClicked()
client->on_select_click([this](const dpp::select_click_t& event)
{
selectMenuValue = event.custom_id;
HarshieServerConfig(event);
HarshieConfigServer(event);

if (event.custom_id == "server_language")
HarshieLanguageSelection(event);
Expand Down
Loading

0 comments on commit f45c67b

Please sign in to comment.