Skip to content

Commit

Permalink
feat: Add IUserMessage TypeReader
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed Jun 4, 2022
1 parent da66fe1 commit bc91328
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 5 additions & 1 deletion HuTao.Services/Core/Listeners/CommandHandlingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ public async Task InitializeAsync()

_commands.AddEnumerableTypeReader<LogType>(new EnumTryParseTypeReader<LogType>());

_commands.AddTypeReaders<IUserMessage>(
new JumpUrlTypeReader<IUserMessage>(),
new TypeReaders.Commands.MessageTypeReader<IUserMessage>());

_commands.AddTypeReaders<IMessage>(
new JumpUrlTypeReader(),
new JumpUrlTypeReader<IMessage>(),
new TypeReaders.Commands.MessageTypeReader<IMessage>());

_commands.AddTypeReaders<IEmote>(
Expand Down
7 changes: 4 additions & 3 deletions HuTao.Services/Core/TypeReaders/Commands/JumpUrlTypeReader.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using HuTao.Services.Utilities;
using CommandContext = HuTao.Data.Models.Discord.CommandContext;
using MessageExtensions = HuTao.Services.Utilities.MessageExtensions;

namespace HuTao.Services.Core.TypeReaders.Commands;

public class JumpUrlTypeReader : TypeReader
public class JumpUrlTypeReader<T> : TypeReader where T : IMessage
{
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input,
IServiceProvider services)
Expand All @@ -16,8 +17,8 @@ public override async Task<TypeReaderResult> ReadAsync(ICommandContext context,
if (jump is null) return TypeReaderResult.FromError(CommandError.ParseFailed, "Not a valid jump url.");

var message = await jump.GetMessageAsync(new CommandContext(context));
return message is null
return message is not T result
? TypeReaderResult.FromError(CommandError.Unsuccessful, "Could not find message.")
: TypeReaderResult.FromSuccess(message);
: TypeReaderResult.FromSuccess(result);
}
}

0 comments on commit bc91328

Please sign in to comment.