added current user dictionary

This commit is contained in:
Yannik
2023-05-15 19:40:15 +02:00
parent 54d6cebd5c
commit c62e9af0ef
6 changed files with 46 additions and 0 deletions

View File

@@ -9,6 +9,8 @@ func _ready():
discord_sdk.connect("activity_spectate",_on_activity_spectate) discord_sdk.connect("activity_spectate",_on_activity_spectate)
download_texture("https://cdn.discordapp.com/embed/avatars/1.png", "res://discord_pfp_cache/invitepfp.png") download_texture("https://cdn.discordapp.com/embed/avatars/1.png", "res://discord_pfp_cache/invitepfp.png")
debug_text_update() debug_text_update()
print(discord_sdk.get_current_user())
func download_texture(url, file_name): func download_texture(url, file_name):
$user_request_avatar/HTTPRequest.download_file = file_name $user_request_avatar/HTTPRequest.download_file = file_name
invite_pfp = file_name invite_pfp = file_name
@@ -91,3 +93,7 @@ func _on_line_edit_text_submitted(new_text):
func _on_line_edit_2_text_submitted(new_text): func _on_line_edit_2_text_submitted(new_text):
discord_sdk.accept_invite(int(new_text)) discord_sdk.accept_invite(int(new_text))
print(int(new_text)) print(int(new_text))
func _on_button_2_pressed():
print(discord_sdk.get_current_user())

View File

@@ -138,8 +138,19 @@ grow_horizontal = 2
grow_vertical = 0 grow_vertical = 0
placeholder_text = "Accept Invite with user_id here" placeholder_text = "Accept Invite with user_id here"
[node name="Button2" type="Button" parent="."]
anchors_preset = 4
anchor_top = 0.5
anchor_bottom = 0.5
offset_top = -4.0
offset_right = 136.0
offset_bottom = 29.0
grow_vertical = 2
text = "Get current user"
[connection signal="toggled" from="CheckButton" to="." method="_on_check_button_toggled"] [connection signal="toggled" from="CheckButton" to="." method="_on_check_button_toggled"]
[connection signal="request_completed" from="user_request_avatar/HTTPRequest" to="." method="_on_http_request_request_completed"] [connection signal="request_completed" from="user_request_avatar/HTTPRequest" to="." method="_on_http_request_request_completed"]
[connection signal="pressed" from="Button" to="." method="_on_button_pressed"] [connection signal="pressed" from="Button" to="." method="_on_button_pressed"]
[connection signal="text_submitted" from="LineEdit" to="." method="_on_line_edit_text_submitted"] [connection signal="text_submitted" from="LineEdit" to="." method="_on_line_edit_text_submitted"]
[connection signal="text_submitted" from="LineEdit2" to="." method="_on_line_edit_2_text_submitted"] [connection signal="text_submitted" from="LineEdit2" to="." method="_on_line_edit_2_text_submitted"]
[connection signal="pressed" from="Button2" to="." method="_on_button_2_pressed"]

View File

@@ -93,6 +93,8 @@ void discord_sdk::_bind_methods()
ClassDB::bind_method(D_METHOD("send_invite", "user_id", "is_spectate", "message_content"), &discord_sdk::send_invite); ClassDB::bind_method(D_METHOD("send_invite", "user_id", "is_spectate", "message_content"), &discord_sdk::send_invite);
ClassDB::bind_method(D_METHOD("accept_invite", "user_id"), &discord_sdk::accept_invite); ClassDB::bind_method(D_METHOD("accept_invite", "user_id"), &discord_sdk::accept_invite);
ClassDB::bind_method(D_METHOD("get_current_user"), &discord_sdk::get_current_user);
ClassDB::bind_method(D_METHOD("get_is_discord_working"), &discord_sdk::get_is_discord_working); ClassDB::bind_method(D_METHOD("get_is_discord_working"), &discord_sdk::get_is_discord_working);
ClassDB::bind_method(D_METHOD("get_result_int"), &discord_sdk::get_result_int); ClassDB::bind_method(D_METHOD("get_result_int"), &discord_sdk::get_result_int);
@@ -149,6 +151,10 @@ void discord_sdk::set_app_id(int64_t value)
if (result == discord::Result::Ok && app_id > 0) if (result == discord::Result::Ok && app_id > 0)
{ {
// initialize currentuser stuff
core->UserManager().OnCurrentUserUpdate.Connect([]()
{discord::User user{};
core->UserManager().GetCurrentUser(&user); });
// signals // signals
core->ActivityManager().OnActivityJoin.Connect([](const char *secret) core->ActivityManager().OnActivityJoin.Connect([](const char *secret)
{ discord_sdk::get_singleton() { discord_sdk::get_singleton()
@@ -394,6 +400,26 @@ void discord_sdk::register_steam(int32_t value)
if (result == discord::Result::Ok && app_id > 0) if (result == discord::Result::Ok && app_id > 0)
core->ActivityManager().RegisterSteam(value); core->ActivityManager().RegisterSteam(value);
} }
Dictionary discord_sdk::get_current_user()
{
Dictionary userdict;
if (result == discord::Result::Ok && app_id > 0)
{
discord::User user{};
core->UserManager().GetCurrentUser(&user);
userdict["avatar"] = user.GetAvatar(); // can be empty when user has no avatar
userdict["is_bot"] = user.GetBot();
userdict["discriminator"] = user.GetDiscriminator();
userdict["id"] = user.GetId();
userdict["username"] = user.GetUsername();
if (String(userdict["avatar"]).is_empty())
userdict["avatar_url"] = String(std::string("https://cdn.discordapp.com/embed/avatars/" + std::to_string((userdict["discriminator"].INT % 5) - 1) + ".png").c_str());
else
userdict["avatar_url"] = String(std::string("https://cdn.discordapp.com/avatars/" + std::to_string(user.GetId()) + "/" + user.GetAvatar() + ".png?size=512").c_str());
userdict.make_read_only();
}
return userdict;
}
bool discord_sdk::get_is_discord_working() bool discord_sdk::get_is_discord_working()
{ {

View File

@@ -102,6 +102,9 @@ public:
void register_command(String value); void register_command(String value);
void register_steam(int32_t value); void register_steam(int32_t value);
Dictionary get_current_user();
void set_current_user(Dictionary value);
bool get_is_discord_working(); bool get_is_discord_working();
int get_result_int(); int get_result_int();
}; };