finished discord connector node
This commit is contained in:
@@ -7,7 +7,7 @@ func _on_user_updated() -> void:
|
||||
print_debug("USER UPDATED")
|
||||
|
||||
func _on_user_connection_failed(error: String) -> void:
|
||||
push_error("User connection failed! Error: %s", error)
|
||||
push_error("User connection failed! Error: " + error)
|
||||
|
||||
func _on_user_update_failed(error: String) -> void:
|
||||
push_error("User update failed! Error: %s", error)
|
||||
push_error("User update failed! Error: " + error)
|
||||
|
@@ -43,9 +43,9 @@ text = "[center][font s=60]DiscordUtil Test"
|
||||
script = ExtResource("6_ujijw")
|
||||
|
||||
[node name="DiscordConnector" type="DiscordConnector" parent="."]
|
||||
auto_connect = true
|
||||
app_id = 1099618430065324082
|
||||
encryption_key = "H8jOL;.+F9B7&/jLb:Yr_,V'He/nRQw.wII8GE1$l_P,&YG)Yc"
|
||||
auto_connect = true
|
||||
auto_encryption_key = "nS\"#&bOk4?bFAD0h(h,5=dIp&7o#r#TGKAywD%Gd2IKa%gk0qf"
|
||||
script = ExtResource("3_h2yge")
|
||||
|
||||
[connection signal="user_connected" from="DiscordConnector" to="DiscordConnector" method="_on_user_connected"]
|
||||
|
@@ -4,19 +4,20 @@ DiscordConnector *DiscordConnector::singleton = nullptr;
|
||||
|
||||
void DiscordConnector::_bind_methods()
|
||||
{
|
||||
BIND_SET_GET(DiscordConnector, auto_connect, Variant::BOOL);
|
||||
BIND_SET_GET(DiscordConnector, app_id, Variant::STRING, godot::PROPERTY_HINT_RANGE, "-99999,99999,or_less,or_greater,hide_slider");
|
||||
BIND_SET_GET(DiscordConnector, encryption_key, Variant::STRING, godot::PROPERTY_HINT_PASSWORD);
|
||||
BIND_SET_GET(DiscordConnector, token_auto_manage, Variant::BOOL);
|
||||
ADD_GROUP("Automatic", "auto_");
|
||||
BIND_SET_GET(DiscordConnector, auto_connect, Variant::BOOL);
|
||||
BIND_SET_GET(DiscordConnector, auto_encryption_key, Variant::STRING, godot::PROPERTY_HINT_PASSWORD);
|
||||
BIND_SET_GET(DiscordConnector, auto_token_manage, Variant::BOOL);
|
||||
BIND_METHOD(DiscordConnector, connect_user);
|
||||
BIND_METHOD(DiscordConnector, update_user_token, "access_token");
|
||||
BIND_METHOD(DiscordConnector, get_access_token);
|
||||
BIND_METHOD(DiscordConnector, get_refresh_token);
|
||||
BIND_METHOD(DiscordConnector, get_expires_in);
|
||||
BIND_METHOD(DiscordConnector, refresh_user_token, "current_refresh_token");
|
||||
BIND_SIGNAL(user_connected, PropertyInfo(Variant::STRING, "access_token"), PropertyInfo(Variant::STRING, "refresh_token"), PropertyInfo(Variant::INT, "expires_in"));
|
||||
BIND_SIGNAL(user_connection_failed, PropertyInfo(Variant::STRING, "error"));
|
||||
BIND_SIGNAL(user_updated);
|
||||
BIND_SIGNAL(user_update_failed, PropertyInfo(Variant::STRING, "error"));
|
||||
BIND_SIGNAL(user_token_refreshed, PropertyInfo(Variant::STRING, "access_token"), PropertyInfo(Variant::STRING, "refresh_token"), PropertyInfo(Variant::INT, "expires_in"));
|
||||
BIND_SIGNAL(user_token_refresh_failed, PropertyInfo(Variant::STRING, "error"));
|
||||
}
|
||||
DiscordConnector::DiscordConnector()
|
||||
{
|
||||
@@ -34,7 +35,7 @@ DiscordConnector *DiscordConnector::get_singleton()
|
||||
void DiscordConnector::_ready()
|
||||
{
|
||||
client = std::make_shared<discordpp::Client>();
|
||||
if (encryption_key == "")
|
||||
if (auto_encryption_key == "" || !auto_token_manage)
|
||||
{
|
||||
if (!Engine::get_singleton()->is_editor_hint() && !editor_process)
|
||||
{
|
||||
@@ -42,9 +43,9 @@ void DiscordConnector::_ready()
|
||||
connect_user();
|
||||
}
|
||||
}
|
||||
else if (token_auto_manage)
|
||||
else if (auto_token_manage)
|
||||
{
|
||||
ConfigFile config = DiscordUtil::get_singleton()->get_tokens(encryption_key);
|
||||
ConfigFile config = DiscordUtil::get_singleton()->get_tokens(auto_encryption_key);
|
||||
if (config.has_section_key("tokens", "access_token") && config.has_section_key("tokens", "refresh_token") && config.has_section_key("tokens", "expires_in"))
|
||||
{
|
||||
access_token = config.get_value("tokens", "access_token");
|
||||
@@ -69,10 +70,10 @@ void DiscordConnector::_process(double delta)
|
||||
{
|
||||
discordpp::RunCallbacks();
|
||||
}
|
||||
if (encryption_key == "")
|
||||
if (auto_encryption_key == "" && auto_token_manage)
|
||||
{
|
||||
DiscordUtil::get_singleton()->delete_tokens();
|
||||
encryption_key = DiscordUtil::get_singleton()->generate_encryption_key();
|
||||
auto_encryption_key = DiscordUtil::get_singleton()->generate_auto_encryption_key();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,24 +97,24 @@ int64_t DiscordConnector::get_app_id()
|
||||
return static_cast<int64_t>(app_id);
|
||||
}
|
||||
|
||||
void DiscordConnector::set_token_auto_manage(bool value)
|
||||
void DiscordConnector::set_auto_token_manage(bool value)
|
||||
{
|
||||
token_auto_manage = value;
|
||||
auto_token_manage = value;
|
||||
}
|
||||
|
||||
bool DiscordConnector::get_token_auto_manage()
|
||||
bool DiscordConnector::get_auto_token_manage()
|
||||
{
|
||||
return token_auto_manage;
|
||||
return auto_token_manage;
|
||||
}
|
||||
|
||||
void DiscordConnector::set_encryption_key(String value)
|
||||
void DiscordConnector::set_auto_encryption_key(String value)
|
||||
{
|
||||
encryption_key = value;
|
||||
auto_encryption_key = value;
|
||||
}
|
||||
|
||||
String DiscordConnector::get_encryption_key()
|
||||
String DiscordConnector::get_auto_encryption_key()
|
||||
{
|
||||
return encryption_key;
|
||||
return auto_encryption_key;
|
||||
}
|
||||
|
||||
void DiscordConnector::connect_user()
|
||||
@@ -129,7 +130,7 @@ void DiscordConnector::connect_user()
|
||||
client->Authorize(args, [this, codeVerifier](auto result, auto code, auto redirectUri)
|
||||
{
|
||||
if (!result.Successful()) {
|
||||
UtilityFunctions::push_error("Authentication Error: " + String(result.Error().c_str()));
|
||||
emit_signal("user_connection_failed", result.Error().c_str());
|
||||
return;
|
||||
} else {
|
||||
client->GetToken(app_id, code, codeVerifier.Verifier(), redirectUri,
|
||||
@@ -141,9 +142,9 @@ void DiscordConnector::connect_user()
|
||||
std::string scope) {
|
||||
if (result.Successful()) {
|
||||
emit_signal("user_connected", accessToken.c_str(), refreshToken.c_str(), expiresIn);
|
||||
if (token_auto_manage)
|
||||
if (auto_token_manage)
|
||||
{
|
||||
DiscordUtil::get_singleton()->save_tokens(accessToken.c_str(), refreshToken.c_str(), expiresIn, encryption_key);
|
||||
DiscordUtil::get_singleton()->save_tokens(accessToken.c_str(), refreshToken.c_str(), expiresIn, auto_encryption_key);
|
||||
}
|
||||
} else {
|
||||
emit_signal("user_connection_failed", result.Error().c_str());
|
||||
@@ -166,17 +167,21 @@ void DiscordConnector::update_user_token(String access_token)
|
||||
} });
|
||||
}
|
||||
|
||||
String DiscordConnector::get_access_token()
|
||||
void DiscordConnector::refresh_user_token(String current_refresh_token)
|
||||
{
|
||||
return access_token;
|
||||
}
|
||||
|
||||
String DiscordConnector::get_refresh_token()
|
||||
{
|
||||
return refresh_token;
|
||||
}
|
||||
|
||||
int64_t DiscordConnector::get_expires_in()
|
||||
{
|
||||
return expires_in;
|
||||
client->RefreshToken(std::stoull(refresh_token.utf8().get_data()), refresh_token.utf8().get_data(), [this, current_refresh_token](discordpp::ClientResult result, std::string accessToken, std::string refreshToken, discordpp::AuthorizationTokenType tokenType, int32_t expiresIn, std::string scopes)
|
||||
{
|
||||
if (result.Successful())
|
||||
{
|
||||
emit_signal("user_token_refreshed", accessToken.c_str(), refreshToken.c_str(), expiresIn);
|
||||
if (auto_token_manage)
|
||||
{
|
||||
DiscordUtil::get_singleton()->save_tokens(accessToken.c_str(), refreshToken.c_str(), expiresIn, auto_encryption_key);
|
||||
update_user_token(accessToken.c_str());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
emit_signal("user_token_refresh_failed", result.Error().c_str());
|
||||
} });
|
||||
}
|
@@ -24,20 +24,17 @@ public:
|
||||
std::shared_ptr<discordpp::Client> client;
|
||||
|
||||
H_SET_GET(app_id, 0)
|
||||
H_SET_GET(encryption_key, "")
|
||||
H_SET_GET(token_auto_manage, true)
|
||||
H_SET_GET(auto_encryption_key, "")
|
||||
H_SET_GET(auto_token_manage, false)
|
||||
H_SET_GET(auto_connect, false)
|
||||
|
||||
String access_token;
|
||||
String refresh_token;
|
||||
int64_t expires_in;
|
||||
String get_access_token();
|
||||
String get_refresh_token();
|
||||
int64_t get_expires_in();
|
||||
|
||||
void connect_user();
|
||||
void update_user_token(String access_token);
|
||||
String refresh_user_token(String refresh_token);
|
||||
void refresh_user_token(String refresh_token);
|
||||
|
||||
DiscordConnector();
|
||||
~DiscordConnector();
|
||||
|
14
src/util.cpp
14
src/util.cpp
@@ -6,8 +6,8 @@ void DiscordUtil::_bind_methods()
|
||||
{
|
||||
BIND_METHOD(DiscordUtil, debug);
|
||||
BIND_METHOD(DiscordUtil, run_callbacks);
|
||||
BIND_METHOD(DiscordUtil, save_tokens, "access_token", "refresh_token", "expires_in", "encryption_key");
|
||||
BIND_METHOD(DiscordUtil, generate_encryption_key);
|
||||
BIND_METHOD(DiscordUtil, save_tokens, "access_token", "refresh_token", "expires_in", "auto_encryption_key");
|
||||
BIND_METHOD(DiscordUtil, generate_auto_encryption_key);
|
||||
}
|
||||
|
||||
DiscordUtil::DiscordUtil()
|
||||
@@ -197,7 +197,7 @@ Dictionary DiscordUtil::relationship2dict(discordpp::RelationshipHandle relation
|
||||
return dict_relationship;
|
||||
}
|
||||
|
||||
String DiscordUtil::generate_encryption_key()
|
||||
String DiscordUtil::generate_auto_encryption_key()
|
||||
{
|
||||
const char *charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"$%&/()=?*+#-.,;:_'";
|
||||
String key;
|
||||
@@ -208,13 +208,13 @@ String DiscordUtil::generate_encryption_key()
|
||||
return key;
|
||||
}
|
||||
|
||||
void DiscordUtil::save_tokens(String access_token, String refresh_token, int64_t expires_in, String encryption_key)
|
||||
void DiscordUtil::save_tokens(String access_token, String refresh_token, int64_t expires_in, String auto_encryption_key)
|
||||
{
|
||||
ConfigFile config;
|
||||
config.set_value("tokens", "access_token", access_token);
|
||||
config.set_value("tokens", "refresh_token", refresh_token);
|
||||
config.set_value("tokens", "expires_in", expires_in);
|
||||
config.save_encrypted_pass("user://discord_data.binary", encryption_key);
|
||||
config.save_encrypted_pass("user://discord_data.binary", auto_encryption_key);
|
||||
}
|
||||
|
||||
void DiscordUtil::delete_tokens()
|
||||
@@ -223,14 +223,14 @@ void DiscordUtil::delete_tokens()
|
||||
config.save("user://discord_data.binary");
|
||||
}
|
||||
|
||||
ConfigFile DiscordUtil::get_tokens(String encryption_key)
|
||||
ConfigFile DiscordUtil::get_tokens(String auto_encryption_key)
|
||||
{
|
||||
ConfigFile config;
|
||||
if (!FileAccess::file_exists("user://discord_data.binary"))
|
||||
{
|
||||
return ConfigFile();
|
||||
}
|
||||
Error err = config.load_encrypted_pass("user://discord_data.binary", encryption_key);
|
||||
Error err = config.load_encrypted_pass("user://discord_data.binary", auto_encryption_key);
|
||||
if (err != OK)
|
||||
{
|
||||
config.save("user://discord_data.binary");
|
||||
|
@@ -32,10 +32,10 @@ public:
|
||||
Dictionary relationship2dict(discordpp::RelationshipHandle relationship);
|
||||
Dictionary user2dict(discordpp::UserHandle user);
|
||||
|
||||
void save_tokens(String access_token, String refresh_token, int64_t expires_in, String encryption_key);
|
||||
String generate_encryption_key();
|
||||
void save_tokens(String access_token, String refresh_token, int64_t expires_in, String auto_encryption_key);
|
||||
String generate_auto_encryption_key();
|
||||
void delete_tokens();
|
||||
ConfigFile get_tokens(String encryption_key);
|
||||
ConfigFile get_tokens(String auto_encryption_key);
|
||||
|
||||
void debug();
|
||||
void run_callbacks();
|
||||
|
Reference in New Issue
Block a user