Browse Source

Added support for custom ModuleBase command contexts. Added SocketCommandContext/RpcCommandContext.

tags/1.0-rc
RogueException 8 years ago
parent
commit
2c075e186a
32 changed files with 138 additions and 53 deletions
  1. +1
    -1
      src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs
  2. +1
    -1
      src/Discord.Net.Commands/Attributes/PreconditionAttribute.cs
  3. +1
    -1
      src/Discord.Net.Commands/Attributes/Preconditions/RequireBotPermissionAttribute.cs
  4. +1
    -1
      src/Discord.Net.Commands/Attributes/Preconditions/RequireContextAttribute.cs
  5. +1
    -1
      src/Discord.Net.Commands/Attributes/Preconditions/RequireOwnerAttribute.cs
  6. +1
    -1
      src/Discord.Net.Commands/Attributes/Preconditions/RequireUserPermissionAttribute.cs
  7. +2
    -2
      src/Discord.Net.Commands/Builders/CommandBuilder.cs
  8. +1
    -1
      src/Discord.Net.Commands/Builders/ModuleBuilder.cs
  9. +3
    -3
      src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
  10. +2
    -10
      src/Discord.Net.Commands/CommandContext.cs
  11. +4
    -4
      src/Discord.Net.Commands/CommandMatch.cs
  12. +1
    -1
      src/Discord.Net.Commands/CommandParser.cs
  13. +4
    -4
      src/Discord.Net.Commands/CommandService.cs
  14. +7
    -0
      src/Discord.Net.Commands/IModuleBase.cs
  15. +5
    -5
      src/Discord.Net.Commands/Info/CommandInfo.cs
  16. +2
    -2
      src/Discord.Net.Commands/Info/ParameterInfo.cs
  17. +16
    -3
      src/Discord.Net.Commands/ModuleBase.cs
  18. +1
    -1
      src/Discord.Net.Commands/Readers/ChannelTypeReader.cs
  19. +1
    -1
      src/Discord.Net.Commands/Readers/EnumTypeReader.cs
  20. +1
    -1
      src/Discord.Net.Commands/Readers/MessageTypeReader.cs
  21. +1
    -1
      src/Discord.Net.Commands/Readers/PrimitiveTypeReader.cs
  22. +1
    -1
      src/Discord.Net.Commands/Readers/RoleTypeReader.cs
  23. +1
    -1
      src/Discord.Net.Commands/Readers/TypeReader.cs
  24. +1
    -1
      src/Discord.Net.Commands/Readers/UserTypeReader.cs
  25. +11
    -0
      src/Discord.Net.Core/Commands/ICommandContext.cs
  26. +1
    -1
      src/Discord.Net.Rest/Discord.Net.Rest.csproj
  27. +0
    -0
      src/Discord.Net.Rest/Entities/Channels/RpcVirtualMessageChannel.cs
  28. +29
    -0
      src/Discord.Net.Rpc/Commands/RpcCommandContext.cs
  29. +4
    -2
      src/Discord.Net.Rpc/Entities/Messages/RpcMessage.cs
  30. +1
    -1
      src/Discord.Net.Rpc/Entities/Messages/RpcSystemMessage.cs
  31. +1
    -1
      src/Discord.Net.Rpc/Entities/Messages/RpcUserMessage.cs
  32. +31
    -0
      src/Discord.Net.WebSocket/Commands/SocketCommandContext.cs

+ 1
- 1
src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs View File

@@ -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);
} }
} }

+ 1
- 1
src/Discord.Net.Commands/Attributes/PreconditionAttribute.cs View File

@@ -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);
} }
} }

+ 1
- 1
src/Discord.Net.Commands/Attributes/Preconditions/RequireBotPermissionAttribute.cs View File

@@ -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();




+ 1
- 1
src/Discord.Net.Commands/Attributes/Preconditions/RequireContextAttribute.cs View File

@@ -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;




+ 1
- 1
src/Discord.Net.Commands/Attributes/Preconditions/RequireOwnerAttribute.cs View File

@@ -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();


+ 1
- 1
src/Discord.Net.Commands/Attributes/Preconditions/RequireUserPermissionAttribute.cs View File

@@ -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;




+ 2
- 2
src/Discord.Net.Commands/Builders/CommandBuilder.cs View File

@@ -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));


+ 1
- 1
src/Discord.Net.Commands/Builders/ModuleBuilder.cs View File

@@ -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);


+ 3
- 3
src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs View File

@@ -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);


+ 2
- 10
src/Discord.Net.Commands/CommandContext.cs View File

@@ -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; }
@@ -9,15 +9,7 @@
public IUserMessage Message { get; } public IUserMessage Message { get; }


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;


+ 4
- 4
src/Discord.Net.Commands/CommandMatch.cs View File

@@ -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);
} }
} }

+ 1
- 1
src/Discord.Net.Commands/CommandParser.cs View File

@@ -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);


+ 4
- 4
src/Discord.Net.Commands/CommandService.cs View File

@@ -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
- 0
src/Discord.Net.Commands/IModuleBase.cs View File

@@ -0,0 +1,7 @@
namespace Discord.Commands
{
internal interface IModuleBase
{
void SetContext(ICommandContext context);
}
}

+ 5
- 5
src/Discord.Net.Commands/Info/CommandInfo.cs View File

@@ -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;


+ 2
- 2
src/Discord.Net.Commands/Info/ParameterInfo.cs View File

@@ -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);
} }


+ 16
- 3
src/Discord.Net.Commands/ModuleBase.cs View File

@@ -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;
}
} }
} }

+ 1
- 1
src/Discord.Net.Commands/Readers/ChannelTypeReader.cs View File

@@ -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)
{ {


+ 1
- 1
src/Discord.Net.Commands/Readers/EnumTypeReader.cs View File

@@ -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;


+ 1
- 1
src/Discord.Net.Commands/Readers/MessageTypeReader.cs View File

@@ -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;




+ 1
- 1
src/Discord.Net.Commands/Readers/PrimitiveTypeReader.cs View File

@@ -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))


+ 1
- 1
src/Discord.Net.Commands/Readers/RoleTypeReader.cs View File

@@ -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;




+ 1
- 1
src/Discord.Net.Commands/Readers/TypeReader.cs View File

@@ -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);
} }
} }

+ 1
- 1
src/Discord.Net.Commands/Readers/UserTypeReader.cs View File

@@ -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
- 0
src/Discord.Net.Core/Commands/ICommandContext.cs View 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
- 1
src/Discord.Net.Rest/Discord.Net.Rest.csproj View File

@@ -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>


src/Discord.Net.Rest/Entities/Channels/RestVirtualMessageChannel.cs → src/Discord.Net.Rest/Entities/Channels/RpcVirtualMessageChannel.cs View File


+ 29
- 0
src/Discord.Net.Rpc/Commands/RpcCommandContext.cs View 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;
}
}

+ 4
- 2
src/Discord.Net.Rpc/Entities/Messages/RpcMessage.cs View File

@@ -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;
@@ -62,7 +62,9 @@ namespace Discord.Rpc
=> MessageHelper.DeleteAsync(this, Discord, options); => MessageHelper.DeleteAsync(this, Discord, options);


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;


+ 1
- 1
src/Discord.Net.Rpc/Entities/Messages/RpcSystemMessage.cs View File

@@ -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)
{ {
} }


+ 1
- 1
src/Discord.Net.Rpc/Entities/Messages/RpcUserMessage.cs View File

@@ -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
- 0
src/Discord.Net.WebSocket/Commands/SocketCommandContext.cs View 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;
}
}

Loading…
Cancel
Save