Skip to content

Commit

Permalink
[0.1.2.356]: Fix button handler
Browse files Browse the repository at this point in the history
- Massive changes!
- Working in progress with kick command for button handle

Co-Authored-By: Harshfeudal <[email protected]>
  • Loading branch information
harshfeudal and dhtson committed Oct 23, 2022
1 parent d898728 commit 2428d59
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 102 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ These are libraries that we're using in this project:
- [Spdlog](https://github.com/gabime/spdlog) - version `1.10.0` (latest)

Bot current version:
- BETA version - `0.1.2.324` (`Release`)
- BETA version - `0.1.2.356` (`Release`)
- Stable version - `Unknown`

### 🚨 Generate and use
Expand Down Expand Up @@ -57,6 +57,10 @@ Bot current version:
- Visual Studio platform `x64`
- C++ 17 `ISO/IEC 14882`
- Please install `.dll` files on [dpp page](https://dpp.dev/) and put it near your `.exe` executable file so that it will run well
- Issue may occur: [LNK2005](https://learn.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-error-lnk2005?f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(LNK2005)%26rd%3Dtrue&view=msvc-170) from Visual Studio. To fix that, please follow these steps:
1. Open the project's **Property Pages** dialog box. For more information, see [Set compiler and build properties](https://learn.microsoft.com/en-us/cpp/build/working-with-project-properties?view=msvc-170).
2. Select the **Configuration Properties > Linker > General property page**.
3. Modify the **Force File Output** property and choose **/FORCE:MULTIPLE**. Choose **OK** or **Apply** to save your changes.

Other cases may not compile or crash occurs.

Expand Down
8 changes: 4 additions & 4 deletions Raiden Shogun.rc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <Winver.h>

#define FILEVERSION_STR "0.1.2.324"
#define PRODUCTVERSION_STR "0.1.2.324"
#define FILEVERSION_STR "0.1.2.356"
#define PRODUCTVERSION_STR "0.1.2.356"

VS_VERSION_INFO VERSIONINFO
FILEVERSION 0,1,2,324
PRODUCTVERSION 0,1,2,324
FILEVERSION 0,1,2,356
PRODUCTVERSION 0,1,2,356
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
#ifdef _DEBUG
FILEFLAGS VS_FF_DEBUG
Expand Down
63 changes: 0 additions & 63 deletions commands/buttonHandler.h

This file was deleted.

114 changes: 114 additions & 0 deletions handler/btnHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#pragma once

#include <dpp/dpp.h>

#define ID_SPACING "###"

struct Session
{
time_t created_at;
std::function<bool(const dpp::button_click_t&)> function;

uint16_t cache_duration;
Session() : created_at(time(nullptr)), cache_duration(10) {}

bool isExpired() const
{
return difftime(time(nullptr), created_at) > cache_duration * 60;
}
};

std::unordered_map<uint64_t, Session> cachedSessions;
std::shared_mutex cachedSessionsMutex;
uint64_t customIdCounter;

inline void ButtonClear()
{
std::unique_lock l(cachedSessionsMutex);
auto it = cachedSessions.begin();

while (it != cachedSessions.end())
{
if (it->second.isExpired())
{
assert(!cachedSessions.empty());
it = cachedSessions.erase(it);
}
else
{
++it;
}
}
}

inline void ButtonBind(dpp::component& component, const std::function<bool(const dpp::button_click_t&)>& function, uint16_t cache_duration = 10)
{
ButtonClear();

std::unique_lock l(cachedSessionsMutex);

bool customIdAlreadyExists;
do
{
if (customIdCounter >= UINT_LEAST64_MAX)
{
customIdCounter = 0;
}

customIdCounter++;

customIdAlreadyExists = cachedSessions.find(customIdCounter) != cachedSessions.end();
if (!customIdAlreadyExists)
{
component.custom_id = std::to_string(customIdCounter);

Session session;
session.function = function;
session.cache_duration = cache_duration;

component.custom_id += ID_SPACING + std::to_string(static_cast<long int>(session.created_at));

cachedSessions[customIdCounter] = session;
customIdAlreadyExists = false;
}
} while (customIdAlreadyExists);
}

inline void ButtonHandle(const dpp::button_click_t& event)
{
uint64_t customId;
time_t creationTimestamp;

try
{
std::string id = event.custom_id.substr(0, event.custom_id.find(ID_SPACING));
std::string creation = event.custom_id.substr(
event.custom_id.find(ID_SPACING) + std::strlen(ID_SPACING),
std::string::npos
);
customId = std::stoul(id);
creationTimestamp = std::stol(creation);
}
catch (std::out_of_range& e)
{
event.reply(dpp::message("invalid component").set_flags(dpp::m_ephemeral));
return;
}
catch (std::invalid_argument& e)
{
event.reply(dpp::message("invalid component").set_flags(dpp::m_ephemeral));
return;
}

std::unique_lock l(cachedSessionsMutex);
auto existing = cachedSessions.find(customId);

if (existing != cachedSessions.end() && existing->second.created_at == creationTimestamp && !existing->second.isExpired())
{
bool forget = existing->second.function(event);
if (forget)
{
cachedSessions.erase(existing);
}
}
}
8 changes: 4 additions & 4 deletions commands/builder.h → handler/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <map>

#include "handler.h"
#include "ping.h"
#include "kick.h"
#include "ban.h"
#include "userInfo.h"
#include "../commands/ping.h"
#include "../commands/kick.h"
#include "../commands/ban.h"
#include "../commands/userInfo.h"

void SlashCommandCreate(dpp::cluster& client);

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/ban.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <spdlog/spdlog.h>
#include <harshfeudal/shorten.h>

#include "../commands/handler.h"
#include "../handler/handler.h"
#include "../commands/ban.h"

void ban(dpp::cluster& client, const dpp::slashcommand_t& event)
Expand Down
54 changes: 29 additions & 25 deletions src/kick.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <spdlog/spdlog.h>

#include "../commands/handler.h"
#include "../handler/handler.h"
#include "../handler/btnHandler.h"
#include "../commands/kick.h"

void kick(dpp::cluster& client, const dpp::slashcommand_t& event)
Expand All @@ -24,8 +25,16 @@ void kick(dpp::cluster& client, const dpp::slashcommand_t& event)
}
}

// Working in progress ...

dpp::message k_Confirm("Do you want to kick? Press the button below to confirm");

auto k_Component = dpp::component().set_label("Kick")
.set_type(dpp::cot_button)
.set_style(dpp::cos_danger)
.set_id("k_Id");

/*
k_Confirm.add_component(
dpp::component().add_component(
dpp::component().set_label("Kick")
Expand All @@ -39,6 +48,7 @@ void kick(dpp::cluster& client, const dpp::slashcommand_t& event)
.set_id("cnl_Id")
)
);
*/

event.reply(
k_Confirm.set_flags(dpp::m_ephemeral)
Expand All @@ -48,31 +58,25 @@ void kick(dpp::cluster& client, const dpp::slashcommand_t& event)
k_Reason = "No kick reason provided";
k_Reason = std::get<std::string>(trgReason);

client.on_button_click([&client, k_Reason, tgtGuild, tgtUser](const dpp::button_click_t& event)
{
std::string kContent = fmt::format("<@{}> has been kicked!", tgtUser);
std::string cnlContent = "Cancelled request!";
client.set_audit_reason(k_Reason);
client.guild_member_kick(tgtGuild, tgtUser);

if (event.custom_id == "k_Id")
{
client.set_audit_reason(k_Reason);
client.guild_member_kick(tgtGuild, tgtUser);
std::string kContent = fmt::format("<@{}> has been kicked!", tgtUser);
std::string cnlContent = "Cancelled request!";

event.reply(
dpp::interaction_response_type::ir_update_message,
dpp::message().set_flags(dpp::m_ephemeral)
.set_content(kContent)
);
}
else if (event.custom_id == "cnl_Id")
{
event.reply(
dpp::interaction_response_type::ir_update_message,
dpp::message().set_flags(dpp::m_ephemeral)
.set_content(cnlContent)
);
}
});
/*
event.reply(
dpp::interaction_response_type::ir_update_message,
dpp::message().set_flags(dpp::m_ephemeral)
.set_content(kContent)
);
*/

// Working in progress ...
/*
event.reply(
dpp::interaction_response_type::ir_update_message,
dpp::message().set_flags(dpp::m_ephemeral)
.set_content(cnlContent)
);
*/
}
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <dpp/nlohmann/json.hpp>
#include <dpp/dpp.h>

#include "../commands/builder.h"
#include "../commands/buttonHandler.h"
#include "../handler/builder.h"
#include "../handler/btnHandler.h"

using json = nlohmann::json;

Expand Down Expand Up @@ -57,7 +57,7 @@ int main()

client.on_button_click([](const dpp::button_click_t& event)
{
Buttonhandle(event);
ButtonHandle(event);
});

client.start(false);
Expand Down
2 changes: 1 addition & 1 deletion src/ping.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <spdlog/spdlog.h>

#include "../handler/handler.h"
#include "../commands/ping.h"
#include "../commands/handler.h"

void ping(dpp::cluster& client, const dpp::slashcommand_t& event)
{
Expand Down

0 comments on commit 2428d59

Please sign in to comment.