question

jdeegz avatar image
jdeegz asked

How can I send PlayFab statistic (leaderboard) updated to Discord?

Hi!

I am a dev learning how to program. I have followed a tutorial that allows me to use the Revisions view to create a Cloud Script function that will send a Discord message to my channel when there is a new user registration.

I am trying to figure out how to build upon this concept by creating a function that is triggered by the event: player_statistic_changed assigned to fire off the function:

handlers.newScorePosted = function(args){ var contentBody = { "content": "New Score from" + DisplayName + ": " + StatisticValue + "on mission: " + StatisticName + "!" }; var url = "[Redacted URL]"; var method = "post"; var contentType = "application/json"; var headers = {}; var responseString = http.request(url,method,JSON.stringify(contentBody),contentType,headers); }

But I am not receiving any messages in Discord. I'm taking shots in the dark to try and figure out how to pass dynamic content into my message's string.

Any help would be greatly appreciated.

P.S. Would it be possible to determine if the score (value) is in the top-10 of a leaderboard, and only send a Discord message if so?

PlayStream
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Xiao Zha avatar image
Xiao Zha answered

You could get the dynamic DisplayName, StatisticValue and StatisticName in the Cloud Script context. Here is the code example you may refer to:

 handlers.Test=function(args,context)
 {
     log.debug(context);
        
     //Ensure variables are defined
     var DisplayName = context.playerProfile.DisplayName || 'Unknown Player';
     var StatisticValue = context.playStreamEvent.StatisticValue || 'Unknown Value';
     var StatisticName = context.playStreamEvent.StatisticName || 'Unknown Statistic';
    
     var contentBody = {
         "content": "New Score from " + DisplayName + ": " + StatisticValue + " on mission: " + StatisticName + "!"
     };
    
     var url = "your_discord_webhook";
     var method = "post";
     var contentType = "application/json";
     var headers = {};
    
     try {
         var responseString = http.request(url, method, JSON.stringify(contentBody), contentType, headers);
     } catch (error) {
         log.error('Error sending Discord message: ' + error.message);
     }
    
 }

In addition, you could call GetLeaderboard API to get the score of the 10th player in the leaderboard and compare it to the updated score to determine if the score (value) is in the top-10.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.