Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Feb 19, 2023
2 parents b7306d1 + ca1a0ed commit 8b15df3
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 173 deletions.
30 changes: 0 additions & 30 deletions Present/Commands/GiveawayCommand.Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,4 @@ public async Task CreateAsync(InteractionContext context,
followup.AddEmbed(embed);
await context.FollowUpAsync(followup).ConfigureAwait(false);
}

private static async Task<DateTimeOffset> ValidateTimeStamp(
InteractionContext context,
DiscordModalTextInput timeInput,
DiscordEmbedBuilder embed,
DiscordFollowupMessageBuilder followup
)
{
if (!TimeStampUtility.TryParse(timeInput.Value, out DateTimeOffset endTime))
{
Logger.Warn($"Provided time was invalid ({timeInput.Value}). Giveaway creation has been cancelled");
embed.WithDescription(EmbedStrings.GiveawayCreation_InvalidTimestamp);
followup.AsEphemeral();
followup.AddEmbed(embed);
await context.FollowUpAsync(followup).ConfigureAwait(false);
return default;
}

if (endTime < DateTimeOffset.UtcNow)
{
Logger.Warn($"Provided time ({timeInput.Value}) is in the past. Giveaway creation has been cancelled");
embed.WithDescription(EmbedStrings.GiveawayCreation_FutureTimestamp);
followup.AsEphemeral();
followup.AddEmbed(embed);
await context.FollowUpAsync(followup).ConfigureAwait(false);
return default;
}

return endTime;
}
}
12 changes: 2 additions & 10 deletions Present/Commands/GiveawayCommand.End.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CSharpVitamins;
using DSharpPlus.Entities;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using Present.Data;
Expand All @@ -13,20 +12,13 @@ internal sealed partial class GiveawayCommand
[SlashCommand(CommandNames.End, CommandDescriptions.End, false)]
[SlashRequireGuild]
public async Task EndAsync(InteractionContext context,
[Option(OptionNames.Id, OptionDescriptions.EndGiveawayId)] string idRaw
[Option(OptionNames.Id, OptionDescriptions.EndGiveawayId)] long giveawayId
)
{
var embed = new DiscordEmbedBuilder();
embed.WithColor(DiscordColor.Red);
embed.WithTitle(EmbedStrings.InvalidGiveawayId);

if (!ShortGuid.TryParse(idRaw, out ShortGuid giveawayId))
{
embed.WithDescription(string.Format(EmbedStrings.InvalidId, idRaw));
await context.CreateResponseAsync(embed, true).ConfigureAwait(false);
return;
}

DiscordGuild guild = context.Guild;
if (!_giveawayService.TryGetGiveaway(giveawayId, out Giveaway? giveaway) || giveaway.GuildId != guild.Id)
{
Expand Down
28 changes: 12 additions & 16 deletions Present/Commands/GiveawayCommand.Redraw.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CSharpVitamins;
using DSharpPlus.Entities;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using Present.Data;
Expand All @@ -12,21 +11,14 @@ internal sealed partial class GiveawayCommand
[SlashCommand(CommandNames.Redraw, CommandDescriptions.Redraw, false)]
[SlashRequireGuild]
public async Task RedrawAsync(InteractionContext context,
[Option(OptionNames.Id, OptionDescriptions.RedrawGiveawayId)] string idRaw,
[Option(OptionNames.Id, OptionDescriptions.RedrawGiveawayId)] long giveawayId,
[Option(OptionNames.KeepIds, OptionDescriptions.RedrawKeep)] string? keepIds = null
)
{
var embed = new DiscordEmbedBuilder();
embed.WithColor(DiscordColor.Red);
embed.WithTitle(EmbedStrings.InvalidGiveawayId);

if (!ShortGuid.TryParse(idRaw, out ShortGuid giveawayId))
{
embed.WithDescription(string.Format(EmbedStrings.InvalidId, idRaw));
await context.CreateResponseAsync(embed, true).ConfigureAwait(false);
return;
}

DiscordGuild guild = context.Guild;
if (!_giveawayService.TryGetGiveaway(giveawayId, out Giveaway? giveaway) || giveaway.GuildId != guild.Id)
{
Expand All @@ -49,22 +41,26 @@ public async Task RedrawAsync(InteractionContext context,
{
foreach (string keepId in keepIds.Split((char[]?) null, StringSplitOptions.RemoveEmptyEntries))
{
if (ulong.TryParse(keepId, out ulong userId) &&
_giveawayService.ValidateUser(userId, guild, out DiscordMember? member))
keep.Add(member);
else
if (!ulong.TryParse(keepId, out ulong userId))
{
invalidKeepIds.Add(keepId);
continue;
}

DiscordMember? member = await _giveawayService.ValidateUserAsync(userId, giveaway).ConfigureAwait(false);
if (member is null) invalidKeepIds.Add(keepId);
else keep.Add(member);
}
}

IReadOnlyList<DiscordMember> winners = _giveawayService.SelectWinners(giveaway, keep);
IReadOnlyList<DiscordMember> winners = await _giveawayService.SelectWinnersAsync(giveaway, keep).ConfigureAwait(false);
await _giveawayService.UpdateWinnersAsync(giveaway, winners).ConfigureAwait(false);

await _giveawayService.EditGiveawayAsync(giveaway).ConfigureAwait(false);
await _giveawayService.UpdateGiveawayLogMessageAsync(giveaway).ConfigureAwait(false);
await _giveawayService.UpdateGiveawayPublicMessageAsync(giveaway).ConfigureAwait(false);

embed = _giveawayService.CreateGiveawayInformationEmbed(giveaway);
embed = await _giveawayService.CreateGiveawayInformationEmbedAsync(giveaway).ConfigureAwait(false);
embed.WithColor(DiscordColor.Green);
embed.WithTitle(EmbedStrings.GiveawayEdited_Title);

Expand Down
14 changes: 3 additions & 11 deletions Present/Commands/GiveawayCommand.SetWinners.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CSharpVitamins;
using DSharpPlus.Entities;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using Present.Data;
Expand All @@ -12,21 +11,14 @@ internal sealed partial class GiveawayCommand
[SlashCommand(CommandNames.SetWinners, CommandDescriptions.End, false)]
[SlashRequireGuild]
public async Task SetWinnersAsync(InteractionContext context,
[Option(OptionNames.Id, OptionDescriptions.EndGiveawayId)] string idRaw,
[Option(OptionNames.Id, OptionDescriptions.EndGiveawayId)] long giveawayId,
[Option(OptionNames.WinnerCount, OptionDescriptions.WinnerCount)] long winnerCount
)
{
var embed = new DiscordEmbedBuilder();
embed.WithColor(DiscordColor.Red);
embed.WithTitle(EmbedStrings.InvalidGiveawayId);

if (!ShortGuid.TryParse(idRaw, out ShortGuid giveawayId))
{
embed.WithDescription(string.Format(EmbedStrings.InvalidId, idRaw));
await context.CreateResponseAsync(embed, true).ConfigureAwait(false);
return;
}

DiscordGuild guild = context.Guild;
if (!_giveawayService.TryGetGiveaway(giveawayId, out Giveaway? giveaway) || giveaway.GuildId != guild.Id)
{
Expand Down Expand Up @@ -55,7 +47,7 @@ public async Task SetWinnersAsync(InteractionContext context,
await _giveawayService.UpdateGiveawayLogMessageAsync(giveaway).ConfigureAwait(false);
await _giveawayService.UpdateGiveawayPublicMessageAsync(giveaway).ConfigureAwait(false);

embed = _giveawayService.CreateGiveawayInformationEmbed(giveaway);
embed = await _giveawayService.CreateGiveawayInformationEmbedAsync(giveaway).ConfigureAwait(false);
embed.WithColor(DiscordColor.Green);
embed.WithTitle(EmbedStrings.GiveawayEdited_Title);
await context.CreateResponseAsync(embed).ConfigureAwait(false);
Expand Down
14 changes: 3 additions & 11 deletions Present/Commands/GiveawayCommand.View.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CSharpVitamins;
using DSharpPlus.Entities;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using Present.Data;
Expand All @@ -12,20 +11,13 @@ internal sealed partial class GiveawayCommand
[SlashCommand(CommandNames.View, CommandDescriptions.ViewGiveaway, false)]
[SlashRequireGuild]
public async Task ViewAsync(InteractionContext context,
[Option(OptionNames.Id, OptionDescriptions.ViewGiveawayId)] string idRaw
[Option(OptionNames.Id, OptionDescriptions.ViewGiveawayId)] long giveawayId
)
{
var embed = new DiscordEmbedBuilder();
embed.WithColor(DiscordColor.Red);
embed.WithTitle(EmbedStrings.InvalidGiveawayId);

if (!ShortGuid.TryParse(idRaw, out ShortGuid giveawayId))
{
embed.WithDescription(string.Format(EmbedStrings.InvalidId, idRaw));
await context.CreateResponseAsync(embed, true).ConfigureAwait(false);
return;
}

DiscordGuild guild = context.Guild;
if (!_giveawayService.TryGetGiveaway(giveawayId, out Giveaway? giveaway) || giveaway.GuildId != guild.Id)
{
Expand All @@ -34,7 +26,7 @@ public async Task ViewAsync(InteractionContext context,
return;
}

embed = _giveawayService.CreateGiveawayInformationEmbed(giveaway);
embed = await _giveawayService.CreateGiveawayInformationEmbedAsync(giveaway).ConfigureAwait(false);
embed.WithTitle(EmbedStrings.GiveawayInformation);

await context.CreateResponseAsync(embed).ConfigureAwait(false);
Expand Down
12 changes: 2 additions & 10 deletions Present/Commands/GiveawayCommand.ViewEntrants.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CSharpVitamins;
using DSharpPlus.Entities;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using Present.Data;
Expand All @@ -13,20 +12,13 @@ internal sealed partial class GiveawayCommand
[SlashCommand(CommandNames.ViewEntrants, CommandDescriptions.ViewEntrants, false)]
[SlashRequireGuild]
public async Task ViewEntrantsAsync(InteractionContext context,
[Option(OptionNames.Id, OptionDescriptions.ViewGiveawayId)] string idRaw
[Option(OptionNames.Id, OptionDescriptions.ViewGiveawayId)] long giveawayId
)
{
var embed = new DiscordEmbedBuilder();
embed.WithColor(DiscordColor.Red);
embed.WithTitle(EmbedStrings.InvalidGiveawayId);

if (!ShortGuid.TryParse(idRaw, out ShortGuid giveawayId))
{
embed.WithDescription(string.Format(EmbedStrings.InvalidId, idRaw));
await context.CreateResponseAsync(embed, true).ConfigureAwait(false);
return;
}

DiscordGuild guild = context.Guild;
if (!_giveawayService.TryGetGiveaway(giveawayId, out Giveaway? giveaway) || giveaway.GuildId != guild.Id)
{
Expand Down
34 changes: 33 additions & 1 deletion Present/Commands/GiveawayCommand.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using DSharpPlus.SlashCommands;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using Present.Resources;
using Present.Services;
using NLog;
using Present.Interactivity;

namespace Present.Commands;

Expand Down Expand Up @@ -41,4 +43,34 @@ UserExclusionService userExclusionService
_roleExclusionService = roleExclusionService;
_userExclusionService = userExclusionService;
}

private static async Task<DateTimeOffset> ValidateTimeStamp(
InteractionContext context,
DiscordModalTextInput timeInput,
DiscordEmbedBuilder embed,
DiscordFollowupMessageBuilder followup
)
{
if (!TimeStampUtility.TryParse(timeInput.Value, out DateTimeOffset endTime))
{
Logger.Warn($"Provided time was invalid ({timeInput.Value}). Giveaway creation has been cancelled");
embed.WithDescription(EmbedStrings.GiveawayCreation_InvalidTimestamp);
followup.AsEphemeral();
followup.AddEmbed(embed);
await context.FollowUpAsync(followup).ConfigureAwait(false);
return default;
}

if (endTime < DateTimeOffset.UtcNow)
{
Logger.Warn($"Provided time ({timeInput.Value}) is in the past. Giveaway creation has been cancelled");
embed.WithDescription(EmbedStrings.GiveawayCreation_FutureTimestamp);
followup.AsEphemeral();
followup.AddEmbed(embed);
await context.FollowUpAsync(followup).ConfigureAwait(false);
return default;
}

return endTime;
}
}
4 changes: 3 additions & 1 deletion Present/Data/EntityConfigurations/GiveawayConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Configure(EntityTypeBuilder<Giveaway> builder)
builder.ToTable(nameof(Giveaway));
builder.HasKey(e => e.Id);

builder.Property(e => e.Id).HasConversion<ShortGuidToBytesConverter>();
builder.Property(e => e.Id);
builder.Property(e => e.StartTime).HasConversion<DateTimeOffsetToBytesConverter>();
builder.Property(e => e.EndTime).HasConversion<DateTimeOffsetToBytesConverter>();
builder.Property(e => e.EndHandled);
Expand All @@ -27,6 +27,8 @@ public void Configure(EntityTypeBuilder<Giveaway> builder)
builder.Property(e => e.Description);
builder.Property(e => e.ImageUri).HasConversion<UriToStringConverter>();
builder.Property(e => e.Entrants).HasConversion<UInt64ListToBytesConverter>();
builder.Property(e => e.ExcludedRoles).HasConversion<UInt64ListToBytesConverter>();
builder.Property(e => e.ExcludedUsers).HasConversion<UInt64ListToBytesConverter>();
builder.Property(e => e.WinnerCount);
builder.Property(e => e.WinnerIds).HasConversion<UInt64ListToBytesConverter>();
builder.Property(e => e.MessageId);
Expand Down
18 changes: 14 additions & 4 deletions Present/Data/Giveaway.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using CSharpVitamins;

namespace Present.Data;
namespace Present.Data;

/// <summary>
/// Represents a giveaway.
Expand Down Expand Up @@ -43,6 +41,18 @@ internal sealed class Giveaway : IEquatable<Giveaway>
/// <value>The end date and time.</value>
public DateTimeOffset EndTime { get; set; }

/// <summary>
/// Gets or sets the list of excluded role IDs for this giveaway.
/// </summary>
/// <value>A <see cref="List{T}" /> of <see cref="ulong" /> representing the excluded role IDs.</value>
public List<ulong> ExcludedRoles { get; set; } = new();

/// <summary>
/// Gets or sets the list of excluded user IDs for this giveaway.
/// </summary>
/// <value>A <see cref="List{T}" /> of <see cref="ulong" /> representing the excluded user IDs.</value>
public List<ulong> ExcludedUsers { get; set; } = new();

/// <summary>
/// Gets or sets the guild ID of the giveaway.
/// </summary>
Expand All @@ -53,7 +63,7 @@ internal sealed class Giveaway : IEquatable<Giveaway>
/// Gets or sets the ID of the giveaway.
/// </summary>
/// <value>The giveaway ID.</value>
public ShortGuid Id { get; set; }
public long Id { get; set; }

/// <summary>
/// Gets or sets the image URI for this giveaway.
Expand Down
36 changes: 0 additions & 36 deletions Present/Data/ValueConverters/ShortGuidToBytesConverter.cs

This file was deleted.

Loading

0 comments on commit 8b15df3

Please sign in to comment.