Typereaders will be autoloaded when module loads

This commit is contained in:
Master Kwoth
2017-10-09 02:52:46 +02:00
parent 72f36270dc
commit f3513779b7
60 changed files with 316 additions and 140 deletions

View File

@ -4,11 +4,17 @@ using System.Threading.Tasks;
using Discord.Commands;
using NadekoBot.Services;
using NadekoBot.Modules.CustomReactions.Services;
using NadekoBot.Core.Common.TypeReaders;
using Discord.WebSocket;
namespace NadekoBot.Common.TypeReaders
{
public class CommandTypeReader : TypeReader
public class CommandTypeReader : NadekoTypeReader
{
public CommandTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{
}
public override Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services)
{
var _cmds = ((INServiceProvider)services).GetService<CommandService>();
@ -28,9 +34,13 @@ namespace NadekoBot.Common.TypeReaders
return Task.FromResult(TypeReaderResult.FromSuccess(cmd));
}
}
//todo dependency on the module
public class CommandOrCrTypeReader : CommandTypeReader
{
public CommandOrCrTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{
}
public override async Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services)
{
input = input.ToUpperInvariant();

View File

@ -1,40 +1,46 @@
//using System;
//using System.Threading.Tasks;
//using Discord.Commands;
//using NadekoBot.Modules.Administration.Services;
using System;
using System.Threading.Tasks;
using Discord.Commands;
using NadekoBot.Modules.Administration.Services;
using NadekoBot.Core.Common.TypeReaders;
using Discord.WebSocket;
//namespace NadekoBot.Common.TypeReaders
//{
// public class GuildDateTimeTypeReader : TypeReader
// {
// public override Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services)
// {
// var _gts = (GuildTimezoneService)services.GetService(typeof(GuildTimezoneService));
// if (!DateTime.TryParse(input, out var dt))
// return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Input string is in an incorrect format."));
namespace NadekoBot.Common.TypeReaders
{
public class GuildDateTimeTypeReader : NadekoTypeReader
{
public GuildDateTimeTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{
}
// var tz = _gts.GetTimeZoneOrUtc(context.Guild.Id);
public override Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services)
{
var _gts = (GuildTimezoneService)services.GetService(typeof(GuildTimezoneService));
if (!DateTime.TryParse(input, out var dt))
return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Input string is in an incorrect format."));
// return Task.FromResult(TypeReaderResult.FromSuccess(new GuildDateTime(tz, dt)));
// }
// }
var tz = _gts.GetTimeZoneOrUtc(context.Guild.Id);
// public class GuildDateTime
// {
// public TimeZoneInfo Timezone { get; }
// public DateTime CurrentGuildTime { get; }
// public DateTime InputTime { get; }
// public DateTime InputTimeUtc { get; }
return Task.FromResult(TypeReaderResult.FromSuccess(new GuildDateTime(tz, dt)));
}
}
// private GuildDateTime() { }
public class GuildDateTime
{
public TimeZoneInfo Timezone { get; }
public DateTime CurrentGuildTime { get; }
public DateTime InputTime { get; }
public DateTime InputTimeUtc { get; }
// public GuildDateTime(TimeZoneInfo guildTimezone, DateTime inputTime)
// {
// var now = DateTime.UtcNow;
// Timezone = guildTimezone;
// CurrentGuildTime = TimeZoneInfo.ConvertTime(now, TimeZoneInfo.Utc, Timezone);
// InputTime = inputTime;
// InputTimeUtc = TimeZoneInfo.ConvertTime(inputTime, Timezone, TimeZoneInfo.Utc);
// }
// }
//}
private GuildDateTime() { }
public GuildDateTime(TimeZoneInfo guildTimezone, DateTime inputTime)
{
var now = DateTime.UtcNow;
Timezone = guildTimezone;
CurrentGuildTime = TimeZoneInfo.ConvertTime(now, TimeZoneInfo.Utc, Timezone);
InputTime = inputTime;
InputTimeUtc = TimeZoneInfo.ConvertTime(inputTime, Timezone, TimeZoneInfo.Utc);
}
}
}

View File

@ -3,17 +3,19 @@ using System.Linq;
using System.Threading.Tasks;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Core.Common.TypeReaders;
namespace NadekoBot.Common.TypeReaders
{
public class GuildTypeReader : TypeReader
public class GuildTypeReader : NadekoTypeReader
{
private readonly DiscordSocketClient _client;
public GuildTypeReader(DiscordSocketClient client)
public GuildTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{
_client = client;
}
public override Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider _)
{
input = input.Trim().ToLowerInvariant();

View File

@ -3,14 +3,16 @@ using System.Linq;
using System.Threading.Tasks;
using Discord.Commands;
using NadekoBot.Extensions;
using NadekoBot.Core.Common.TypeReaders;
using Discord.WebSocket;
namespace NadekoBot.Common.TypeReaders
{
public class ModuleTypeReader : TypeReader
public class ModuleTypeReader : NadekoTypeReader
{
private readonly CommandService _cmds;
public ModuleTypeReader(CommandService cmds)
public ModuleTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{
_cmds = cmds;
}
@ -26,11 +28,11 @@ namespace NadekoBot.Common.TypeReaders
}
}
public class ModuleOrCrTypeReader : TypeReader
public class ModuleOrCrTypeReader : NadekoTypeReader
{
private readonly CommandService _cmds;
public ModuleOrCrTypeReader(CommandService cmds)
public ModuleOrCrTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{
_cmds = cmds;
}

View File

@ -0,0 +1,18 @@
using Discord.Commands;
using Discord.WebSocket;
namespace NadekoBot.Core.Common.TypeReaders
{
public abstract class NadekoTypeReader : TypeReader
{
private readonly DiscordSocketClient _client;
private readonly CommandService _cmds;
private NadekoTypeReader() { }
public NadekoTypeReader(DiscordSocketClient client, CommandService cmds)
{
_client = client;
_cmds = cmds;
}
}
}

View File

@ -1,15 +1,21 @@
using System;
using System.Threading.Tasks;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Common.TypeReaders.Models;
using NadekoBot.Core.Common.TypeReaders;
namespace NadekoBot.Common.TypeReaders
{
/// <summary>
/// Used instead of bool for more flexible keywords for true/false only in the permission module
/// </summary>
public class PermissionActionTypeReader : TypeReader
public class PermissionActionTypeReader : NadekoTypeReader
{
public PermissionActionTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{
}
public override Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider _)
{
input = input.ToUpperInvariant();