Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Guild incident actions #2783

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/GuildIncidentsData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace Discord;

public class GuildIncidentsData
{
/// <summary>
/// Gets the time when invites get enabled again. <see langword="null"/> if invites are not disabled.
/// </summary>
public DateTimeOffset? InvitesDisabledUntil { get; set; }

/// <summary>
/// Gets the time when DMs get enabled again. <see langword="null"/> if DMs are not disabled.
/// </summary>
public DateTimeOffset? DmsDisabledUntil { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace Discord;

public class GuildIncidentsDataProperties
{
/// <summary>
/// Gets or set when invites get enabled again, up to 24 hours in the future.
/// </summary>
/// <remarks>
/// Set to <see langword="null"/> to enable invites.
/// </remarks>
public Optional<DateTimeOffset?> InvitesDisabledUntil { get; set; }

/// <summary>
/// Gets or set when dms get enabled again, up to 24 hours in the future.
/// </summary>
/// <remarks>
/// Set to <see langword="null"/> to enable dms.
/// </remarks>
public Optional<DateTimeOffset?> DmsDisabledUntil { get; set; }
}
13 changes: 13 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// </summary>
ulong MaxUploadLimit { get; }

/// <summary>
/// Gets the incidents data for this guild.
/// </summary>
GuildIncidentsData IncidentsData { get; }

/// <summary>
/// Modifies this guild.
/// </summary>
Expand Down Expand Up @@ -1325,5 +1330,13 @@ Task<IReadOnlyCollection<IApplicationCommand>> BulkOverwriteApplicationCommandsA
/// A task that represents the asynchronous creation operation. The task result contains the modified <see cref="IGuildOnboarding"/>.
/// </returns>
Task<IGuildOnboarding> ModifyOnboardingAsync(Action<GuildOnboardingProperties> props, RequestOptions options = null);

/// <summary>
/// Modifies the incident actions of the guild.
/// </summary>
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the modified <see cref="IncidentsData"/>.
/// </returns>
Task<GuildIncidentsData> ModifyIncidentActionsAsync(Action<GuildIncidentsDataProperties> props, RequestOptions options = null);
}
}
3 changes: 3 additions & 0 deletions src/Discord.Net.Rest/API/Common/Guild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,8 @@ internal class Guild

[JsonProperty("max_stage_video_channel_users")]
public Optional<int> MaxStageVideoChannelUsers { get; set; }

[JsonProperty("incidents_data")]
public GuildIncidentsData IncidentsData { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/Discord.Net.Rest/API/Common/GuildIncidentsData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;
using System;

namespace Discord.API;

internal class GuildIncidentsData
{
[JsonProperty("invites_disabled_until")]
public DateTimeOffset? InvitesDisabledUntil { get; set; }

[JsonProperty("dms_disabled_until")]
public DateTimeOffset? DmsDisabledUntil { get; set; }
}
13 changes: 13 additions & 0 deletions src/Discord.Net.Rest/API/Rest/ModifyGuildIncidentsDataParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;
using System;

namespace Discord.API.Rest;

internal class ModifyGuildIncidentsDataParams
{
[JsonProperty("invites_disabled_until")]
public Optional<DateTimeOffset?> InvitesDisabledUntil { get; set; }

[JsonProperty("dms_disabled_until")]
public Optional<DateTimeOffset?> DmsDisabledUntil { get; set; }
}
10 changes: 10 additions & 0 deletions src/Discord.Net.Rest/DiscordRestApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,16 @@ public async Task<GetGuildPruneCountResponse> GetGuildPruneCountAsync(ulong guil
var ids = new BucketIds(guildId: guildId);
return await SendAsync<GetGuildPruneCountResponse>("GET", () => $"guilds/{guildId}/prune?days={args.Days}{endpointRoleIds}", ids, options: options).ConfigureAwait(false);
}

public async Task<GuildIncidentsData> ModifyGuildIncidentActionsAsync(ulong guildId, ModifyGuildIncidentsDataParams args, RequestOptions options = null)
{
Preconditions.NotEqual(guildId, 0, nameof(guildId));

var ids = new BucketIds(guildId: guildId);

return await SendJsonAsync<GuildIncidentsData>("PUT", () => $"guilds/{guildId}/incident-actions", args, ids, options: options).ConfigureAwait(false);
}

#endregion

#region Guild Bans
Expand Down
18 changes: 18 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ public static ulong GetUploadLimit(IGuild guild)
var mebibyte = Math.Pow(2, 20);
return (ulong)(tierFactor * mebibyte);
}

public static async Task<GuildIncidentsData> ModifyGuildIncidentActionsAsync(IGuild guild, BaseDiscordClient client, Action<GuildIncidentsDataProperties> func, RequestOptions options = null)
{
var props = new GuildIncidentsDataProperties();
func(props);

var args = props.DmsDisabledUntil.IsSpecified || props.InvitesDisabledUntil.IsSpecified
? new ModifyGuildIncidentsDataParams { DmsDisabledUntil = props.DmsDisabledUntil, InvitesDisabledUntil = props.InvitesDisabledUntil }
: null;

var model = await client.ApiClient.ModifyGuildIncidentActionsAsync(guild.Id, args, options);

return new GuildIncidentsData
{
DmsDisabledUntil = model.DmsDisabledUntil,
InvitesDisabledUntil = model.InvitesDisabledUntil
};
}
#endregion

#region Bans
Expand Down
16 changes: 16 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public ulong MaxUploadLimit
/// <inheritdoc />
public GuildFeatures Features { get; private set; }

/// <inheritdoc/>
public GuildIncidentsData IncidentsData { get; private set; }

/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);

Expand Down Expand Up @@ -181,6 +184,10 @@ internal void Update(Model model)
Description = model.Description;
PremiumSubscriptionCount = model.PremiumSubscriptionCount.GetValueOrDefault();
NsfwLevel = model.NsfwLevel;
IncidentsData = model.IncidentsData is not null
? new GuildIncidentsData { DmsDisabledUntil = model.IncidentsData.DmsDisabledUntil, InvitesDisabledUntil = model.IncidentsData.InvitesDisabledUntil }
: new GuildIncidentsData();

if (model.MaxPresences.IsSpecified)
MaxPresences = model.MaxPresences.Value ?? 25000;
if (model.MaxMembers.IsSpecified)
Expand Down Expand Up @@ -303,6 +310,15 @@ public async Task ReorderRolesAsync(IEnumerable<ReorderRoleProperties> args, Req
/// <inheritdoc />
public Task LeaveAsync(RequestOptions options = null)
=> GuildHelper.LeaveAsync(this, Discord, options);

/// <inheritdoc />
public async Task<GuildIncidentsData> ModifyIncidentActionsAsync(Action<GuildIncidentsDataProperties> props, RequestOptions options = null)
{
IncidentsData = await GuildHelper.ModifyGuildIncidentActionsAsync(this, Discord, props, options);

return IncidentsData;
}

#endregion

#region Interactions
Expand Down
10 changes: 10 additions & 0 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public class SocketGuild : SocketEntity<ulong>, IGuild, IDisposable
public bool IsBoostProgressBarEnabled { get; private set; }
/// <inheritdoc />
public GuildFeatures Features { get; private set; }
/// <inheritdoc/>
public GuildIncidentsData IncidentsData { get; private set; }

/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
Expand Down Expand Up @@ -560,6 +562,9 @@ internal void Update(ClientState state, Model model)
PreferredCulture = PreferredLocale == null ? null : new CultureInfo(PreferredLocale);
if (model.IsBoostProgressBarEnabled.IsSpecified)
IsBoostProgressBarEnabled = model.IsBoostProgressBarEnabled.Value;
IncidentsData = model.IncidentsData is not null
? new GuildIncidentsData { DmsDisabledUntil = model.IncidentsData.DmsDisabledUntil, InvitesDisabledUntil = model.IncidentsData.InvitesDisabledUntil }
: new GuildIncidentsData();
if (model.Emojis != null)
{
var emojis = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length);
Expand Down Expand Up @@ -656,6 +661,11 @@ public Task ReorderRolesAsync(IEnumerable<ReorderRoleProperties> args, RequestOp
/// <inheritdoc />
public Task LeaveAsync(RequestOptions options = null)
=> GuildHelper.LeaveAsync(this, Discord, options);

/// <inheritdoc />
public Task<GuildIncidentsData> ModifyIncidentActionsAsync(Action<GuildIncidentsDataProperties> props, RequestOptions options = null)
=> GuildHelper.ModifyGuildIncidentActionsAsync(this, Discord, props, options);

#endregion

#region Bans
Expand Down