Split IMessage into IUserMessage and ISystemMessage
This commit is contained in:
@@ -6,6 +6,6 @@ namespace Discord.Commands
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
|
||||
public abstract class PreconditionAttribute : Attribute
|
||||
{
|
||||
public abstract Task<PreconditionResult> CheckPermissions(IMessage context, Command executingCommand, object moduleInstance);
|
||||
public abstract Task<PreconditionResult> CheckPermissions(IUserMessage context, Command executingCommand, object moduleInstance);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace Discord.Commands
|
||||
Contexts = contexts;
|
||||
}
|
||||
|
||||
public override Task<PreconditionResult> CheckPermissions(IMessage context, Command executingCommand, object moduleInstance)
|
||||
public override Task<PreconditionResult> CheckPermissions(IUserMessage context, Command executingCommand, object moduleInstance)
|
||||
{
|
||||
bool isValid = false;
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace Discord.Commands
|
||||
GuildPermission = null;
|
||||
}
|
||||
|
||||
public override Task<PreconditionResult> CheckPermissions(IMessage context, Command executingCommand, object moduleInstance)
|
||||
public override Task<PreconditionResult> CheckPermissions(IUserMessage context, Command executingCommand, object moduleInstance)
|
||||
{
|
||||
var guildUser = context.Author as IGuildUser;
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Discord.Commands
|
||||
private static readonly ConcurrentDictionary<Type, Func<IEnumerable<object>, object>> _arrayConverters = new ConcurrentDictionary<Type, Func<IEnumerable<object>, object>>();
|
||||
|
||||
private readonly object _instance;
|
||||
private readonly Func<IMessage, IReadOnlyList<object>, Task> _action;
|
||||
private readonly Func<IUserMessage, IReadOnlyList<object>, Task> _action;
|
||||
|
||||
public MethodInfo Source { get; }
|
||||
public Module Module { get; }
|
||||
@@ -66,7 +66,7 @@ namespace Discord.Commands
|
||||
_action = BuildAction(source);
|
||||
}
|
||||
|
||||
public async Task<PreconditionResult> CheckPreconditions(IMessage context)
|
||||
public async Task<PreconditionResult> CheckPreconditions(IUserMessage context)
|
||||
{
|
||||
foreach (PreconditionAttribute precondition in Module.Preconditions)
|
||||
{
|
||||
@@ -85,7 +85,7 @@ namespace Discord.Commands
|
||||
return PreconditionResult.FromSuccess();
|
||||
}
|
||||
|
||||
public async Task<ParseResult> Parse(IMessage msg, SearchResult searchResult, PreconditionResult? preconditionResult = null)
|
||||
public async Task<ParseResult> Parse(IUserMessage context, SearchResult searchResult, PreconditionResult? preconditionResult = null)
|
||||
{
|
||||
if (!searchResult.IsSuccess)
|
||||
return ParseResult.FromError(searchResult);
|
||||
@@ -104,9 +104,9 @@ namespace Discord.Commands
|
||||
|
||||
input = input.Substring(matchingAlias.Length);
|
||||
|
||||
return await CommandParser.ParseArgs(this, msg, input, 0).ConfigureAwait(false);
|
||||
return await CommandParser.ParseArgs(this, context, input, 0).ConfigureAwait(false);
|
||||
}
|
||||
public Task<ExecuteResult> Execute(IMessage msg, ParseResult parseResult)
|
||||
public Task<ExecuteResult> Execute(IUserMessage context, ParseResult parseResult)
|
||||
{
|
||||
if (!parseResult.IsSuccess)
|
||||
return Task.FromResult(ExecuteResult.FromError(parseResult));
|
||||
@@ -127,13 +127,13 @@ namespace Discord.Commands
|
||||
paramList[i] = parseResult.ParamValues[i].Values.First().Value;
|
||||
}
|
||||
|
||||
return Execute(msg, argList, paramList);
|
||||
return Execute(context, argList, paramList);
|
||||
}
|
||||
public async Task<ExecuteResult> Execute(IMessage msg, IEnumerable<object> argList, IEnumerable<object> paramList)
|
||||
public async Task<ExecuteResult> Execute(IUserMessage context, IEnumerable<object> argList, IEnumerable<object> paramList)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _action.Invoke(msg, GenerateArgs(argList, paramList)).ConfigureAwait(false);//Note: This code may need context
|
||||
await _action.Invoke(context, GenerateArgs(argList, paramList)).ConfigureAwait(false);//Note: This code may need context
|
||||
return ExecuteResult.FromSuccess();
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -150,8 +150,8 @@ namespace Discord.Commands
|
||||
private IReadOnlyList<CommandParameter> BuildParameters(MethodInfo methodInfo)
|
||||
{
|
||||
var parameters = methodInfo.GetParameters();
|
||||
if (parameters.Length == 0 || parameters[0].ParameterType != typeof(IMessage))
|
||||
throw new InvalidOperationException("The first parameter of a command must be IMessage.");
|
||||
if (parameters.Length == 0 || parameters[0].ParameterType != typeof(IUserMessage))
|
||||
throw new InvalidOperationException($"The first parameter of a command must be {nameof(IUserMessage)}.");
|
||||
|
||||
var paramBuilder = ImmutableArray.CreateBuilder<CommandParameter>(parameters.Length - 1);
|
||||
for (int i = 1; i < parameters.Length; i++)
|
||||
@@ -190,7 +190,7 @@ namespace Discord.Commands
|
||||
}
|
||||
return paramBuilder.ToImmutable();
|
||||
}
|
||||
private Func<IMessage, IReadOnlyList<object>, Task> BuildAction(MethodInfo methodInfo)
|
||||
private Func<IUserMessage, IReadOnlyList<object>, Task> BuildAction(MethodInfo methodInfo)
|
||||
{
|
||||
if (methodInfo.ReturnType != typeof(Task))
|
||||
throw new InvalidOperationException("Commands must return a non-generic Task.");
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Discord.Commands
|
||||
DefaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public async Task<TypeReaderResult> Parse(IMessage context, string input)
|
||||
public async Task<TypeReaderResult> Parse(IUserMessage context, string input)
|
||||
{
|
||||
return await _reader.Read(context, input).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Discord.Commands
|
||||
QuotedParameter
|
||||
}
|
||||
|
||||
public static async Task<ParseResult> ParseArgs(Command command, IMessage context, string input, int startPos)
|
||||
public static async Task<ParseResult> ParseArgs(Command command, IUserMessage context, string input, int startPos)
|
||||
{
|
||||
CommandParameter curParam = null;
|
||||
StringBuilder argBuilder = new StringBuilder(input.Length);
|
||||
|
||||
@@ -41,8 +41,9 @@ namespace Discord.Commands
|
||||
[typeof(DateTime)] = new SimpleTypeReader<DateTime>(),
|
||||
[typeof(DateTimeOffset)] = new SimpleTypeReader<DateTimeOffset>(),
|
||||
|
||||
[typeof(IMessage)] = new MessageTypeReader(),
|
||||
|
||||
[typeof(IMessage)] = new MessageTypeReader<IMessage>(),
|
||||
[typeof(IUserMessage)] = new MessageTypeReader<IUserMessage>(),
|
||||
//[typeof(ISystemMessage)] = new MessageTypeReader<ISystemMessage>(),
|
||||
[typeof(IChannel)] = new ChannelTypeReader<IChannel>(),
|
||||
[typeof(IDMChannel)] = new ChannelTypeReader<IDMChannel>(),
|
||||
[typeof(IGroupChannel)] = new ChannelTypeReader<IGroupChannel>(),
|
||||
@@ -175,8 +176,8 @@ namespace Discord.Commands
|
||||
return false;
|
||||
}
|
||||
|
||||
public SearchResult Search(IMessage message, int argPos) => Search(message, message.Content.Substring(argPos));
|
||||
public SearchResult Search(IMessage message, string input)
|
||||
public SearchResult Search(IUserMessage message, int argPos) => Search(message, message.Content.Substring(argPos));
|
||||
public SearchResult Search(IUserMessage message, string input)
|
||||
{
|
||||
string lowerInput = input.ToLowerInvariant();
|
||||
var matches = _map.GetCommands(input).ToImmutableArray();
|
||||
@@ -187,9 +188,9 @@ namespace Discord.Commands
|
||||
return SearchResult.FromError(CommandError.UnknownCommand, "Unknown command.");
|
||||
}
|
||||
|
||||
public Task<IResult> Execute(IMessage message, int argPos, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
||||
public Task<IResult> Execute(IUserMessage message, int argPos, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
||||
=> Execute(message, message.Content.Substring(argPos), multiMatchHandling);
|
||||
public async Task<IResult> Execute(IMessage message, string input, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
||||
public async Task<IResult> Execute(IUserMessage message, string input, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
||||
{
|
||||
var searchResult = Search(message, input);
|
||||
if (!searchResult.IsSuccess)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
{
|
||||
public static class MessageExtensions
|
||||
{
|
||||
public static bool HasCharPrefix(this IMessage msg, char c, ref int argPos)
|
||||
public static bool HasCharPrefix(this IUserMessage msg, char c, ref int argPos)
|
||||
{
|
||||
var text = msg.Content;
|
||||
if (text.Length > 0 && text[0] == c)
|
||||
@@ -12,7 +12,7 @@
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static bool HasStringPrefix(this IMessage msg, string str, ref int argPos)
|
||||
public static bool HasStringPrefix(this IUserMessage msg, string str, ref int argPos)
|
||||
{
|
||||
var text = msg.Content;
|
||||
if (text.StartsWith(str))
|
||||
@@ -22,7 +22,7 @@
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static bool HasMentionPrefix(this IMessage msg, IUser user, ref int argPos)
|
||||
public static bool HasMentionPrefix(this IUserMessage msg, IUser user, ref int argPos)
|
||||
{
|
||||
var text = msg.Content;
|
||||
if (text.Length <= 3 || text[0] != '<' || text[1] != '@') return false;
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Discord.Commands
|
||||
internal class ChannelTypeReader<T> : TypeReader
|
||||
where T : class, IChannel
|
||||
{
|
||||
public override async Task<TypeReaderResult> Read(IMessage context, string input)
|
||||
public override async Task<TypeReaderResult> Read(IUserMessage context, string input)
|
||||
{
|
||||
var guild = (context.Channel as IGuildChannel)?.Guild;
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace Discord.Commands
|
||||
_enumsByValue = byValueBuilder.ToImmutable();
|
||||
}
|
||||
|
||||
public override Task<TypeReaderResult> Read(IMessage context, string input)
|
||||
public override Task<TypeReaderResult> Read(IUserMessage context, string input)
|
||||
{
|
||||
T baseValue;
|
||||
object enumValue;
|
||||
|
||||
@@ -3,16 +3,17 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
internal class MessageTypeReader : TypeReader
|
||||
internal class MessageTypeReader<T> : TypeReader
|
||||
where T : class, IMessage
|
||||
{
|
||||
public override Task<TypeReaderResult> Read(IMessage context, string input)
|
||||
public override Task<TypeReaderResult> Read(IUserMessage context, string input)
|
||||
{
|
||||
ulong id;
|
||||
|
||||
//By Id (1.0)
|
||||
if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id))
|
||||
{
|
||||
var msg = context.Channel.GetCachedMessage(id);
|
||||
var msg = context.Channel.GetCachedMessage(id) as T;
|
||||
if (msg != null)
|
||||
return Task.FromResult(TypeReaderResult.FromSuccess(msg));
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Discord.Commands
|
||||
internal class RoleTypeReader<T> : TypeReader
|
||||
where T : class, IRole
|
||||
{
|
||||
public override Task<TypeReaderResult> Read(IMessage context, string input)
|
||||
public override Task<TypeReaderResult> Read(IUserMessage context, string input)
|
||||
{
|
||||
var guild = (context.Channel as IGuildChannel)?.Guild;
|
||||
ulong id;
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Discord.Commands
|
||||
_tryParse = PrimitiveParsers.Get<T>();
|
||||
}
|
||||
|
||||
public override Task<TypeReaderResult> Read(IMessage context, string input)
|
||||
public override Task<TypeReaderResult> Read(IUserMessage context, string input)
|
||||
{
|
||||
T value;
|
||||
if (_tryParse(input, out value))
|
||||
|
||||
@@ -4,6 +4,6 @@ namespace Discord.Commands
|
||||
{
|
||||
public abstract class TypeReader
|
||||
{
|
||||
public abstract Task<TypeReaderResult> Read(IMessage context, string input);
|
||||
public abstract Task<TypeReaderResult> Read(IUserMessage context, string input);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Discord.Commands
|
||||
internal class UserTypeReader<T> : TypeReader
|
||||
where T : class, IUser
|
||||
{
|
||||
public override async Task<TypeReaderResult> Read(IMessage context, string input)
|
||||
public override async Task<TypeReaderResult> Read(IUserMessage context, string input)
|
||||
{
|
||||
var results = new Dictionary<ulong, TypeReaderValue>();
|
||||
var guild = (context.Channel as IGuildChannel)?.Guild;
|
||||
|
||||
Reference in New Issue
Block a user