Added support for custom ModuleBase command contexts. Added SocketCommandContext/RpcCommandContext.
This commit is contained in:
@@ -6,6 +6,6 @@ namespace Discord.Commands
|
|||||||
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true, Inherited = true)]
|
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true, Inherited = true)]
|
||||||
public abstract class ParameterPreconditionAttribute : Attribute
|
public abstract class ParameterPreconditionAttribute : Attribute
|
||||||
{
|
{
|
||||||
public abstract Task<PreconditionResult> CheckPermissions(CommandContext context, ParameterInfo parameter, object value, IDependencyMap map);
|
public abstract Task<PreconditionResult> CheckPermissions(ICommandContext context, ParameterInfo parameter, object value, IDependencyMap map);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,6 @@ namespace Discord.Commands
|
|||||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
|
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
|
||||||
public abstract class PreconditionAttribute : Attribute
|
public abstract class PreconditionAttribute : Attribute
|
||||||
{
|
{
|
||||||
public abstract Task<PreconditionResult> CheckPermissions(CommandContext context, CommandInfo command, IDependencyMap map);
|
public abstract Task<PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ namespace Discord.Commands
|
|||||||
GuildPermission = null;
|
GuildPermission = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task<PreconditionResult> CheckPermissions(CommandContext context, CommandInfo command, IDependencyMap map)
|
public override async Task<PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map)
|
||||||
{
|
{
|
||||||
var guildUser = await context.Guild.GetCurrentUserAsync();
|
var guildUser = await context.Guild.GetCurrentUserAsync();
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ namespace Discord.Commands
|
|||||||
Contexts = contexts;
|
Contexts = contexts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<PreconditionResult> CheckPermissions(CommandContext context, CommandInfo command, IDependencyMap map)
|
public override Task<PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map)
|
||||||
{
|
{
|
||||||
bool isValid = false;
|
bool isValid = false;
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace Discord.Commands
|
|||||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||||
public class RequireOwnerAttribute : PreconditionAttribute
|
public class RequireOwnerAttribute : PreconditionAttribute
|
||||||
{
|
{
|
||||||
public override async Task<PreconditionResult> CheckPermissions(CommandContext context, CommandInfo command, IDependencyMap map)
|
public override async Task<PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map)
|
||||||
{
|
{
|
||||||
var application = await context.Client.GetApplicationInfoAsync();
|
var application = await context.Client.GetApplicationInfoAsync();
|
||||||
if (context.User.Id == application.Owner.Id) return PreconditionResult.FromSuccess();
|
if (context.User.Id == application.Owner.Id) return PreconditionResult.FromSuccess();
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ namespace Discord.Commands
|
|||||||
GuildPermission = null;
|
GuildPermission = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<PreconditionResult> CheckPermissions(CommandContext context, CommandInfo command, IDependencyMap map)
|
public override Task<PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map)
|
||||||
{
|
{
|
||||||
var guildUser = context.User as IGuildUser;
|
var guildUser = context.User as IGuildUser;
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Discord.Commands.Builders
|
|||||||
private readonly List<string> _aliases;
|
private readonly List<string> _aliases;
|
||||||
|
|
||||||
public ModuleBuilder Module { get; }
|
public ModuleBuilder Module { get; }
|
||||||
internal Func<CommandContext, object[], IDependencyMap, Task> Callback { get; set; }
|
internal Func<ICommandContext, object[], IDependencyMap, Task> Callback { get; set; }
|
||||||
|
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Summary { get; set; }
|
public string Summary { get; set; }
|
||||||
@@ -34,7 +34,7 @@ namespace Discord.Commands.Builders
|
|||||||
_aliases = new List<string>();
|
_aliases = new List<string>();
|
||||||
}
|
}
|
||||||
//User-defined
|
//User-defined
|
||||||
internal CommandBuilder(ModuleBuilder module, string primaryAlias, Func<CommandContext, object[], IDependencyMap, Task> callback)
|
internal CommandBuilder(ModuleBuilder module, string primaryAlias, Func<ICommandContext, object[], IDependencyMap, Task> callback)
|
||||||
: this(module)
|
: this(module)
|
||||||
{
|
{
|
||||||
Discord.Preconditions.NotNull(primaryAlias, nameof(primaryAlias));
|
Discord.Preconditions.NotNull(primaryAlias, nameof(primaryAlias));
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ namespace Discord.Commands.Builders
|
|||||||
_preconditions.Add(precondition);
|
_preconditions.Add(precondition);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
public ModuleBuilder AddCommand(string primaryAlias, Func<CommandContext, object[], IDependencyMap, Task> callback, Action<CommandBuilder> createFunc)
|
public ModuleBuilder AddCommand(string primaryAlias, Func<ICommandContext, object[], IDependencyMap, Task> callback, Action<CommandBuilder> createFunc)
|
||||||
{
|
{
|
||||||
var builder = new CommandBuilder(this, primaryAlias, callback);
|
var builder = new CommandBuilder(this, primaryAlias, callback);
|
||||||
createFunc(builder);
|
createFunc(builder);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace Discord.Commands
|
|||||||
{
|
{
|
||||||
internal static class ModuleClassBuilder
|
internal static class ModuleClassBuilder
|
||||||
{
|
{
|
||||||
private static readonly TypeInfo _moduleTypeInfo = typeof(ModuleBase).GetTypeInfo();
|
private static readonly TypeInfo _moduleTypeInfo = typeof(IModuleBase).GetTypeInfo();
|
||||||
|
|
||||||
public static IEnumerable<TypeInfo> Search(Assembly assembly)
|
public static IEnumerable<TypeInfo> Search(Assembly assembly)
|
||||||
{
|
{
|
||||||
@@ -155,12 +155,12 @@ namespace Discord.Commands
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var createInstance = ReflectionUtils.CreateBuilder<ModuleBase>(typeInfo, service);
|
var createInstance = ReflectionUtils.CreateBuilder<IModuleBase>(typeInfo, service);
|
||||||
|
|
||||||
builder.Callback = (ctx, args, map) =>
|
builder.Callback = (ctx, args, map) =>
|
||||||
{
|
{
|
||||||
var instance = createInstance(map);
|
var instance = createInstance(map);
|
||||||
instance.Context = ctx;
|
instance.SetContext(ctx);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return method.Invoke(instance, args) as Task ?? Task.Delay(0);
|
return method.Invoke(instance, args) as Task ?? Task.Delay(0);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace Discord.Commands
|
namespace Discord.Commands
|
||||||
{
|
{
|
||||||
public struct CommandContext
|
public class CommandContext : ICommandContext
|
||||||
{
|
{
|
||||||
public IDiscordClient Client { get; }
|
public IDiscordClient Client { get; }
|
||||||
public IGuild Guild { get; }
|
public IGuild Guild { get; }
|
||||||
@@ -10,14 +10,6 @@
|
|||||||
|
|
||||||
public bool IsPrivate => Channel is IPrivateChannel;
|
public bool IsPrivate => Channel is IPrivateChannel;
|
||||||
|
|
||||||
public CommandContext(IDiscordClient client, IGuild guild, IMessageChannel channel, IUser user, IUserMessage msg)
|
|
||||||
{
|
|
||||||
Client = client;
|
|
||||||
Guild = guild;
|
|
||||||
Channel = channel;
|
|
||||||
User = user;
|
|
||||||
Message = msg;
|
|
||||||
}
|
|
||||||
public CommandContext(IDiscordClient client, IUserMessage msg)
|
public CommandContext(IDiscordClient client, IUserMessage msg)
|
||||||
{
|
{
|
||||||
Client = client;
|
Client = client;
|
||||||
|
|||||||
@@ -14,13 +14,13 @@ namespace Discord.Commands
|
|||||||
Alias = alias;
|
Alias = alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<PreconditionResult> CheckPreconditionsAsync(CommandContext context, IDependencyMap map = null)
|
public Task<PreconditionResult> CheckPreconditionsAsync(ICommandContext context, IDependencyMap map = null)
|
||||||
=> Command.CheckPreconditionsAsync(context, map);
|
=> Command.CheckPreconditionsAsync(context, map);
|
||||||
public Task<ParseResult> ParseAsync(CommandContext context, SearchResult searchResult, PreconditionResult? preconditionResult = null)
|
public Task<ParseResult> ParseAsync(ICommandContext context, SearchResult searchResult, PreconditionResult? preconditionResult = null)
|
||||||
=> Command.ParseAsync(context, Alias.Length, searchResult, preconditionResult);
|
=> Command.ParseAsync(context, Alias.Length, searchResult, preconditionResult);
|
||||||
public Task<ExecuteResult> ExecuteAsync(CommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList, IDependencyMap map)
|
public Task<ExecuteResult> ExecuteAsync(ICommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList, IDependencyMap map)
|
||||||
=> Command.ExecuteAsync(context, argList, paramList, map);
|
=> Command.ExecuteAsync(context, argList, paramList, map);
|
||||||
public Task<ExecuteResult> ExecuteAsync(CommandContext context, ParseResult parseResult, IDependencyMap map)
|
public Task<ExecuteResult> ExecuteAsync(ICommandContext context, ParseResult parseResult, IDependencyMap map)
|
||||||
=> Command.ExecuteAsync(context, parseResult, map);
|
=> Command.ExecuteAsync(context, parseResult, map);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace Discord.Commands
|
|||||||
QuotedParameter
|
QuotedParameter
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<ParseResult> ParseArgs(CommandInfo command, CommandContext context, string input, int startPos)
|
public static async Task<ParseResult> ParseArgs(CommandInfo command, ICommandContext context, string input, int startPos)
|
||||||
{
|
{
|
||||||
ParameterInfo curParam = null;
|
ParameterInfo curParam = null;
|
||||||
StringBuilder argBuilder = new StringBuilder(input.Length);
|
StringBuilder argBuilder = new StringBuilder(input.Length);
|
||||||
|
|||||||
@@ -222,9 +222,9 @@ namespace Discord.Commands
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Execution
|
//Execution
|
||||||
public SearchResult Search(CommandContext context, int argPos)
|
public SearchResult Search(ICommandContext context, int argPos)
|
||||||
=> Search(context, context.Message.Content.Substring(argPos));
|
=> Search(context, context.Message.Content.Substring(argPos));
|
||||||
public SearchResult Search(CommandContext context, string input)
|
public SearchResult Search(ICommandContext context, string input)
|
||||||
{
|
{
|
||||||
string searchInput = _caseSensitive ? input : input.ToLowerInvariant();
|
string searchInput = _caseSensitive ? input : input.ToLowerInvariant();
|
||||||
var matches = _map.GetCommands(searchInput).OrderByDescending(x => x.Command.Priority).ToImmutableArray();
|
var matches = _map.GetCommands(searchInput).OrderByDescending(x => x.Command.Priority).ToImmutableArray();
|
||||||
@@ -235,9 +235,9 @@ namespace Discord.Commands
|
|||||||
return SearchResult.FromError(CommandError.UnknownCommand, "Unknown command.");
|
return SearchResult.FromError(CommandError.UnknownCommand, "Unknown command.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<IResult> ExecuteAsync(CommandContext context, int argPos, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
public Task<IResult> ExecuteAsync(ICommandContext context, int argPos, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
||||||
=> ExecuteAsync(context, context.Message.Content.Substring(argPos), dependencyMap, multiMatchHandling);
|
=> ExecuteAsync(context, context.Message.Content.Substring(argPos), dependencyMap, multiMatchHandling);
|
||||||
public async Task<IResult> ExecuteAsync(CommandContext context, string input, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
public async Task<IResult> ExecuteAsync(ICommandContext context, string input, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
||||||
{
|
{
|
||||||
dependencyMap = dependencyMap ?? DependencyMap.Empty;
|
dependencyMap = dependencyMap ?? DependencyMap.Empty;
|
||||||
|
|
||||||
|
|||||||
7
src/Discord.Net.Commands/IModuleBase.cs
Normal file
7
src/Discord.Net.Commands/IModuleBase.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Discord.Commands
|
||||||
|
{
|
||||||
|
internal interface IModuleBase
|
||||||
|
{
|
||||||
|
void SetContext(ICommandContext context);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,7 +17,7 @@ namespace Discord.Commands
|
|||||||
private static readonly System.Reflection.MethodInfo _convertParamsMethod = typeof(CommandInfo).GetTypeInfo().GetDeclaredMethod(nameof(ConvertParamsList));
|
private static readonly System.Reflection.MethodInfo _convertParamsMethod = typeof(CommandInfo).GetTypeInfo().GetDeclaredMethod(nameof(ConvertParamsList));
|
||||||
private static readonly ConcurrentDictionary<Type, Func<IEnumerable<object>, object>> _arrayConverters = new ConcurrentDictionary<Type, Func<IEnumerable<object>, object>>();
|
private static readonly ConcurrentDictionary<Type, Func<IEnumerable<object>, object>> _arrayConverters = new ConcurrentDictionary<Type, Func<IEnumerable<object>, object>>();
|
||||||
|
|
||||||
private readonly Func<CommandContext, object[], IDependencyMap, Task> _action;
|
private readonly Func<ICommandContext, object[], IDependencyMap, Task> _action;
|
||||||
|
|
||||||
public ModuleInfo Module { get; }
|
public ModuleInfo Module { get; }
|
||||||
public string Name { get; }
|
public string Name { get; }
|
||||||
@@ -63,7 +63,7 @@ namespace Discord.Commands
|
|||||||
_action = builder.Callback;
|
_action = builder.Callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<PreconditionResult> CheckPreconditionsAsync(CommandContext context, IDependencyMap map = null)
|
public async Task<PreconditionResult> CheckPreconditionsAsync(ICommandContext context, IDependencyMap map = null)
|
||||||
{
|
{
|
||||||
if (map == null)
|
if (map == null)
|
||||||
map = DependencyMap.Empty;
|
map = DependencyMap.Empty;
|
||||||
@@ -85,7 +85,7 @@ namespace Discord.Commands
|
|||||||
return PreconditionResult.FromSuccess();
|
return PreconditionResult.FromSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ParseResult> ParseAsync(CommandContext context, int startIndex, SearchResult searchResult, PreconditionResult? preconditionResult = null)
|
public async Task<ParseResult> ParseAsync(ICommandContext context, int startIndex, SearchResult searchResult, PreconditionResult? preconditionResult = null)
|
||||||
{
|
{
|
||||||
if (!searchResult.IsSuccess)
|
if (!searchResult.IsSuccess)
|
||||||
return ParseResult.FromError(searchResult);
|
return ParseResult.FromError(searchResult);
|
||||||
@@ -96,7 +96,7 @@ namespace Discord.Commands
|
|||||||
return await CommandParser.ParseArgs(this, context, input, 0).ConfigureAwait(false);
|
return await CommandParser.ParseArgs(this, context, input, 0).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<ExecuteResult> ExecuteAsync(CommandContext context, ParseResult parseResult, IDependencyMap map)
|
public Task<ExecuteResult> ExecuteAsync(ICommandContext context, ParseResult parseResult, IDependencyMap map)
|
||||||
{
|
{
|
||||||
if (!parseResult.IsSuccess)
|
if (!parseResult.IsSuccess)
|
||||||
return Task.FromResult(ExecuteResult.FromError(parseResult));
|
return Task.FromResult(ExecuteResult.FromError(parseResult));
|
||||||
@@ -119,7 +119,7 @@ namespace Discord.Commands
|
|||||||
|
|
||||||
return ExecuteAsync(context, argList, paramList, map);
|
return ExecuteAsync(context, argList, paramList, map);
|
||||||
}
|
}
|
||||||
public async Task<ExecuteResult> ExecuteAsync(CommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList, IDependencyMap map)
|
public async Task<ExecuteResult> ExecuteAsync(ICommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList, IDependencyMap map)
|
||||||
{
|
{
|
||||||
if (map == null)
|
if (map == null)
|
||||||
map = DependencyMap.Empty;
|
map = DependencyMap.Empty;
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ namespace Discord.Commands
|
|||||||
_reader = builder.TypeReader;
|
_reader = builder.TypeReader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<PreconditionResult> CheckPreconditionsAsync(CommandContext context, object[] args, IDependencyMap map = null)
|
public async Task<PreconditionResult> CheckPreconditionsAsync(ICommandContext context, object[] args, IDependencyMap map = null)
|
||||||
{
|
{
|
||||||
if (map == null)
|
if (map == null)
|
||||||
map = DependencyMap.Empty;
|
map = DependencyMap.Empty;
|
||||||
@@ -61,7 +61,7 @@ namespace Discord.Commands
|
|||||||
return PreconditionResult.FromSuccess();
|
return PreconditionResult.FromSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TypeReaderResult> Parse(CommandContext context, string input)
|
public async Task<TypeReaderResult> Parse(ICommandContext context, string input)
|
||||||
{
|
{
|
||||||
return await _reader.Read(context, input).ConfigureAwait(false);
|
return await _reader.Read(context, input).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,27 @@
|
|||||||
using System.Threading.Tasks;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Discord.Commands
|
namespace Discord.Commands
|
||||||
{
|
{
|
||||||
public abstract class ModuleBase
|
public abstract class ModuleBase : ModuleBase<CommandContext> { }
|
||||||
|
|
||||||
|
public abstract class ModuleBase<T> : IModuleBase
|
||||||
|
where T : class, ICommandContext
|
||||||
{
|
{
|
||||||
public CommandContext Context { get; internal set; }
|
public T Context { get; private set; }
|
||||||
|
|
||||||
protected virtual async Task<IUserMessage> ReplyAsync(string message, bool isTTS = false, EmbedBuilder embed = null, RequestOptions options = null)
|
protected virtual async Task<IUserMessage> ReplyAsync(string message, bool isTTS = false, EmbedBuilder embed = null, RequestOptions options = null)
|
||||||
{
|
{
|
||||||
return await Context.Channel.SendMessageAsync(message, isTTS, embed, options).ConfigureAwait(false);
|
return await Context.Channel.SendMessageAsync(message, isTTS, embed, options).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IModuleBase
|
||||||
|
void IModuleBase.SetContext(ICommandContext context)
|
||||||
|
{
|
||||||
|
var newValue = context as T;
|
||||||
|
if (newValue == null)
|
||||||
|
throw new InvalidOperationException($"Invalid context type. Expected {typeof(T).Name}, got {context.GetType().Name}");
|
||||||
|
Context = newValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace Discord.Commands
|
|||||||
internal class ChannelTypeReader<T> : TypeReader
|
internal class ChannelTypeReader<T> : TypeReader
|
||||||
where T : class, IChannel
|
where T : class, IChannel
|
||||||
{
|
{
|
||||||
public override async Task<TypeReaderResult> Read(CommandContext context, string input)
|
public override async Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||||
{
|
{
|
||||||
if (context.Guild != null)
|
if (context.Guild != null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ namespace Discord.Commands
|
|||||||
_enumsByValue = byValueBuilder.ToImmutable();
|
_enumsByValue = byValueBuilder.ToImmutable();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<TypeReaderResult> Read(CommandContext context, string input)
|
public override Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||||
{
|
{
|
||||||
T baseValue;
|
T baseValue;
|
||||||
object enumValue;
|
object enumValue;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace Discord.Commands
|
|||||||
internal class MessageTypeReader<T> : TypeReader
|
internal class MessageTypeReader<T> : TypeReader
|
||||||
where T : class, IMessage
|
where T : class, IMessage
|
||||||
{
|
{
|
||||||
public override async Task<TypeReaderResult> Read(CommandContext context, string input)
|
public override async Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||||
{
|
{
|
||||||
ulong id;
|
ulong id;
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace Discord.Commands
|
|||||||
_tryParse = PrimitiveParsers.Get<T>();
|
_tryParse = PrimitiveParsers.Get<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<TypeReaderResult> Read(CommandContext context, string input)
|
public override Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||||
{
|
{
|
||||||
T value;
|
T value;
|
||||||
if (_tryParse(input, out value))
|
if (_tryParse(input, out value))
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace Discord.Commands
|
|||||||
internal class RoleTypeReader<T> : TypeReader
|
internal class RoleTypeReader<T> : TypeReader
|
||||||
where T : class, IRole
|
where T : class, IRole
|
||||||
{
|
{
|
||||||
public override Task<TypeReaderResult> Read(CommandContext context, string input)
|
public override Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||||
{
|
{
|
||||||
ulong id;
|
ulong id;
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,6 @@ namespace Discord.Commands
|
|||||||
{
|
{
|
||||||
public abstract class TypeReader
|
public abstract class TypeReader
|
||||||
{
|
{
|
||||||
public abstract Task<TypeReaderResult> Read(CommandContext context, string input);
|
public abstract Task<TypeReaderResult> Read(ICommandContext context, string input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace Discord.Commands
|
|||||||
internal class UserTypeReader<T> : TypeReader
|
internal class UserTypeReader<T> : TypeReader
|
||||||
where T : class, IUser
|
where T : class, IUser
|
||||||
{
|
{
|
||||||
public override async Task<TypeReaderResult> Read(CommandContext context, string input)
|
public override async Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||||
{
|
{
|
||||||
var results = new Dictionary<ulong, TypeReaderValue>();
|
var results = new Dictionary<ulong, TypeReaderValue>();
|
||||||
IReadOnlyCollection<IUser> channelUsers = (await context.Channel.GetUsersAsync(CacheMode.CacheOnly).Flatten().ConfigureAwait(false)).ToArray(); //TODO: must be a better way?
|
IReadOnlyCollection<IUser> channelUsers = (await context.Channel.GetUsersAsync(CacheMode.CacheOnly).Flatten().ConfigureAwait(false)).ToArray(); //TODO: must be a better way?
|
||||||
|
|||||||
11
src/Discord.Net.Core/Commands/ICommandContext.cs
Normal file
11
src/Discord.Net.Core/Commands/ICommandContext.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
namespace Discord.Commands
|
||||||
|
{
|
||||||
|
public interface ICommandContext
|
||||||
|
{
|
||||||
|
IDiscordClient Client { get; }
|
||||||
|
IGuild Guild { get; }
|
||||||
|
IMessageChannel Channel { get; }
|
||||||
|
IUser User { get; }
|
||||||
|
IUserMessage Message { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Description>A core Discord.Net library containing the REST client and models.</Description>
|
<Description>A core Discord.Net library containing the REST client and models.</Description>
|
||||||
<VersionPrefix>1.0.0-beta2</VersionPrefix>
|
<VersionPrefix>1.0.0-beta2</VersionPrefix>
|
||||||
|
|||||||
29
src/Discord.Net.Rpc/Commands/RpcCommandContext.cs
Normal file
29
src/Discord.Net.Rpc/Commands/RpcCommandContext.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using Discord.Rpc;
|
||||||
|
|
||||||
|
namespace Discord.Commands
|
||||||
|
{
|
||||||
|
public class RpcCommandContext : ICommandContext
|
||||||
|
{
|
||||||
|
public DiscordRpcClient Client { get; }
|
||||||
|
public IMessageChannel Channel { get; }
|
||||||
|
public RpcUser User { get; }
|
||||||
|
public RpcUserMessage Message { get; }
|
||||||
|
|
||||||
|
public bool IsPrivate => Channel is IPrivateChannel;
|
||||||
|
|
||||||
|
public RpcCommandContext(DiscordRpcClient client, RpcUserMessage msg)
|
||||||
|
{
|
||||||
|
Client = client;
|
||||||
|
Channel = msg.Channel;
|
||||||
|
User = msg.Author;
|
||||||
|
Message = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
//ICommandContext
|
||||||
|
IDiscordClient ICommandContext.Client => Client;
|
||||||
|
IGuild ICommandContext.Guild => null;
|
||||||
|
IMessageChannel ICommandContext.Channel => Channel;
|
||||||
|
IUser ICommandContext.User => User;
|
||||||
|
IUserMessage ICommandContext.Message => Message;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,7 +33,7 @@ namespace Discord.Rpc
|
|||||||
|
|
||||||
public DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks);
|
public DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks);
|
||||||
|
|
||||||
internal RpcMessage(DiscordRpcClient discord, ulong id, IMessageChannel channel, RpcUser author)
|
internal RpcMessage(DiscordRpcClient discord, ulong id, RestVirtualMessageChannel channel, RpcUser author)
|
||||||
: base(discord, id)
|
: base(discord, id)
|
||||||
{
|
{
|
||||||
Channel = channel;
|
Channel = channel;
|
||||||
@@ -63,6 +63,8 @@ namespace Discord.Rpc
|
|||||||
|
|
||||||
public override string ToString() => Content;
|
public override string ToString() => Content;
|
||||||
|
|
||||||
|
//IMessage
|
||||||
|
IMessageChannel IMessage.Channel => Channel;
|
||||||
MessageType IMessage.Type => MessageType.Default;
|
MessageType IMessage.Type => MessageType.Default;
|
||||||
IUser IMessage.Author => Author;
|
IUser IMessage.Author => Author;
|
||||||
IReadOnlyCollection<IAttachment> IMessage.Attachments => Attachments;
|
IReadOnlyCollection<IAttachment> IMessage.Attachments => Attachments;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace Discord.Rpc
|
|||||||
{
|
{
|
||||||
public MessageType Type { get; private set; }
|
public MessageType Type { get; private set; }
|
||||||
|
|
||||||
internal RpcSystemMessage(DiscordRpcClient discord, ulong id, IMessageChannel channel, RpcUser author)
|
internal RpcSystemMessage(DiscordRpcClient discord, ulong id, RestVirtualMessageChannel channel, RpcUser author)
|
||||||
: base(discord, id, channel, author)
|
: base(discord, id, channel, author)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ namespace Discord.Rpc
|
|||||||
public override IReadOnlyCollection<ITag> Tags => _tags;
|
public override IReadOnlyCollection<ITag> Tags => _tags;
|
||||||
public IReadOnlyDictionary<Emoji, int> Reactions => ImmutableDictionary.Create<Emoji, int>();
|
public IReadOnlyDictionary<Emoji, int> Reactions => ImmutableDictionary.Create<Emoji, int>();
|
||||||
|
|
||||||
internal RpcUserMessage(DiscordRpcClient discord, ulong id, IMessageChannel channel, RpcUser author)
|
internal RpcUserMessage(DiscordRpcClient discord, ulong id, RestVirtualMessageChannel channel, RpcUser author)
|
||||||
: base(discord, id, channel, author)
|
: base(discord, id, channel, author)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
31
src/Discord.Net.WebSocket/Commands/SocketCommandContext.cs
Normal file
31
src/Discord.Net.WebSocket/Commands/SocketCommandContext.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using Discord.WebSocket;
|
||||||
|
|
||||||
|
namespace Discord.Commands
|
||||||
|
{
|
||||||
|
public class SocketCommandContext : ICommandContext
|
||||||
|
{
|
||||||
|
public DiscordSocketClient Client { get; }
|
||||||
|
public SocketGuild Guild { get; }
|
||||||
|
public ISocketMessageChannel Channel { get; }
|
||||||
|
public SocketUser User { get; }
|
||||||
|
public SocketUserMessage Message { get; }
|
||||||
|
|
||||||
|
public bool IsPrivate => Channel is IPrivateChannel;
|
||||||
|
|
||||||
|
public SocketCommandContext(DiscordSocketClient client, SocketUserMessage msg)
|
||||||
|
{
|
||||||
|
Client = client;
|
||||||
|
Guild = (msg.Channel as SocketGuildChannel)?.Guild;
|
||||||
|
Channel = msg.Channel;
|
||||||
|
User = msg.Author;
|
||||||
|
Message = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
//ICommandContext
|
||||||
|
IDiscordClient ICommandContext.Client => Client;
|
||||||
|
IGuild ICommandContext.Guild => Guild;
|
||||||
|
IMessageChannel ICommandContext.Channel => Channel;
|
||||||
|
IUser ICommandContext.User => User;
|
||||||
|
IUserMessage ICommandContext.Message => Message;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user