Skip to content

Commit

Permalink
Bump to .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickklaeren committed Nov 15, 2023
1 parent 1eb8606 commit f3ea981
Show file tree
Hide file tree
Showing 47 changed files with 320 additions and 168 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<UserSecretsId>c485d4ee-5d66-4543-8a06-977f2843399c</UserSecretsId>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0-bullseye-slim-amd64 AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0-bullseye-slim-amd64 AS build
COPY ["Directory.Build.props", "./"]
WORKDIR /src
COPY ["src/Accord.Web/Accord.Web.csproj", "src/Accord.Web/"]
Expand Down
12 changes: 6 additions & 6 deletions src/Accord.Bot/Accord.Bot.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="AutoConstructor" Version="3.2.5">
<PackageReference Include="AutoConstructor" Version="5.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="AutoRegisterInject" Version="1.1.2" />
<PackageReference Include="AutoRegisterInject" Version="1.2.1" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="LazyCache" Version="2.4.0" />
<PackageReference Include="MediatR" Version="11.1.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
<PackageReference Include="Remora.Discord" Version="2023.4.0" />
<PackageReference Include="Serilog" Version="2.12.1-dev-01587" />
<PackageReference Include="MediatR" Version="12.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Remora.Discord" Version="2023.5.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="TimeSpanParserUtil" Version="1.2.0" />
</ItemGroup>

Expand Down
122 changes: 102 additions & 20 deletions src/Accord.Bot/CommandGroups/ReminderCommandGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,123 @@
using Remora.Discord.Commands.Attributes;
using Remora.Discord.Commands.Conditions;
using Remora.Discord.Commands.Contexts;
using Remora.Discord.Commands.Extensions;
using Remora.Discord.Commands.Feedback.Messages;
using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Interactivity;
using Remora.Rest.Core;
using Remora.Results;

namespace Accord.Bot.CommandGroups;

[Group("remind"), AutoConstructor]
public partial class ReminderCommandGroup: AccordCommandGroup
public partial class ReminderCommandGroup : AccordCommandGroup
{
private readonly ICommandContext _commandContext;
private readonly IMediator _mediator;
private readonly IDiscordRestGuildAPI _guildApi;
private readonly DiscordAvatarHelper _discordAvatarHelper;
private readonly FeedbackService _feedbackService;
private readonly IDiscordRestInteractionAPI _discordRestInteractionApi;

[Command("me"), Description("Add a reminder for yourself"), Ephemeral]
public async Task<IResult> AddReminder(TimeSpan timeSpan, string message)
[Command("me"), Description("Add a reminder for yourself")]
[SuppressInteractionResponse(true)]
public async Task<IResult> AddReminder()
{
var proxy = _commandContext.GetCommandProxy();

var sanitizedMessage = message.DiscordSanitize();
if (!_commandContext.TryGetUserID(out var discordUserId))
{
return await _feedbackService.SendContextualAsync("Failed to get the Discord user ID");
}

if (_commandContext is not IInteractionContext interactionContext)
{
return (Result)await _feedbackService.SendContextualWarningAsync
(
"This command can only be used with slash commands.",
discordUserId,
new FeedbackMessageOptions(MessageFlags: MessageFlags.Ephemeral)
);
}

var response = await _mediator.Send(new AddReminderRequest(
proxy.UserId.Value,
proxy.ChannelId.Value,
timeSpan,
sanitizedMessage
));
var response = new InteractionResponse
(
InteractionCallbackType.Modal,
new
(
new InteractionModalCallbackData
(
CustomIDHelpers.CreateModalID("add-reminder-modal"),
"Add a reminder",
new[]
{
new ActionRowComponent
(
new[]
{
new TextInputComponent
(
"description",
TextInputStyle.Paragraph,
"What should I remind you about?",
1,
500,
true,
default,
"Remind me to put a semi colon on the end of line 69420"
)
}
),
new ActionRowComponent
(
new[]
{
new TextInputComponent
(
"number",
TextInputStyle.Short,
"When should I remind you?",
1,
3,
true,
default,
"1"
)
}
),
new ActionRowComponent
(
new[]
{
new StringSelectComponent(
"period-unit",
new ISelectOption[]
{
new SelectOption("Seconds", "Seconds"),
new SelectOption("Minutes", "Minutes"),
new SelectOption("Hours", "Hours"),
new SelectOption("Days", "Days"),
new SelectOption("Weeks", "Weeks"),
new SelectOption("Years", "Years"),
},
"Hours",
1,
1)
}
)
}
)
)
);

await response.GetAction(
async () => await _feedbackService.SendContextualAsync($"You will be reminded about it in {timeSpan.Humanize()}"),
async () => await _feedbackService.SendContextualAsync(response.ErrorMessage)
var result = await _discordRestInteractionApi.CreateInteractionResponseAsync
(
interactionContext.Interaction.ID,
interactionContext.Interaction.Token,
response,
ct: CancellationToken
);

return Result.FromSuccess();
return result;
}

[Command("list"), Description("List pending reminders"), Ephemeral]
Expand Down Expand Up @@ -102,7 +184,7 @@ await response.GetAction(async () => await _feedbackService.SendContextualAsync(
public async Task<IResult> DeleteAllReminders()
{
var proxy = _commandContext.GetCommandProxy();

var response = await _mediator.Send(new DeleteAllRemindersRequest(proxy.UserId.Value));

await response.GetAction(async () => await _feedbackService.SendContextualAsync($"Your reminders have been deleted."),
Expand Down Expand Up @@ -150,9 +232,9 @@ private async Task<Embed> GetUserReminders(Snowflake id, int page = 0)
var guildUser = guildUserEntity.Entity;
var (userDto, _, _) = userResponse.Value!;

var avatarUrl = _discordAvatarHelper.GetAvatarUrl(guildUser.User.Value.ID.Value,
guildUser.User.Value.Discriminator,
guildUser.User.Value.Avatar?.Value,
var avatarUrl = _discordAvatarHelper.GetAvatarUrl(guildUser.User.Value.ID.Value,
guildUser.User.Value.Discriminator,
guildUser.User.Value.Avatar?.Value,
guildUser.User.Value.Avatar?.HasGif == true);

var userHandle = !string.IsNullOrWhiteSpace(userDto.UsernameWithDiscriminator)
Expand Down
66 changes: 66 additions & 0 deletions src/Accord.Bot/CommandGroups/ReminderCommandGroupInteractions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Threading.Tasks;
using Accord.Bot.Extensions;
using Accord.Services.Reminder;
using MediatR;
using Remora.Discord.Commands.Attributes;
using Remora.Discord.Commands.Contexts;
using Remora.Discord.Commands.Extensions;
using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Interactivity;
using Remora.Results;

namespace Accord.Bot.CommandGroups;

[AutoConstructor]
public partial class ReminderCommandGroupInteractions : InteractionGroup
{
private readonly FeedbackService _feedbackService;
private readonly ICommandContext _commandContext;
private readonly IMediator _mediator;

[Modal("add-reminder-modal")]
[Ephemeral]
public async Task<Result> OnAddReminder(string description, string number, string periodUnit)
{
if (!int.TryParse(number, out var actualNumber))
{
return (Result)await _feedbackService.SendContextualAsync("The number you input was not a number? :S");
}

if (!_commandContext.TryGetUserID(out var userId))
{
return (Result)await _feedbackService.SendContextualAsync("Could not find your user ID");
}

if (!_commandContext.TryGetChannelID(out var channelId))
{
return (Result)await _feedbackService.SendContextualAsync("This channel looks odd - there is no ID");
}

var sanitizedMessage = description.DiscordSanitize();

const int DAYS_IN_WEEK = 7;
const int DAYS_IN_YEAR = 365;

var timeSpan = periodUnit switch
{
"Seconds" => new TimeSpan(0, 0, 0, actualNumber),
"Minutes" => new TimeSpan(0, 0, actualNumber, 0),
"Hours" => new TimeSpan(0, actualNumber, 0, 0),
"Days" => new TimeSpan(actualNumber, 0, 0, 0),
"Weeks" => new TimeSpan(actualNumber * DAYS_IN_WEEK, 0, 0, 0),
"Years" => new TimeSpan(actualNumber * DAYS_IN_YEAR, 0, 0, 0),
_ => throw new ArgumentOutOfRangeException(nameof(periodUnit), periodUnit, "Unknwon time unit")
};

await _mediator.Send(new AddReminderRequest(
userId.Value,
channelId.Value,
timeSpan,
sanitizedMessage
));

return (Result)await _feedbackService.SendContextualAsync("I'll remind you!");
}
}
2 changes: 1 addition & 1 deletion src/Accord.Bot/Helpers/CommandContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static CommandContextProxy GetCommandProxy(this ICommandContext context)
{
return context switch
{
IInteractionCommandContext ix => new CommandContextProxy(ix.Interaction.Member.Value.User.Value.ID, ix.Interaction.GuildID.Value, ix.Interaction.ChannelID.Value),
IInteractionCommandContext ix => new CommandContextProxy(ix.Interaction.Member.Value.User.Value.ID, ix.Interaction.GuildID.Value, ix.Interaction.Channel.Value.ID.Value),
ITextCommandContext tx => new CommandContextProxy(tx.Message.Author.Value.ID, tx.GuildID.Value, tx.Message.ChannelID.Value),
_ => throw new NotSupportedException()
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.Linq;
using Accord.Bot.CommandGroups;
using Accord.Bot.CommandGroups.UserReports;
using Accord.Bot.Parsers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Remora.Commands.Extensions;
using Remora.Discord.API.Abstractions.Gateway.Commands;
using Remora.Discord.Commands.Extensions;
using Remora.Discord.Gateway;
using Remora.Discord.Gateway.Extensions;
using Remora.Discord.Interactivity.Extensions;

namespace Accord.Bot.Infrastructure;

Expand Down Expand Up @@ -36,8 +36,11 @@ public static IServiceCollection AddDiscordBot(this IServiceCollection services,
o.Intents |= GatewayIntents.GuildMessages;
})
.AddDiscordCommands(true)
.AddPostExecutionEvent<AfterCommandPostExecutionEvent>()
.AddParser<TimeSpanParser>();
.AddPostExecutionEvent<AfterCommandPostExecutionEvent>();

services
.AddInteractivity()
.AddInteractionGroup<ReminderCommandGroupInteractions>();

services
.AddCommandTree()
Expand Down
23 changes: 0 additions & 23 deletions src/Accord.Bot/Parsers/TimeSpanParser.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Accord.Bot/RequestHandlers/BanHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
namespace Accord.Bot.RequestHandlers;

[AutoConstructor]
public partial class BanHandler : AsyncRequestHandler<BanRequest>
public partial class BanHandler : IRequestHandler<BanRequest>
{
private readonly IDiscordRestChannelAPI _channelApi;
private readonly IDiscordRestGuildAPI _guildApi;
private readonly IMediator _mediator;

protected override async Task Handle(BanRequest request, CancellationToken cancellationToken)
public async Task Handle(BanRequest request, CancellationToken cancellationToken)
{
await _guildApi.CreateGuildBanAsync(new Snowflake(request.DiscordGuildId), new Snowflake(request.User.Id), reason: request.Reason, ct: cancellationToken);

Expand Down
4 changes: 2 additions & 2 deletions src/Accord.Bot/RequestHandlers/CloseHelpForumPostHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace Accord.Bot.RequestHandlers;
public record CloseHelpForumPostRequest(IChannel Channel) : IRequest;

[AutoConstructor]
public partial class CloseHelpForumPostHandler : AsyncRequestHandler<CloseHelpForumPostRequest>
public partial class CloseHelpForumPostHandler : IRequestHandler<CloseHelpForumPostRequest>
{
private readonly IDiscordRestChannelAPI _channelApi;

private const string PREFIX = "";

protected override async Task Handle(CloseHelpForumPostRequest request, CancellationToken cancellationToken)
public async Task Handle(CloseHelpForumPostRequest request, CancellationToken cancellationToken)
{
if (request.Channel.Name.HasValue
&& !request.Channel.Name.Value!.StartsWith(PREFIX))
Expand Down
4 changes: 2 additions & 2 deletions src/Accord.Bot/RequestHandlers/DeactivateForumPostHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace Accord.Bot.RequestHandlers;
public record DeactivateForumPostRequest(IChannel Channel) : IRequest;

[AutoConstructor]
public partial class DeactivateForumPostHandler : AsyncRequestHandler<DeactivateForumPostRequest>
public partial class DeactivateForumPostHandler : IRequestHandler<DeactivateForumPostRequest>
{
private readonly IDiscordRestChannelAPI _channelApi;

private const string PREFIX = "";

protected override async Task Handle(DeactivateForumPostRequest request, CancellationToken cancellationToken)
public async Task Handle(DeactivateForumPostRequest request, CancellationToken cancellationToken)
{
if (request.Channel.Name.HasValue
&& !request.Channel.Name.Value!.EndsWith(PREFIX))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
namespace Accord.Bot.RequestHandlers;

[AutoConstructor]
public partial class DeleteUserReportDiscordMessageHandler : AsyncRequestHandler<DeleteUserReportDiscordMessageRequest>
public partial class DeleteUserReportDiscordMessageHandler : IRequestHandler<DeleteUserReportDiscordMessageRequest>
{
private readonly IDiscordRestWebhookAPI _webhookApi;

protected override async Task Handle(DeleteUserReportDiscordMessageRequest request, CancellationToken cancellationToken)
public async Task Handle(DeleteUserReportDiscordMessageRequest request, CancellationToken cancellationToken)
{
await _webhookApi.DeleteWebhookMessageAsync(
new Snowflake(request.DiscordProxyWebhookId),
Expand Down
Loading

0 comments on commit f3ea981

Please sign in to comment.