Skip to content

Commit

Permalink
Merge pull request #398 from Mahsaap/Add_Warn_Chat_User
Browse files Browse the repository at this point in the history
Add WarnChatUser Endpoint
  • Loading branch information
Mahsaap authored Jun 23, 2024
2 parents 978edaf + 8ce7155 commit 1c7af68
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Newtonsoft.Json;

namespace TwitchLib.Api.Helix.Models.Moderation.WarnChatUser.Request;

/// <summary>
/// Request that contains information about the warning.
/// </summary>
public class WarnChatUserRequest
{
/// <summary>
/// The ID of the twitch user to be warned.
/// </summary>
[JsonProperty(PropertyName = "user_id")]
public string UserId { get; set; }

/// <summary>
/// A custom reason for the warning. Max 500 chars.
/// </summary>
[JsonProperty(PropertyName = "reason")]
public string Reason { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;

namespace TwitchLib.Api.Helix.Models.Moderation.WarnChatUser;

/// <summary>
/// Warn chat user response object.
/// </summary>
public class WarnChatUserResponse
{
/// <summary>
/// A list that contains information about the warning.
/// </summary>
[JsonProperty(PropertyName = "data")]
public WarnedChatUser[] Data { get; protected set; }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Newtonsoft.Json;

namespace TwitchLib.Api.Helix.Models.Moderation.WarnChatUser;

/// <summary>
/// Contains information about the warning.
/// </summary>
public class WarnedChatUser
{
/// <summary>
/// The ID of the channel in which the warning will take effect.
/// </summary>
[JsonProperty(PropertyName = "broadcaster_id")]
public string BroadcasterId { get; protected set; }

/// <summary>
/// The ID of the warned user.
/// </summary>
[JsonProperty(PropertyName = "user_id")]
public string UserId { get; protected set; }

/// <summary>
/// The ID of the user who applied the warning.
/// </summary>
[JsonProperty(PropertyName = "moderator_id")]
public string ModeratorId { get; protected set; }

/// <summary>
/// The reason provided for warning.
/// </summary>
[JsonProperty(PropertyName = "reason")]
public string Reason { get; protected set; }
}

48 changes: 48 additions & 0 deletions TwitchLib.Api.Helix/Moderation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -810,5 +812,51 @@ public Task<GetModeratedChannelsResponse> GetModeratedChannelsAsync(string userI
}

#endregion

#region WarnChatUser
/// <summary>
/// Warns a user in the specified broadcaster’s chat room, preventing them from chat interaction until the warning is acknowledged.
/// <para>New warnings can be issued to a user when they already have a warning in the channel (new warning will replace old warning).</para>
/// <para>Requires a user access token that includes the moderator:manage:warnings scope.</para>
/// <para>Query parameter moderator_id must match the user_id in the user access token.</para>
/// </summary>
/// <param name="broadcasterId">The ID of the channel in which the warning will take effect.</param>
/// <param name="moderatorId">The ID of the twitch user who requested the warning.</param>
/// <param name="warnChatUserRequest">request object contains information about the warning.</param>
/// <param name="accessToken">optional access token to override the one used while creating the TwitchAPI object</param>
/// <returns cref="WarnChatUserResponse"></returns>
/// <exception cref="BadParameterException"></exception>
public Task<WarnChatUserResponse> 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<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("broadcaster_id", broadcasterId),
new KeyValuePair<string, string>("moderator_id", moderatorId)
};

var body = new
{
data = warnChatUserRequest
};

return TwitchPostGenericAsync<WarnChatUserResponse>("/moderation/warnings", ApiVersion.Helix, JsonConvert.SerializeObject(body), getParams, accessToken);
}

#endregion
}
}

0 comments on commit 1c7af68

Please sign in to comment.