diff --git a/project/discord_connector.gd b/project/discord_connector.gd index d45c223..6929412 100644 --- a/project/discord_connector.gd +++ b/project/discord_connector.gd @@ -1,17 +1,16 @@ extends DiscordConnector -func _on_user_connected(access_token: String, refresh_token: String, expires_in: int) -> void: +@onready var discord_activity: DiscordActivity = $DiscordActivity + +func _on_authenticated(access_token: String, refresh_token: String, expires_in: int) -> void: print_debug("Access token: %s \nRefresh Token: %s \nExpires in: %s" % [access_token, refresh_token, expires_in]) -func _on_user_connection_failed(error: String) -> void: - push_error("User connection failed! Error: " + error) - -func _on_user_update_failed(error: String) -> void: - push_error("User update failed! Error: " + error) - -func _on_connection_ready() -> void: - print_debug("CONNECTION READY") - $DiscordLobby.create_or_join_lobby("beanis") +func _on_authentication_failed(error: String) -> void: + push_error("Auth failed! Error: " + error) func _on_connection_error(error: String) -> void: push_error(error) + +func _on_connection_ready() -> void: + print_debug("CONNECTION READY") + discord_activity.update_rich_presence() diff --git a/project/main.tscn b/project/main.tscn index b63e020..b6da28d 100644 --- a/project/main.tscn +++ b/project/main.tscn @@ -1,9 +1,17 @@ -[gd_scene load_steps=4 format=3 uid="uid://dyc3kseph4el7"] +[gd_scene load_steps=5 format=3 uid="uid://dyc3kseph4el7"] [ext_resource type="Texture2D" uid="uid://b3qm246m7pnsx" path="res://assets/Logo_V2.png" id="2_gd222"] [ext_resource type="Script" uid="uid://kmubk5a6i385" path="res://discord_connector.gd" id="3_h2yge"] [ext_resource type="Script" uid="uid://46tue7u6crd6" path="res://addons/discord-rpc-gd/nodes/debug.gd" id="6_ujijw"] +[sub_resource type="RichPresence" id="RichPresence_h2yge"] +state = "Inside a Node" +details = "Godot -> Discord Social SDK" +large_image = &"example_game" +large_text = "Example" +small_image = &"boss" +small_text = "Fighting the boss D:" + [node name="Node" type="Node"] [node name="ColorRect" type="ColorRect" parent="."] @@ -45,10 +53,10 @@ script = ExtResource("6_ujijw") [node name="DiscordConnector" type="DiscordConnector" parent="."] app_id = 1099618430065324082 auto_connect = true -auto_token_manage = true script = ExtResource("3_h2yge") [node name="DiscordActivity" type="DiscordActivity" parent="DiscordConnector"] +rich_presence = SubResource("RichPresence_h2yge") root_connector = NodePath("..") [node name="DiscordLobby" type="DiscordLobby" parent="DiscordConnector"] @@ -58,8 +66,7 @@ root_connector = NodePath("..") position = Vector2(789, 330.5) scale = Vector2(0.408203, 0.408203) +[connection signal="authenticated" from="DiscordConnector" to="DiscordConnector" method="_on_authenticated"] +[connection signal="authentication_failed" from="DiscordConnector" to="DiscordConnector" method="_on_authentication_failed"] [connection signal="connection_error" from="DiscordConnector" to="DiscordConnector" method="_on_connection_error"] [connection signal="connection_ready" from="DiscordConnector" to="DiscordConnector" method="_on_connection_ready"] -[connection signal="user_connected" from="DiscordConnector" to="DiscordConnector" method="_on_user_connected"] -[connection signal="user_connection_failed" from="DiscordConnector" to="DiscordConnector" method="_on_user_connection_failed"] -[connection signal="user_update_failed" from="DiscordConnector" to="DiscordConnector" method="_on_user_update_failed"] diff --git a/src/nodes/discord_activity.cpp b/src/nodes/discord_activity.cpp index 2375317..7d6cb40 100644 --- a/src/nodes/discord_activity.cpp +++ b/src/nodes/discord_activity.cpp @@ -2,10 +2,8 @@ void DiscordActivity::_bind_methods() { - - ClassDB::bind_method(D_METHOD("get_activities"), &DiscordActivity::get_activities); - ClassDB::bind_method(D_METHOD("set_activities", "value"), &DiscordActivity::set_activities); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "activities", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("ActivityResource")), "set_activities", "get_activities"); + BIND_SET_GET(DiscordActivity, rich_presence, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "RichPresence"); + BIND_METHOD(DiscordActivity, update_rich_presence); } DiscordActivity::DiscordActivity() { @@ -14,12 +12,29 @@ DiscordActivity::~DiscordActivity() { } -TypedArray DiscordActivity::get_activities() +Ref DiscordActivity::get_rich_presence() { - return activities; + return rich_presence; } -void DiscordActivity::set_activities(TypedArray value) +void DiscordActivity::set_rich_presence(Ref value) { - activities = value; + rich_presence = value; } + +void DiscordActivity::update_rich_presence() +{ + if(rich_presence.is_valid()){ + discordpp::Activity activity; + activity.SetState(rich_presence->get_state().utf8().get_data()); + activity.SetDetails(rich_presence->get_details().utf8().get_data()); + discordpp::ActivityAssets assets; + assets.SetSmallImage(String(rich_presence->get_small_image()).utf8().get_data()); + assets.SetSmallText(rich_presence->get_small_text().utf8().get_data()); + assets.SetLargeImage(String(rich_presence->get_large_image()).utf8().get_data()); + assets.SetLargeText(rich_presence->get_large_text().utf8().get_data()); + activity.SetAssets(assets); + + connector->client->UpdateRichPresence(activity, [](discordpp::ClientResult result){}); + } +} \ No newline at end of file diff --git a/src/nodes/discord_activity.h b/src/nodes/discord_activity.h index c22a24f..ab2a953 100644 --- a/src/nodes/discord_activity.h +++ b/src/nodes/discord_activity.h @@ -2,7 +2,7 @@ #define DISCORD_ACTIVITY_H #include "discord_connected.h" -#include "../resources/activity.h" +#include "../resources/rich_presence.h" using namespace godot; @@ -14,9 +14,11 @@ protected: static void _bind_methods(); public: - TypedArray activities; - TypedArray get_activities(); - void set_activities(TypedArray value); + Ref rich_presence; + Ref get_rich_presence(); + void set_rich_presence(Ref value); + + void update_rich_presence(); DiscordActivity(); ~DiscordActivity(); diff --git a/src/nodes/discord_connector.cpp b/src/nodes/discord_connector.cpp index 5dd3e7a..8817a67 100644 --- a/src/nodes/discord_connector.cpp +++ b/src/nodes/discord_connector.cpp @@ -7,16 +7,15 @@ void DiscordConnector::_bind_methods() BIND_SET_GET(DiscordConnector, auto_connect, Variant::BOOL); BIND_SET_GET(DiscordConnector, auto_token_manage, Variant::BOOL); BIND_SET_GET(DiscordConnector, auto_encryption_key, Variant::STRING, godot::PROPERTY_HINT_NONE, "", godot::PROPERTY_USAGE_NO_EDITOR); - BIND_METHOD(DiscordConnector, connect_user); - BIND_METHOD(DiscordConnector, update_user_token, "access_token"); - 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_METHOD(DiscordConnector, auth); + BIND_METHOD(DiscordConnector, token_connect, "access_token"); + BIND_METHOD(DiscordConnector, token_refresh, "current_refresh_token"); + BIND_SIGNAL(authenticated, PropertyInfo(Variant::STRING, "access_token"), PropertyInfo(Variant::STRING, "refresh_token"), PropertyInfo(Variant::INT, "expires_in")); + BIND_SIGNAL(authentication_failed, PropertyInfo(Variant::STRING, "error")); BIND_SIGNAL(connection_ready); BIND_SIGNAL(connection_error, PropertyInfo(Variant::STRING, "error")); - 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")); + BIND_SIGNAL(token_refreshed, PropertyInfo(Variant::STRING, "access_token"), PropertyInfo(Variant::STRING, "refresh_token"), PropertyInfo(Variant::INT, "expires_in")); + BIND_SIGNAL(token_refresh_failed, PropertyInfo(Variant::STRING, "error")); } DiscordConnector::DiscordConnector() @@ -59,14 +58,14 @@ void DiscordConnector::_ready() access_token = config->get_value("tokens", "access_token"); refresh_token = config->get_value("tokens", "refresh_token"); expires_in = config->get_value("tokens", "expires_in"); - update_user_token(access_token); + token_connect(access_token); } else { DiscordUtil::get_singleton()->delete_tokens(); if (auto_connect) { - connect_user(); + auth(); } } } @@ -74,7 +73,7 @@ void DiscordConnector::_ready() { if (auto_connect) { - connect_user(); + auth(); } } } @@ -127,7 +126,7 @@ String DiscordConnector::get_auto_encryption_key() return auto_encryption_key; } -void DiscordConnector::connect_user() +void DiscordConnector::auth() { auto codeVerifier = client->CreateAuthorizationCodeVerifier(); @@ -140,7 +139,7 @@ void DiscordConnector::connect_user() client->Authorize(args, [this, codeVerifier](auto result, auto code, auto redirectUri) { if (!result.Successful()) { - emit_signal("user_connection_failed", result.Error().c_str()); + emit_signal("authentication_failed", result.Error().c_str()); return; } else { client->GetToken(app_id, code, codeVerifier.Verifier(), redirectUri, @@ -156,40 +155,40 @@ void DiscordConnector::connect_user() 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()); + emit_signal("authenticated", result.Error().c_str()); return; } - update_user_token(accessToken.c_str()); + token_connect(accessToken.c_str()); }); } }); } -void DiscordConnector::update_user_token(String access_token) +void DiscordConnector::token_connect(String access_token) { client->UpdateToken(discordpp::AuthorizationTokenType::Bearer, access_token.utf8().get_data(), [this](discordpp::ClientResult result) { if(result.Successful()) { client->Connect(); } else { - emit_signal("user_update_failed", result.Error().c_str()); + emit_signal("connection_error", result.Error().c_str()); } }); } -void DiscordConnector::refresh_user_token(String current_refresh_token) +void DiscordConnector::token_refresh(String current_refresh_token) { 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); + emit_signal("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()); + token_connect(accessToken.c_str()); } } else { - emit_signal("user_token_refresh_failed", result.Error().c_str()); + emit_signal("token_refresh_failed", result.Error().c_str()); } }); } \ No newline at end of file diff --git a/src/nodes/discord_connector.h b/src/nodes/discord_connector.h index 5370143..b57e78c 100644 --- a/src/nodes/discord_connector.h +++ b/src/nodes/discord_connector.h @@ -20,16 +20,16 @@ public: H_SET_GET(app_id, 0) H_SET_GET(auto_encryption_key, "") - H_SET_GET(auto_token_manage, false) + H_SET_GET(auto_token_manage, true) H_SET_GET(auto_connect, false) String access_token; String refresh_token; int64_t expires_in; - void connect_user(); - void update_user_token(String access_token); - void refresh_user_token(String refresh_token); + void auth(); + void token_connect(String access_token); + void token_refresh(String refresh_token); void refresh_auto_encryption_key(); diff --git a/src/register_types.cpp b/src/register_types.cpp index 8c4d763..301ef82 100644 --- a/src/register_types.cpp +++ b/src/register_types.cpp @@ -18,7 +18,7 @@ void initialize_DiscordUtil_module(ModuleInitializationLevel p_level) ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); - ClassDB::register_class(); + ClassDB::register_class(); } } diff --git a/src/register_types.h b/src/register_types.h index 0c81c2c..98f5111 100644 --- a/src/register_types.h +++ b/src/register_types.h @@ -20,6 +20,6 @@ void uninitialize_DiscordUtil_module(); #include "nodes/discord_lobby.h" #include "nodes/discord_linked_channel.h" #include "nodes/discord_activity.h" -#include "resources/activity.h" +#include "resources/rich_presence.h" #endif // REGISTER_TYPES_H \ No newline at end of file diff --git a/src/resources/activity.cpp b/src/resources/activity.cpp deleted file mode 100644 index d2d4402..0000000 --- a/src/resources/activity.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include "activity.h" - -void ActivityResource::_bind_methods() -{ - BIND_SET_GET(ActivityResource, type, Variant::INT, PROPERTY_HINT_ENUM, "Playing,Streaming,Listening,Watching,Custom Status,Competing,Hang Status"); - BIND_SET_GET(ActivityResource, state, Variant::STRING); - BIND_SET_GET(ActivityResource, details, Variant::STRING); - BIND_SET_GET(ActivityResource, large_image, Variant::STRING); - BIND_SET_GET(ActivityResource, large_text, Variant::STRING); - BIND_SET_GET(ActivityResource, small_image, Variant::STRING); - BIND_SET_GET(ActivityResource, small_text, Variant::STRING); - BIND_SET_GET(ActivityResource, timestamps_start, Variant::INT); - BIND_SET_GET(ActivityResource, timestamps_end, Variant::INT); - - BIND_ENUM_CONSTANT(TYPE_PLAYING); - BIND_ENUM_CONSTANT(TYPE_STREAMING); - BIND_ENUM_CONSTANT(TYPE_LISTENING); - BIND_ENUM_CONSTANT(TYPE_WATCHING); - BIND_ENUM_CONSTANT(TYPE_CUSTOM_STATUS); - BIND_ENUM_CONSTANT(TYPE_COMPETING); - BIND_ENUM_CONSTANT(TYPE_HANG_STATUS); -} - -ActivityResource::ActivityResource() -{ -} - -void ActivityResource::set_type(ActivityType p_type) -{ - type = p_type; -} - -ActivityResource::ActivityType ActivityResource::get_type() -{ - return type; -} - -void ActivityResource::set_state(String p_state) -{ - state = p_state; -} - -String ActivityResource::get_state() -{ - return state; -} - -void ActivityResource::set_details(String value) -{ - details = value; -} - -String ActivityResource::get_details() -{ - return details; -} - -void ActivityResource::set_large_image(String value) -{ - large_image = value; -} - -String ActivityResource::get_large_image() -{ - return large_image; -} - -void ActivityResource::set_large_text(String value) -{ - large_text = value; -} - -String ActivityResource::get_large_text() -{ - return large_text; -} - -void ActivityResource::set_small_image(String value) -{ - small_image = value; -} - -String ActivityResource::get_small_image() -{ - return small_image; -} - -void ActivityResource::set_small_text(String value) -{ - small_text = value; -} - -String ActivityResource::get_small_text() -{ - return small_text; -} - -void ActivityResource::set_timestamps_start(int64_t value) -{ - timestamps_start = value; -} - -int64_t ActivityResource::get_timestamps_start() -{ - return timestamps_start; -} - -void ActivityResource::set_timestamps_end(int64_t value) -{ - timestamps_end = value; -} - -int64_t ActivityResource::get_timestamps_end() -{ - return timestamps_end; -} \ No newline at end of file diff --git a/src/resources/activity.h b/src/resources/activity.h deleted file mode 100644 index 9852f6f..0000000 --- a/src/resources/activity.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef ACTIVITY_H -#define ACTIVITY_H - -#include -#include -#include -#include -#include "../util.h" - -using namespace godot; - -class ActivityResource : public Resource { - GDCLASS(ActivityResource, Resource); - -protected: - static void _bind_methods(); - -public: - enum ActivityType { - TYPE_PLAYING = 0, - TYPE_STREAMING = 1, - TYPE_LISTENING = 2, - TYPE_WATCHING = 3, - TYPE_CUSTOM_STATUS = 4, - TYPE_COMPETING = 5, - TYPE_HANG_STATUS = 6, - }; - - ActivityType type = TYPE_PLAYING; - ActivityType get_type(); - void set_type(ActivityType p_type); - - H_SET_GET(state, "") - H_SET_GET(details, "") - H_SET_GET(large_image, "") - H_SET_GET(large_text, "") - H_SET_GET(small_image, "") - H_SET_GET(small_text, "") - H_SET_GET(timestamps_start, 0) - H_SET_GET(timestamps_end, 0) - - ActivityResource(); -}; - -VARIANT_ENUM_CAST(ActivityResource::ActivityType); - -#endif \ No newline at end of file diff --git a/src/resources/rich_presence.cpp b/src/resources/rich_presence.cpp new file mode 100644 index 0000000..13708c8 --- /dev/null +++ b/src/resources/rich_presence.cpp @@ -0,0 +1,97 @@ +#include "rich_presence.h" + +void RichPresence::_bind_methods() +{ + BIND_SET_GET(RichPresence, state, Variant::STRING); + BIND_SET_GET(RichPresence, details, Variant::STRING); + ADD_GROUP("Large Image", "large_"); + BIND_SET_GET(RichPresence, large_image, Variant::STRING_NAME); + BIND_SET_GET(RichPresence, large_text, Variant::STRING); + ADD_GROUP("Small Image", "small_"); + BIND_SET_GET(RichPresence, small_image, Variant::STRING_NAME); + BIND_SET_GET(RichPresence, small_text, Variant::STRING); +} + +RichPresence::RichPresence() +{ +} + +void RichPresence::set_state(String p_state) +{ + state = p_state; +} + +String RichPresence::get_state() +{ + return state; +} + +void RichPresence::set_details(String value) +{ + details = value; +} + +String RichPresence::get_details() +{ + return details; +} + +void RichPresence::set_large_image(StringName value) +{ + large_image = value; +} + +StringName RichPresence::get_large_image() +{ + return large_image; +} + +void RichPresence::set_large_text(String value) +{ + large_text = value; +} + +String RichPresence::get_large_text() +{ + return large_text; +} + +void RichPresence::set_small_image(StringName value) +{ + small_image = value; +} + +StringName RichPresence::get_small_image() +{ + return small_image; +} + +void RichPresence::set_small_text(String value) +{ + small_text = value; +} + +String RichPresence::get_small_text() +{ + return small_text; +} + +void RichPresence::set_timestamps_start(int64_t value) +{ + timestamps_start = value; +} + +int64_t RichPresence::get_timestamps_start() +{ + return timestamps_start; +} + +void RichPresence::set_timestamps_end(int64_t value) +{ + timestamps_end = value; +} + +int64_t RichPresence::get_timestamps_end() +{ + return timestamps_end; +} \ No newline at end of file diff --git a/src/resources/rich_presence.h b/src/resources/rich_presence.h new file mode 100644 index 0000000..67694b3 --- /dev/null +++ b/src/resources/rich_presence.h @@ -0,0 +1,36 @@ +#ifndef RICH_PRESENCE_H +#define RICH_PRESENCE_H + +#include +#include +#include +#include +#include "../util.h" + +using namespace godot; + +class RichPresence : public Resource { + GDCLASS(RichPresence, Resource); + +protected: + static void _bind_methods(); + +public: + + H_SET_GET(state, "") + H_SET_GET(details, "") + StringName large_image; + StringName get_large_image(); + void set_large_image(StringName value); + H_SET_GET(large_text, "") + StringName small_image; + StringName get_small_image(); + void set_small_image(StringName value); + H_SET_GET(small_text, "") + H_SET_GET(timestamps_start, 0) + H_SET_GET(timestamps_end, 0) + + RichPresence(); +}; + +#endif \ No newline at end of file