[Feature] Scheduled event recurrence rule (#3023)

* api modelsssssssss

* recurrence rulesssss
This commit is contained in:
Mihail Gribkov
2024-12-21 04:16:11 +08:00
committed by GitHub
parent 438e31edd1
commit b7208d4d99
24 changed files with 585 additions and 131 deletions

View File

@@ -1273,6 +1273,55 @@ namespace Discord.Rest
}
}
if (args.RecurrenceRule is { IsSpecified: true, Value: not null })
{
var rule = args.RecurrenceRule.Value;
var hasByWeekDay = rule.ByWeekday?.Any() ?? false;
var hasByNWeekDay = rule.ByNWeekday?.Any() ?? false;
var hasByMonth = (rule.ByMonthDay?.Any() ?? false) || (rule.ByMonth?.Any() ?? false);
if (hasByWeekDay && hasByNWeekDay ||
hasByWeekDay && hasByMonth ||
hasByNWeekDay && hasByMonth)
{
throw new ArgumentException($"A recurrence rule can have one of ('{nameof(rule.ByWeekday)}', '{nameof(rule.ByNWeekday)}', '{nameof(rule.ByMonth)}' + '{nameof(rule.ByMonthDay)}'), but not a combination of them.");
}
if (hasByWeekDay)
{
if (rule.Frequency is not RecurrenceFrequency.Daily and not RecurrenceFrequency.Weekly)
throw new ArgumentException($"A {nameof(rule.ByWeekday)} rule can only be used with {nameof(rule.Frequency)} of 'Daily' or 'Weekly'.");
if (rule.Frequency is RecurrenceFrequency.Weekly)
{
if (rule.ByWeekday.Count != 1)
throw new ArgumentException("A 'Weekly' recurrence rule must have a single weekday selected.");
if (rule.Interval == 1)
throw new ArgumentException($"{nameof(rule.Interval)} can only be set to a value other than '1' when {nameof(rule.Frequency)} is set to 'Weekly'");
}
}
if (hasByNWeekDay)
{
if (rule.Frequency is not RecurrenceFrequency.Monthly)
throw new ArgumentException($"A {rule.ByNWeekday} rule must have {nameof(rule.Frequency)} set to 'Monthly'.");
if (rule.ByNWeekday.Count != 1)
throw new ArgumentException($"A {rule.ByNWeekday} must have exactly one day selected.");
}
if (hasByMonth)
{
if (rule.Frequency is not RecurrenceFrequency.Yearly)
throw new ArgumentException($"A {rule.ByMonth} rule must have {nameof(rule.Frequency)} set to 'Yearly'.");
if (rule.ByMonth?.Count is not 1 || rule.ByMonthDay?.Count is not 1)
throw new ArgumentException($"A {rule.ByMonth} rule must have exactly 1 day and 1 month selected.");
}
}
var apiArgs = new ModifyGuildScheduledEventParams()
{
ChannelId = args.ChannelId,
@@ -1284,10 +1333,11 @@ namespace Discord.Rest
Status = args.Status,
Type = args.Type,
Image = args.CoverImage.IsSpecified
? args.CoverImage.Value.HasValue
? args.CoverImage.Value.Value.ToModel()
: null
: Optional<ImageModel?>.Unspecified
? args.CoverImage.Value?.ToModel()
: Optional<ImageModel?>.Unspecified,
RecurrenceRule = args.RecurrenceRule.IsSpecified
? args.RecurrenceRule.Value?.ToModel()
: Optional<API.GuildScheduledEventRecurrenceRule>.Unspecified
};
if (args.Location.IsSpecified)
@@ -1328,7 +1378,8 @@ namespace Discord.Rest
ulong? channelId = null,
string location = null,
Image? bannerImage = null,
RequestOptions options = null)
RequestOptions options = null,
GuildScheduledEventRecurrenceRuleProperties recurrenceRule = null)
{
if (location != null)
{
@@ -1353,6 +1404,53 @@ namespace Discord.Rest
if (endTime != null && endTime <= startTime)
throw new ArgumentOutOfRangeException(nameof(endTime), $"{nameof(endTime)} cannot be before the start time");
if (recurrenceRule is not null)
{
var hasByWeekDay = recurrenceRule.ByWeekday?.Any() ?? false;
var hasByNWeekDay = recurrenceRule.ByNWeekday?.Any() ?? false;
var hasByMonth = (recurrenceRule.ByMonthDay?.Any() ?? false) || (recurrenceRule.ByMonth?.Any() ?? false);
if (hasByWeekDay && hasByNWeekDay ||
hasByWeekDay && hasByMonth ||
hasByNWeekDay && hasByMonth)
{
throw new ArgumentException($"A recurrence rule can have one of ('{nameof(recurrenceRule.ByWeekday)}', '{nameof(recurrenceRule.ByNWeekday)}', '{nameof(recurrenceRule.ByMonth)}' + '{nameof(recurrenceRule.ByMonthDay)}'), but not a combination of them.");
}
if (hasByWeekDay)
{
if (recurrenceRule.Frequency is not RecurrenceFrequency.Daily and not RecurrenceFrequency.Weekly)
throw new ArgumentException($"A {nameof(recurrenceRule.ByWeekday)} rule can only be used with {nameof(recurrenceRule.Frequency)} of 'Daily' or 'Weekly'.");
if (recurrenceRule.Frequency is RecurrenceFrequency.Weekly)
{
if (recurrenceRule.ByWeekday.Count != 1)
throw new ArgumentException("A 'Weekly' recurrence rule must have a single weekday selected.");
if (recurrenceRule.Interval == 1)
throw new ArgumentException($"{nameof(recurrenceRule.Interval)} can only be set to a value other than '1' when {nameof(recurrenceRule.Frequency)} is set to 'Weekly'");
}
}
if (hasByNWeekDay)
{
if (recurrenceRule.Frequency is not RecurrenceFrequency.Monthly)
throw new ArgumentException($"A {recurrenceRule.ByNWeekday} rule must have {nameof(recurrenceRule.Frequency)} set to 'Monthly'.");
if (recurrenceRule.ByNWeekday.Count != 1)
throw new ArgumentException($"A {recurrenceRule.ByNWeekday} must have exactly one day selected.");
}
if (hasByMonth)
{
if (recurrenceRule.Frequency is not RecurrenceFrequency.Yearly)
throw new ArgumentException($"A {recurrenceRule.ByMonth} rule must have {nameof(recurrenceRule.Frequency)} set to 'Yearly'.");
if (recurrenceRule.ByMonth?.Count is not 1 || recurrenceRule.ByMonthDay?.Count is not 1)
throw new ArgumentException($"A {recurrenceRule.ByMonth} rule must have exactly 1 day and 1 month selected.");
}
}
var apiArgs = new CreateGuildScheduledEventParams()
{
@@ -1363,7 +1461,8 @@ namespace Discord.Rest
PrivacyLevel = privacyLevel,
StartTime = startTime,
Type = type,
Image = bannerImage.HasValue ? bannerImage.Value.ToModel() : Optional<ImageModel>.Unspecified
Image = bannerImage?.ToModel() ?? Optional<ImageModel>.Unspecified,
RecurrenceRule = recurrenceRule?.ToModel() ?? Optional<API.GuildScheduledEventRecurrenceRule>.Unspecified
};
if (location != null)