Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

Commit

Permalink
Replace default TimeSpanConverter with custom TimeSpanArgumentConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Mar 14, 2022
1 parent da1a9f3 commit 696d60f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
71 changes: 71 additions & 0 deletions BrackeysBot/ArgumentConverters/TimeSpanArgumentConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Threading.Tasks;
using DisCatSharp.CommandsNext;
using DisCatSharp.CommandsNext.Converters;
using DisCatSharp.Entities;

namespace BrackeysBot.ArgumentConverters;

/// <summary>
/// Represents a converter which converts a command argument to <see cref="TimeSpan" />.
/// </summary>
internal sealed class TimeSpanArgumentConverter : IArgumentConverter<TimeSpan>
{
/// <inheritdoc />
public Task<Optional<TimeSpan>> ConvertAsync(string value, CommandContext ctx)
{
TimeSpan result = TimeSpan.Zero;
var unitValue = 0;

for (var index = 0; index < value.Length; index++)
{
char current = value[index];
switch (current)
{
case var digitChar when char.IsDigit(digitChar):
var digit = (int) char.GetNumericValue(digitChar);
unitValue = unitValue * 10 + digit;
break;

case 'y':
result += TimeSpan.FromDays(unitValue * 365);
unitValue = 0;
break;

case 'm':
if (index < value.Length - 1 && value[index + 1] == 'o')
{
index++;
result += TimeSpan.FromDays(unitValue * 30);
}
else
result += TimeSpan.FromMinutes(unitValue);

unitValue = 0;
break;

case 'w':
result += TimeSpan.FromDays(unitValue * 7);
unitValue = 0;
break;

case 'd':
result += TimeSpan.FromDays(unitValue);
unitValue = 0;
break;

case 'h':
result += TimeSpan.FromHours(unitValue);
unitValue = 0;
break;

case 's':
result += TimeSpan.FromSeconds(unitValue);
unitValue = 0;
break;
}
}

return Task.FromResult<Optional<TimeSpan>>(result);
}
}
5 changes: 5 additions & 0 deletions BrackeysBot/Plugins/SimplePluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
using System.Threading;
using BrackeysBot.API.Exceptions;
using BrackeysBot.API.Plugins;
using BrackeysBot.ArgumentConverters;
using BrackeysBot.Configuration;
using BrackeysBot.Resources;
using DisCatSharp;
using DisCatSharp.CommandsNext;
using DisCatSharp.CommandsNext.Converters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -287,6 +289,9 @@ public IPlugin LoadPlugin(string name)
CommandsNextExtension? commandsNext = plugin.DiscordClient?.GetCommandsNext();
if (commandsNext is not null)
{
commandsNext.UnregisterConverter<TimeSpanConverter>();
commandsNext.RegisterConverter(new TimeSpanArgumentConverter());

string[] commandNames = commandsNext.RegisteredCommands.Keys.ToArray();
Logger.Info(string.Format(LoggerMessages.PluginRegisteredCommands, pluginInfo.Name, commandNames.Length,
string.Join(", ", commandNames)));
Expand Down

0 comments on commit 696d60f

Please sign in to comment.