From 7d29c6ac3c0ed7bb25e1668091fdfec5fbeca9ea Mon Sep 17 00:00:00 2001 From: Mahsaap Date: Thu, 20 Jun 2024 22:19:15 -0300 Subject: [PATCH 1/2] Add WarnChatUser Endpoint --- .../Request/WarnChatUserRequest.cs | 22 +++++++++ .../WarnChatUser/WarnChatUserResponse.cs | 16 +++++++ .../Moderation/WarnChatUser/WarnedChatUser.cs | 34 +++++++++++++ TwitchLib.Api.Helix/Moderation.cs | 48 +++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/Request/WarnChatUserRequest.cs create mode 100644 TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/WarnChatUserResponse.cs create mode 100644 TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/WarnedChatUser.cs diff --git a/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/Request/WarnChatUserRequest.cs b/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/Request/WarnChatUserRequest.cs new file mode 100644 index 00000000..4a167fc2 --- /dev/null +++ b/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/Request/WarnChatUserRequest.cs @@ -0,0 +1,22 @@ +using Newtonsoft.Json; + +namespace TwitchLib.Api.Helix.Models.Moderation.WarnChatUser.Request; + +/// +/// Request that contains information about the warning. +/// +public class WarnChatUserRequest +{ + /// + /// The ID of the twitch user to be warned. + /// + [JsonProperty(PropertyName = "user_id")] + public string UserId { get; set; } + + /// + /// A custom reason for the warning. Max 500 chars. + /// + [JsonProperty(PropertyName = "reason")] + public string Reason { get; set; } = string.Empty; +} +} diff --git a/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/WarnChatUserResponse.cs b/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/WarnChatUserResponse.cs new file mode 100644 index 00000000..8d19341b --- /dev/null +++ b/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/WarnChatUserResponse.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace TwitchLib.Api.Helix.Models.Moderation.WarnChatUser; + +/// +/// Warn chat user response object. +/// +public class WarnChatUserResponse +{ + /// + /// A list that contains information about the warning. + /// + [JsonProperty(PropertyName = "data")] + public WarnedChatUser[] Data { get; protected set; } +} + diff --git a/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/WarnedChatUser.cs b/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/WarnedChatUser.cs new file mode 100644 index 00000000..16e5574d --- /dev/null +++ b/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/WarnedChatUser.cs @@ -0,0 +1,34 @@ +using Newtonsoft.Json; + +namespace TwitchLib.Api.Helix.Models.Moderation.WarnChatUser; + +/// +/// Contains information about the warning. +/// +public class WarnedChatUser +{ + /// + /// The ID of the channel in which the warning will take effect. + /// + [JsonProperty(PropertyName = "broadcaster_id")] + public string BroadcasterId { get; protected set; } + + /// + /// The ID of the warned user. + /// + [JsonProperty(PropertyName = "user_id")] + public string UserId { get; protected set; } + + /// + /// The ID of the user who applied the warning. + /// + [JsonProperty(PropertyName = "moderator_id")] + public string ModeratorId { get; protected set; } + + /// + /// The reason provided for warning. + /// + [JsonProperty(PropertyName = "reason")] + public string Reason { get; protected set; } +} + diff --git a/TwitchLib.Api.Helix/Moderation.cs b/TwitchLib.Api.Helix/Moderation.cs index a629c6a2..4c96c7c2 100644 --- a/TwitchLib.Api.Helix/Moderation.cs +++ b/TwitchLib.Api.Helix/Moderation.cs @@ -26,6 +26,8 @@ using TwitchLib.Api.Helix.Models.Moderation.UnbanRequests; using TwitchLib.Api.Helix.Models.Moderation.UnbanRequests.GetUnbanRequests; using TwitchLib.Api.Helix.Models.Moderation.UnbanRequests.ResolveUnbanRequests; +using TwitchLib.Api.Helix.Models.Moderation.WarnChatUser; +using TwitchLib.Api.Helix.Models.Moderation.WarnChatUser.Request; namespace TwitchLib.Api.Helix { @@ -810,5 +812,51 @@ public Task GetModeratedChannelsAsync(string userI } #endregion + + #region WarnChatUser + /// + /// Warns a user in the specified broadcaster’s chat room, preventing them from chat interaction until the warning is acknowledged. + /// New warnings can be issued to a user when they already have a warning in the channel (new warning will replace old warning). + /// Requires a user access token that includes the moderator:manage:warnings scope. + /// Query parameter moderator_id must match the user_id in the user access token. + /// + /// The ID of the channel in which the warning will take effect. + /// The ID of the twitch user who requested the warning. + /// request object contains information about the warning. + /// optional access token to override the one used while creating the TwitchAPI object + /// + /// + public Task WarnChatUserAsync(string broadcasterId, string moderatorId, WarnChatUserRequest warnChatUserRequest, string accessToken = null) + { + if (string.IsNullOrEmpty(broadcasterId)) + throw new BadParameterException("broadcasterId must be set"); + + if (string.IsNullOrEmpty(moderatorId)) + throw new BadParameterException("moderatorId must be set"); + + if (warnChatUserRequest == null) + throw new BadParameterException("warnChatUserRequest cannot be null"); + + if (string.IsNullOrWhiteSpace(warnChatUserRequest.UserId)) + throw new BadParameterException("warnChatUserRequest.UserId must be set"); + + if (warnChatUserRequest.Reason.Length > 500) + throw new BadParameterException("warnChatUserRequest.Reason can't be greater then 500 characters."); + + var getParams = new List> + { + new KeyValuePair("broadcaster_id", broadcasterId), + new KeyValuePair("moderator_id", moderatorId) + }; + + var body = new + { + data = warnChatUserRequest + }; + + return TwitchPostGenericAsync("/moderation/warnings", ApiVersion.Helix, JsonConvert.SerializeObject(body), getParams, accessToken); + } + + #endregion } } From 8ce7155c7c02649f4c6771140d0a105466dcdf64 Mon Sep 17 00:00:00 2001 From: Mahsaap Date: Thu, 20 Jun 2024 22:21:45 -0300 Subject: [PATCH 2/2] Fix typo (bracket) --- .../Moderation/WarnChatUser/Request/WarnChatUserRequest.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/Request/WarnChatUserRequest.cs b/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/Request/WarnChatUserRequest.cs index 4a167fc2..977d439c 100644 --- a/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/Request/WarnChatUserRequest.cs +++ b/TwitchLib.Api.Helix.Models/Moderation/WarnChatUser/Request/WarnChatUserRequest.cs @@ -18,5 +18,4 @@ public class WarnChatUserRequest /// [JsonProperty(PropertyName = "reason")] public string Reason { get; set; } = string.Empty; -} -} +} \ No newline at end of file