improved auto_token_manage, added invites and some more stuff

This commit is contained in:
2025-03-28 00:36:01 +01:00
parent 98a4b4783c
commit 6edd50d9c2
11 changed files with 203 additions and 62 deletions

View File

@@ -13,4 +13,4 @@ func _on_connection_error(error: String) -> void:
func _on_connection_ready() -> void:
print_debug("CONNECTION READY")
discord_activity.update_rich_presence()
discord_activity.update()

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=5 format=3 uid="uid://dyc3kseph4el7"]
[gd_scene load_steps=6 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"]
@@ -7,11 +7,16 @@
[sub_resource type="RichPresence" id="RichPresence_h2yge"]
state = "Inside a Node"
details = "Godot -> Discord Social SDK"
large_image = &"example_game"
large_image = "example_game"
large_text = "Example"
small_image = &"boss"
small_image = "boss"
small_text = "Fighting the boss D:"
[sub_resource type="PartyInvite" id="PartyInvite_h2yge"]
id = "test"
join_secret = "test2"
is_public_party = true
[node name="Node" type="Node"]
[node name="ColorRect" type="ColorRect" parent="."]
@@ -57,6 +62,7 @@ script = ExtResource("3_h2yge")
[node name="DiscordActivity" type="DiscordActivity" parent="DiscordConnector"]
rich_presence = SubResource("RichPresence_h2yge")
party_invite = SubResource("PartyInvite_h2yge")
root_connector = NodePath("..")
[node name="DiscordLobby" type="DiscordLobby" parent="DiscordConnector"]

View File

@@ -3,7 +3,8 @@
void DiscordActivity::_bind_methods()
{
BIND_SET_GET(DiscordActivity, rich_presence, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "RichPresence");
BIND_METHOD(DiscordActivity, update_rich_presence);
BIND_SET_GET(DiscordActivity, party_invite, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "PartyInvite");
BIND_METHOD(DiscordActivity, update);
}
DiscordActivity::DiscordActivity()
{
@@ -22,7 +23,17 @@ void DiscordActivity::set_rich_presence(Ref<RichPresence> value)
rich_presence = value;
}
void DiscordActivity::update_rich_presence()
Ref<PartyInvite> DiscordActivity::get_party_invite()
{
return party_invite;
}
void DiscordActivity::set_party_invite(Ref<PartyInvite> value)
{
party_invite = value;
}
void DiscordActivity::update()
{
if(rich_presence.is_valid()){
discordpp::Activity activity;
@@ -34,7 +45,22 @@ void DiscordActivity::update_rich_presence()
assets.SetLargeImage(String(rich_presence->get_large_image()).utf8().get_data());
assets.SetLargeText(rich_presence->get_large_text().utf8().get_data());
activity.SetAssets(assets);
discordpp::ActivityTimestamps timestamps;
timestamps.SetStart(rich_presence->get_timestamps_start());
timestamps.SetEnd(rich_presence->get_timestamps_end());
activity.SetTimestamps(timestamps);
discordpp::ActivityParty party;
party.SetCurrentSize(party_invite->get_current_size());
party.SetMaxSize(party_invite->get_max_size());
party.SetId(party_invite->get_id().utf8().get_data());
party.SetPrivacy(party_invite->get_is_public_party() ? discordpp::ActivityPartyPrivacy::Public : discordpp::ActivityPartyPrivacy::Private);
discordpp::ActivitySecrets secrets;
secrets.SetJoin(party_invite->get_join_secret().utf8().get_data());
activity.SetSecrets(secrets);
activity.SetParty(party);
//TODO: Supported platforms
//activity.SetSupportedPlatforms();
connector->client->UpdateRichPresence(activity, [](discordpp::ClientResult result){});
}
}

View File

@@ -3,6 +3,7 @@
#include "discord_connected.h"
#include "../resources/rich_presence.h"
#include "../resources/party_invite.h"
using namespace godot;
@@ -18,7 +19,18 @@ public:
Ref<RichPresence> get_rich_presence();
void set_rich_presence(Ref<RichPresence> value);
void update_rich_presence();
Ref<PartyInvite> party_invite;
Ref<PartyInvite> get_party_invite();
void set_party_invite(Ref<PartyInvite> value);
void register_launch_command(String command);
void register_steam(int32_t steam_id);
void accept_join_request(int64_t user_id);
void send_invite(int64_t friend_user_id, bool is_spectate, String message_content);
void accept_invite(int64_t user_id);
void update();
DiscordActivity();
~DiscordActivity();

View File

@@ -27,9 +27,14 @@ DiscordConnector::~DiscordConnector()
void DiscordConnector::_ready()
{
if (Engine::get_singleton()->is_editor_hint() || editor_process)
return;
client = std::make_shared<discordpp::Client>();
client->SetStatusChangedCallback([this](discordpp::Client::Status status, discordpp::Client::Error error, int32_t errorDetail) {
client->SetStatusChangedCallback([this](discordpp::Client::Status status, discordpp::Client::Error error, int32_t errorDetail)
{
if (status == discordpp::Client::Status::Ready)
{
emit_signal("connection_ready");
@@ -37,46 +42,13 @@ void DiscordConnector::_ready()
if (error != discordpp::Client::Error::None)
{
emit_signal("connection_error", String(discordpp::Client::ErrorToString(error).c_str()));
}
});
} });
if (Engine::get_singleton()->is_editor_hint() || editor_process)
return;
if (auto_token_manage)
{
if (auto_encryption_key.is_empty())
{
auto_encryption_key = DiscordUtil::get_singleton()->generate_auto_encryption_key();
}
Ref<godot::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");
refresh_token = config->get_value("tokens", "refresh_token");
expires_in = config->get_value("tokens", "expires_in");
token_connect(access_token);
}
else
{
DiscordUtil::get_singleton()->delete_tokens();
if (auto_connect)
{
auth();
}
}
}
else
{
if (auto_connect)
{
auth();
}
}
}
void DiscordConnector::_process(double delta)
{
@@ -128,11 +100,35 @@ String DiscordConnector::get_auto_encryption_key()
void DiscordConnector::auth()
{
if (auto_token_manage)
{
if (auto_encryption_key.is_empty())
{
auto_encryption_key = DiscordUtil::get_singleton()->generate_auto_encryption_key();
}
Ref<godot::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");
refresh_token = config->get_value("tokens", "refresh_token");
expires_in = config->get_value("tokens", "expires_in");
token_connect(access_token);
return;
}
else
{
DiscordUtil::get_singleton()->delete_tokens();
}
}
auto codeVerifier = client->CreateAuthorizationCodeVerifier();
discordpp::AuthorizationArgs args{};
args.SetClientId(app_id);
args.SetScopes(discordpp::Client::GetDefaultPresenceScopes());
args.SetCodeChallenge(codeVerifier.Challenge());
// Begin authentication process // TODO: option to open browser

View File

@@ -19,6 +19,7 @@ void initialize_DiscordUtil_module(ModuleInitializationLevel p_level)
ClassDB::register_class<DiscordLinkedChannel>();
ClassDB::register_class<DiscordActivity>();
ClassDB::register_class<RichPresence>();
ClassDB::register_class<PartyInvite>();
}
}

View File

@@ -21,5 +21,6 @@ void uninitialize_DiscordUtil_module();
#include "nodes/discord_linked_channel.h"
#include "nodes/discord_activity.h"
#include "resources/rich_presence.h"
#include "resources/party_invite.h"
#endif // REGISTER_TYPES_H

View File

@@ -0,0 +1,76 @@
#include "party_invite.h"
void PartyInvite::_bind_methods()
{
BIND_SET_GET(PartyInvite, id, Variant::STRING);
BIND_SET_GET(PartyInvite, current_size, Variant::INT);
BIND_SET_GET(PartyInvite, max_size, Variant::INT);
BIND_SET_GET(PartyInvite, join_secret, Variant::STRING);
BIND_SET_GET(PartyInvite, instanced, Variant::BOOL);
BIND_SET_GET(PartyInvite, is_public_party, Variant::BOOL);
}
PartyInvite::PartyInvite()
{
}
String PartyInvite::get_id()
{
return id;
}
void PartyInvite::set_id(String value)
{
id = value;
}
int64_t PartyInvite::get_current_size()
{
return current_size;
}
void PartyInvite::set_current_size(int64_t value)
{
current_size = value;
}
int64_t PartyInvite::get_max_size()
{
return max_size;
}
void PartyInvite::set_max_size(int64_t value)
{
max_size = value;
}
String PartyInvite::get_join_secret()
{
return join_secret;
}
void PartyInvite::set_join_secret(String value)
{
join_secret = value;
}
bool PartyInvite::get_instanced()
{
return instanced;
}
void PartyInvite::set_instanced(bool value)
{
instanced = value;
}
bool PartyInvite::get_is_public_party()
{
return is_public_party;
}
void PartyInvite::set_is_public_party(bool value)
{
is_public_party = value;
}

View File

@@ -0,0 +1,29 @@
#ifndef PARTY_INVITE_H
#define PARTY_INVITE_H
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/godot.hpp>
#include <godot_cpp/classes/resource.hpp>
#include "../util.h"
using namespace godot;
class PartyInvite : public Resource {
GDCLASS(PartyInvite, Resource);
protected:
static void _bind_methods();
public:
H_SET_GET(id, "")
H_SET_GET(current_size, 1)
H_SET_GET(max_size, 4)
H_SET_GET(join_secret, "")
H_SET_GET(instanced, false)
H_SET_GET(is_public_party, false)
PartyInvite();
};
#endif

View File

@@ -5,20 +5,18 @@ 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_image, Variant::STRING);
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_image, Variant::STRING);
BIND_SET_GET(RichPresence, small_text, Variant::STRING);
}
RichPresence::RichPresence()
{
}
RichPresence::RichPresence() {}
void RichPresence::set_state(String p_state)
void RichPresence::set_state(String value)
{
state = p_state;
state = value;
}
String RichPresence::get_state()
@@ -36,12 +34,12 @@ String RichPresence::get_details()
return details;
}
void RichPresence::set_large_image(StringName value)
void RichPresence::set_large_image(String value)
{
large_image = value;
}
StringName RichPresence::get_large_image()
String RichPresence::get_large_image()
{
return large_image;
}
@@ -56,12 +54,12 @@ String RichPresence::get_large_text()
return large_text;
}
void RichPresence::set_small_image(StringName value)
void RichPresence::set_small_image(String value)
{
small_image = value;
}
StringName RichPresence::get_small_image()
String RichPresence::get_small_image()
{
return small_image;
}

View File

@@ -19,13 +19,9 @@ 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_image, "")
H_SET_GET(large_text, "")
StringName small_image;
StringName get_small_image();
void set_small_image(StringName value);
H_SET_GET(small_image, "")
H_SET_GET(small_text, "")
H_SET_GET(timestamps_start, 0)
H_SET_GET(timestamps_end, 0)