Skip to content

Commit

Permalink
Project: Workaround for approvedUsers config issue, more cooldown opt…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
DatCaptainHorse committed Aug 26, 2024
1 parent 8aad07f commit 6b6f880
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CMake builds
[Bb]uild*/
cmake-build-debug/
cmake-build*/

# clangd
.cache/
Expand Down
28 changes: 23 additions & 5 deletions Source/config.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export struct Config {
std::vector<std::string> approvedUsers = {};
std::string twitchChannel = "", refreshToken = "";
CommandCooldownType enabledCooldowns = CommandCooldownType::eGlobal;
ConfigOption<std::uint32_t> cooldownTime{5, 1, 60};
ConfigOption<std::uint32_t> cooldownGlobal{5, 1, 600};
ConfigOption<std::uint32_t> cooldownPerUser{5, 1, 600};
ConfigOption<std::uint32_t> cooldownPerCommand{5, 1, 600};
ConfigOption<std::uint32_t> maxAudioTriggers{
3, 0, 10}; //< How many audio triggers can a message cause
ConfigOption<float> audioSequenceOffset{
Expand All @@ -57,17 +59,24 @@ export struct Config {
json["notifEffectIntensity"] = notifEffectIntensity.value;
json["notifFontScale"] = notifFontScale.value;
json["globalAudioVolume"] = globalAudioVolume.value;
json["approvedUsers"] = approvedUsers;
json["twitchChannel"] = twitchChannel;
json["refreshToken"] = refreshToken;
json["enabledCooldowns"] = static_cast<std::uint8_t>(enabledCooldowns);
json["cooldownTime"] = cooldownTime.value;
json["cooldownGlobal"] = cooldownGlobal.value;
json["cooldownPerUser"] = cooldownPerUser.value;
json["cooldownPerCommand"] = cooldownPerCommand.value;
json["maxAudioTriggers"] = maxAudioTriggers.value;
json["audioSequenceOffset"] = audioSequenceOffset.value;
json["ttsVoiceSpeed"] = ttsVoiceSpeed.value;
json["ttsVoiceVolume"] = ttsVoiceVolume.value;
json["ttsVoicePitch"] = ttsVoicePitch.value;

// Approved users has to be made into comma separated string
std::string approvedUsersStr;
for (const auto &user : approvedUsers) approvedUsersStr += user + ",";

json["approvedUsers"] = approvedUsersStr;

std::ofstream file(get_config_path());
file << json.dump(4);
file.close();
Expand Down Expand Up @@ -95,18 +104,27 @@ export struct Config {
notifEffectIntensity.value = json["notifEffectIntensity"].get<float>();
notifFontScale.value = json["notifFontScale"].get<float>();
globalAudioVolume.value = json["globalAudioVolume"].get<float>();
//approvedUsers = json["approvedUsers"].get<std::vector<std::string>>();
twitchChannel = json["twitchChannel"].get<std::string>();
refreshToken = json["refreshToken"].get<std::string>();
enabledCooldowns =
static_cast<CommandCooldownType>(json["enabledCooldowns"].get<std::uint8_t>());
cooldownTime.value = json["cooldownTime"].get<std::uint32_t>();
cooldownGlobal.value = json["cooldownGlobal"].get<std::uint32_t>();
cooldownPerUser.value = json["cooldownPerUser"].get<std::uint32_t>();
cooldownPerCommand.value = json["cooldownPerCommand"].get<std::uint32_t>();
maxAudioTriggers.value = json["maxAudioTriggers"].get<std::uint32_t>();
audioSequenceOffset.value = json["audioSequenceOffset"].get<float>();
ttsVoiceSpeed.value = json["ttsVoiceSpeed"].get<float>();
ttsVoiceVolume.value = json["ttsVoiceVolume"].get<float>();
ttsVoicePitch.value = json["ttsVoicePitch"].get<float>();

// Approved users has to be made into vector from comma separated string
const auto approvedUsersStr = json["approvedUsers"].get<std::string>();
const auto splitted = split_string(approvedUsersStr, ",");
approvedUsers.clear();
for (const auto &user : splitted) {
if (!user.empty()) approvedUsers.push_back(user);
}

return Result();
}

Expand Down
32 changes: 22 additions & 10 deletions Source/gui.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,32 @@ public:
reinterpret_cast<unsigned int *>(&global_config.enabledCooldowns),
static_cast<unsigned int>(CommandCooldownType::eGlobal));
ImGui::SameLine();
ImGui::BeginDisabled(~global_config.enabledCooldowns & CommandCooldownType::eGlobal);
{
ImGui::InputScalar("##cooldownGlobal", ImGuiDataType_U32,
&global_config.cooldownGlobal.value);
}
ImGui::EndDisabled();

ImGui::CheckboxFlags("Per User",
reinterpret_cast<unsigned int *>(&global_config.enabledCooldowns),
static_cast<unsigned int>(CommandCooldownType::ePerUser));
ImGui::SameLine();
ImGui::BeginDisabled(~global_config.enabledCooldowns & CommandCooldownType::ePerUser);
{
ImGui::InputScalar("##cooldownPerUser", ImGuiDataType_U32,
&global_config.cooldownPerUser.value);
}
ImGui::EndDisabled();

ImGui::CheckboxFlags("Per Command",
reinterpret_cast<unsigned int *>(&global_config.enabledCooldowns),
static_cast<unsigned int>(CommandCooldownType::ePerCommand));

// Input box for cooldown time
ImGui::BeginDisabled(global_config.enabledCooldowns == CommandCooldownType::eNone);
ImGui::SameLine();
ImGui::BeginDisabled(~global_config.enabledCooldowns & CommandCooldownType::ePerCommand);
{
ImGui::Text("Cooldown time:");
ImGui::InputScalar("##cooldownTime", ImGuiDataType_U32,
&global_config.cooldownTime.value);
ImGui::InputScalar("##cooldownPerCommand", ImGuiDataType_U32,
&global_config.cooldownPerCommand.value);
}
ImGui::EndDisabled();

Expand All @@ -302,17 +314,17 @@ public:

// Modifiable list of approved users
ImGui::Text("Approved users:");
static std::array<char, 32> user_buf = {""};
static std::string user_buf = "";
if (ImGui::Button("Add")) {
if (user_buf[0] != '\0') {
if (!user_buf.empty()) {
// Add the user to the list
global_config.approvedUsers.emplace_back(user_buf.data());
global_config.approvedUsers.emplace_back(user_buf.c_str());
// Clear the input buffer
std::ranges::fill(user_buf, '\0');
}
}
ImGui::SameLine();
ImGui::InputText("##approvedUsers", user_buf.data(), user_buf.size());
ImGui::InputText("##approvedUsers", &user_buf);

// List of approved users, if empty, show warning text
if (global_config.approvedUsers.empty())
Expand Down
7 changes: 4 additions & 3 deletions Source/twitch.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private: // Handlers
if (global_config.enabledCooldowns & CommandCooldownType::eGlobal) {
const auto now = std::chrono::steady_clock::now();
if (now - m_lastCommandTime <
std::chrono::seconds(global_config.cooldownTime.value))
std::chrono::seconds(global_config.cooldownGlobal.value))
return Result();

m_lastCommandTime = now;
Expand All @@ -175,7 +175,7 @@ private: // Handlers
if (global_users.contains(user)) {
if (const auto now = std::chrono::steady_clock::now();
now - global_users[user]->lastMessage.time <
std::chrono::seconds(global_config.cooldownTime.value) &&
std::chrono::seconds(global_config.cooldownPerUser.value) &&
!global_users[user]->bypassCooldown)
return Result();
}
Expand All @@ -187,7 +187,8 @@ private: // Handlers
const auto cmdLastExec = CommandHandler::get_last_executed_time(
CommandHandler::get_command_key(extractedCommand));
if (const auto now = std::chrono::steady_clock::now();
now - cmdLastExec < std::chrono::seconds(global_config.cooldownTime.value))
now - cmdLastExec <
std::chrono::seconds(global_config.cooldownPerCommand.value))
return Result();
}

Expand Down
1 change: 1 addition & 0 deletions cmake/modules/FetchDeps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ FetchContent_Declare(
GIT_TAG "v1.90.4-docking"
PATCH_COMMAND ${IMGUI_PATCH}
UPDATE_DISCONNECTED 1
OVERRIDE_FIND_PACKAGE
)

# Fetch glfw
Expand Down

0 comments on commit 6b6f880

Please sign in to comment.