question

Ben avatar image
Ben asked

How to ban a user through Azure-Functions?

Please somebody help me, I tried to create a function for many hours now, I tried a lot of snippets and all, but im just confused how those functions work. Please no links to the ban/server apis, I saw all of them, I just dont understand it. I even watched hour long videos for azure functions, but no luck...

This code snippet looks easy, but I tried to put that in a azure function for hours now:

https://docs.microsoft.com/en-us/gaming/playfab/features/data/playerdata/player-bans

This is what I have (with a lot of wrong code, I know), I tried to cobine all snippets I found, googled error codes, but its not working:

public static class BanUserNow
    {
        [FunctionName("BanUserNow")]
      public static async Task<dynamic> BanUserNowOk([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) 
      {

       var context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
            var args = context.FunctionArgument;

            var argsPlayerID = (string)args["profileID"] ?? string.Empty;
            var profileID = string.IsNullOrEmpty(argsPlayerID) ? context.CallerEntityProfile.Lineage.MasterPlayerAccountId : argsPlayerID;


        PlayFabServerAPI.BanUsersAsync(new BanUsersRequest() {
        Bans = new List<BanRequest>() {
            new BanRequest() {
                DurationInHours = 6,
                PlayFabId = profileID,
                Reason = "Automatic ban for WH",
                
            }
        }
    });
    
}

  
}
}
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

Here is a Function code example you can refer to:

public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
            var context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
            PlayFabSettings.staticSettings.TitleId = context.TitleAuthenticationContext.Id;
            PlayFabSettings.staticSettings.DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process);
            var args = context.FunctionArgument;
            var argsPlayerID = (string)args["profileID"];
            var profileID = string.IsNullOrEmpty(argsPlayerID) ? context.CallerEntityProfile.Lineage.MasterPlayerAccountId : argsPlayerID;
            var result = await PlayFabServerAPI.BanUsersAsync(new BanUsersRequest()
            {
                Bans = new List<BanRequest>() { new BanRequest() {
                	DurationInHours = 6,
                	PlayFabId = profileID,
                	Reason = "Automatic ban for WH",
            		}
        		}
            });
            return new OkObjectResult(result);
        }
    }

In addition, to use server API, you need to set title id and DeveloperSecretKey first like the code above. For more information about Azure Function, you can refer to PlayFab CloudScript using Azure Functions.

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.