public CommandService(CommandServiceConfig config) { _caseSensitive = config.CaseSensitiveCommands; _separatorChar = config.SeparatorChar; _defaultRunMode = config.DefaultRunMode; _moduleLock = new SemaphoreSlim(1, 1); _typedModuleDefs = new ConcurrentDictionary <Type, ModuleInfo>(); _moduleDefs = new HashSet <ModuleInfo>(); _map = new CommandMap(this); _typeReaders = new ConcurrentDictionary <Type, ConcurrentDictionary <Type, TypeReader> >(); _defaultTypeReaders = new ConcurrentDictionary <Type, TypeReader>(); foreach (var type in PrimitiveParsers.SupportedTypes) { _defaultTypeReaders[type] = PrimitiveTypeReader.Create(type); } var entityTypeReaders = ImmutableList.CreateBuilder <Tuple <Type, Type> >(); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IMessage), typeof(MessageTypeReader <>))); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IChannel), typeof(ChannelTypeReader <>))); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IRole), typeof(RoleTypeReader <>))); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IUser), typeof(UserTypeReader <>))); _entityTypeReaders = entityTypeReaders.ToImmutable(); }
public CommandMap(CommandMap parent, string name, string fullName) : this() { _parent = parent; _name = name; _fullName = fullName; }
private void AddCommand(int index, string[] parts, Command command, bool isAlias) { if (!command.IsHidden) { _isVisible = true; } if (index != parts.Length) { CommandMap nextGroup; string name = parts[index].ToLowerInvariant(); string fullName = string.Join(" ", parts, 0, index + 1); if (!_items.TryGetValue(name, out nextGroup)) { nextGroup = new CommandMap(this, name, fullName); _items.Add(name, nextGroup); _hasSubGroups = true; } nextGroup.AddCommand(index + 1, parts, command, isAlias); } else { _commands.Add(command); if (!isAlias) { _hasNonAliases = true; } } }
internal void AddCommand(Command command) { _allCommands.Add(command); //Get category CommandMap category; string categoryName = command.Category ?? ""; if (!_categories.TryGetValue(categoryName, out category)) { category = new CommandMap(); _categories.Add(categoryName, category); } //Add main command category.AddCommand(command.Text, command, false); _map.AddCommand(command.Text, command, false); //Add aliases foreach (var alias in command.Aliases) { category.AddCommand(alias, command, true); _map.AddCommand(alias, command, true); } }
public CommandService(CommandServiceConfig config) { Config = config; _allCommands = new List <Command>(); _map = new CommandMap(); _categories = new Dictionary <string, CommandMap>(); Root = new CommandGroupBuilder(this); }
public CommandService(CommandServiceConfig config) { Config = config; _allCommands = new List<Command>(); _map = new CommandMap(null, "", ""); _categories = new Dictionary<string, CommandMap>(); Root = new CommandGroupBuilder(this, "", null); }
public static bool ParseCommand(string input, CommandMap map, out IEnumerable <Command> commands, out int endPos) { int startPosition = 0; int endPosition = 0; int inputLength = input.Length; bool isEscaped = false; commands = null; endPos = 0; if (input == "") { return(false); } while (endPosition < inputLength) { char currentChar = input[endPosition++]; if (isEscaped) { isEscaped = false; } else if (currentChar == '\\') { isEscaped = true; } bool isWhitespace = IsWhiteSpace(currentChar); if ((!isEscaped && isWhitespace) || endPosition >= inputLength) { int length = (isWhitespace ? endPosition - 1 : endPosition) - startPosition; string temp = input.Substring(startPosition, length); if (temp == "") { startPosition = endPosition; } else { var newMap = map.GetItem(temp); if (newMap != null) { map = newMap; endPos = endPosition; } else { break; } startPosition = endPosition; } } } commands = map.GetCommands(); //Work our way backwards to find a command that matches our input return(commands != null); }
public CommandMap(CommandMap parent, string name, string fullName) { _parent = parent; _name = name; _fullName = fullName; _items = new Dictionary <string, CommandMap>(); _commands = new List <Command>(); _isVisible = false; _hasNonAliases = false; _hasSubGroups = false; }
public CommandMap(CommandMap parent, string name, string fullName) { _parent = parent; _name = name; _fullName = fullName; _items = new Dictionary<string, CommandMap>(); _commands = new List<Command>(); _isVisible = false; _hasNonAliases = false; _hasSubGroups = false; }
/// <summary> /// Initializes a new <see cref="CommandService"/> class with the provided configuration. /// </summary> /// <param name="config">The configuration class.</param> /// <exception cref="InvalidOperationException"> /// The <see cref="RunMode"/> cannot be set to <see cref="RunMode.Default"/>. /// </exception> public CommandService(CommandServiceConfig config) { _caseSensitive = config.CaseSensitiveCommands; _throwOnError = config.ThrowOnError; _ignoreExtraArgs = config.IgnoreExtraArgs; _separatorChar = config.SeparatorChar; _defaultRunMode = config.DefaultRunMode; _quotationMarkAliasMap = (config.QuotationMarkAliasMap ?? new Dictionary <char, char>()).ToImmutableDictionary(); if (_defaultRunMode == RunMode.Default) { throw new InvalidOperationException("The default run mode cannot be set to Default."); } _logManager = new LogManager(config.LogLevel); _logManager.Message += async msg => await _logEvent.InvokeAsync(msg).ConfigureAwait(false); _cmdLogger = _logManager.CreateLogger("Command"); _moduleLock = new SemaphoreSlim(1, 1); _typedModuleDefs = new ConcurrentDictionary <Type, ModuleInfo>(); _moduleDefs = new HashSet <ModuleInfo>(); _map = new CommandMap(this); _typeReaders = new ConcurrentDictionary <Type, ConcurrentDictionary <Type, TypeReader> >(); _defaultTypeReaders = new ConcurrentDictionary <Type, TypeReader>(); foreach (var type in PrimitiveParsers.SupportedTypes) { _defaultTypeReaders[type] = PrimitiveTypeReader.Create(type); _defaultTypeReaders[typeof(Nullable <>).MakeGenericType(type)] = NullableTypeReader.Create(type, _defaultTypeReaders[type]); } var tsreader = new TimeSpanTypeReader(); _defaultTypeReaders[typeof(TimeSpan)] = tsreader; _defaultTypeReaders[typeof(TimeSpan?)] = NullableTypeReader.Create(typeof(TimeSpan), tsreader); _defaultTypeReaders[typeof(string)] = new PrimitiveTypeReader <string>((string x, out string y) => { y = x; return(true); }, 0); var entityTypeReaders = ImmutableList.CreateBuilder <Tuple <Type, Type> >(); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IMessage), typeof(MessageTypeReader <>))); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IChannel), typeof(ChannelTypeReader <>))); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IRole), typeof(RoleTypeReader <>))); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IUser), typeof(UserTypeReader <>))); _entityTypeReaders = entityTypeReaders.ToImmutable(); }
public CommandService() { _moduleLock = new SemaphoreSlim(1, 1); _modules = new ConcurrentDictionary <Type, Module>(); _map = new CommandMap(); _typeReaders = new ConcurrentDictionary <Type, TypeReader> { [typeof(string)] = new SimpleTypeReader <string>(), [typeof(byte)] = new SimpleTypeReader <byte>(), [typeof(sbyte)] = new SimpleTypeReader <sbyte>(), [typeof(ushort)] = new SimpleTypeReader <ushort>(), [typeof(short)] = new SimpleTypeReader <short>(), [typeof(uint)] = new SimpleTypeReader <uint>(), [typeof(int)] = new SimpleTypeReader <int>(), [typeof(ulong)] = new SimpleTypeReader <ulong>(), [typeof(long)] = new SimpleTypeReader <long>(), [typeof(float)] = new SimpleTypeReader <float>(), [typeof(double)] = new SimpleTypeReader <double>(), [typeof(decimal)] = new SimpleTypeReader <decimal>(), [typeof(DateTime)] = new SimpleTypeReader <DateTime>(), [typeof(DateTimeOffset)] = new SimpleTypeReader <DateTimeOffset>(), [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>(), [typeof(IGuildChannel)] = new ChannelTypeReader <IGuildChannel>(), [typeof(IMessageChannel)] = new ChannelTypeReader <IMessageChannel>(), [typeof(IPrivateChannel)] = new ChannelTypeReader <IPrivateChannel>(), [typeof(ITextChannel)] = new ChannelTypeReader <ITextChannel>(), [typeof(IVoiceChannel)] = new ChannelTypeReader <IVoiceChannel>(), //[typeof(IGuild)] = new GuildTypeReader<IGuild>(), [typeof(IRole)] = new RoleTypeReader <IRole>(), //[typeof(IInvite)] = new InviteTypeReader<IInvite>(), //[typeof(IInviteMetadata)] = new InviteTypeReader<IInviteMetadata>(), [typeof(IUser)] = new UserTypeReader <IUser>(), [typeof(IGroupUser)] = new UserTypeReader <IGroupUser>(), [typeof(IGuildUser)] = new UserTypeReader <IGuildUser>(), }; }
public static bool ParseCommand(string input, CommandMap map, out IEnumerable<Command> commands, out int endPos) { int startPosition = 0; int endPosition = 0; int inputLength = input.Length; bool isEscaped = false; commands = null; endPos = 0; if (input == "") return false; while (endPosition < inputLength) { char currentChar = input[endPosition++]; if (isEscaped) isEscaped = false; else if (currentChar == '\\') isEscaped = true; bool isWhitespace = IsWhiteSpace(currentChar); if ((!isEscaped && isWhitespace) || endPosition >= inputLength) { int length = (isWhitespace ? endPosition - 1 : endPosition) - startPosition; string temp = input.Substring(startPosition, length); if (temp == "") startPosition = endPosition; else { var newMap = map.GetItem(temp); if (newMap != null) { map = newMap; endPos = endPosition; } else break; startPosition = endPosition; } } } commands = map.GetCommands(); //Work our way backwards to find a command that matches our input return commands != null; }
public CommandService(CommandServiceConfig config) { _caseSensitive = config.CaseSensitiveCommands; _throwOnError = config.ThrowOnError; _separatorChar = config.SeparatorChar; _defaultRunMode = config.DefaultRunMode; if (_defaultRunMode == RunMode.Default) { throw new InvalidOperationException("The default run mode cannot be set to Default."); } _logManager = new LogManager(config.LogLevel); _logManager.Message += async msg => await _logEvent.InvokeAsync(msg).ConfigureAwait(false); _cmdLogger = _logManager.CreateLogger("Command"); _moduleLock = new SemaphoreSlim(1, 1); _typedModuleDefs = new ConcurrentDictionary <Type, ModuleInfo>(); _moduleDefs = new HashSet <ModuleInfo>(); _map = new CommandMap(this); _typeReaders = new ConcurrentDictionary <Type, ConcurrentDictionary <Type, TypeReader> >(); _defaultTypeReaders = new ConcurrentDictionary <Type, TypeReader>(); foreach (var type in PrimitiveParsers.SupportedTypes) { _defaultTypeReaders[type] = PrimitiveTypeReader.Create(type); } var entityTypeReaders = ImmutableList.CreateBuilder <Tuple <Type, Type> >(); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IMessage), typeof(MessageTypeReader <>))); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IChannel), typeof(ChannelTypeReader <>))); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IRole), typeof(RoleTypeReader <>))); entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IUser), typeof(UserTypeReader <>))); _entityTypeReaders = entityTypeReaders.ToImmutable(); }
private void AddCommand(int index, string[] parts, Command command, bool isAlias) { if (!command.IsHidden) _isVisible = true; if (index != parts.Length) { CommandMap nextGroup; string name = parts[index].ToLowerInvariant(); string fullName = string.Join(" ", parts, 0, index + 1); if (!_items.TryGetValue(name, out nextGroup)) { nextGroup = new CommandMap(this, name, fullName); _items.Add(name, nextGroup); _hasSubGroups = true; } nextGroup.AddCommand(index + 1, parts, command, isAlias); } else { _commands.Add(command); if (!isAlias) _hasNonAliases = true; } }
private Task ShowCommandHelp(CommandMap map, User user, Channel channel, Channel replyChannel = null) { StringBuilder output = new StringBuilder(); IEnumerable <Command> cmds = map.Commands; bool isFirstCmd = true; string error; if (cmds.Any()) { foreach (var cmd in cmds) { if (!cmd.CanRun(user, channel, out error)) { } //output.AppendLine(error ?? DefaultPermissionError); else { if (isFirstCmd) { isFirstCmd = false; } else { output.AppendLine(); } ShowCommandHelpInternal(cmd, user, channel, output); } } } else { output.Append('`'); output.Append(map.FullName); output.Append("`\n"); } bool isFirstSubCmd = true; foreach (var subCmd in map.SubGroups.Where(x => x.CanRun(user, channel, out error) && x.IsVisible)) { if (isFirstSubCmd) { isFirstSubCmd = false; output.AppendLine("Sub Commands: "); } else { output.Append(", "); } output.Append('`'); output.Append(subCmd.Name); if (subCmd.SubGroups.Any()) { output.Append("*"); } output.Append('`'); } if (isFirstCmd && isFirstSubCmd) //Had no commands and no subcommands { output.Clear(); output.AppendLine("There are no commands you have permission to run."); } return((replyChannel ?? channel).SendMessage(output.ToString())); }
internal void AddCommand(Command command) { _allCommands.Add(command); //Get category CommandMap category; string categoryName = command.Category ?? ""; if (!_categories.TryGetValue(categoryName, out category)) { category = new CommandMap(null, "", ""); _categories.Add(categoryName, category); } //Add main command category.AddCommand(command.Text, command, false); _map.AddCommand(command.Text, command, false); //Add aliases foreach (var alias in command.Aliases) { category.AddCommand(alias, command, true); _map.AddCommand(alias, command, true); } }
private Task ShowCommandHelp(CommandMap map, User user, Channel channel, Channel replyChannel = null) { StringBuilder output = new StringBuilder(); IEnumerable<Command> cmds = map.Commands; bool isFirstCmd = true; string error; if (cmds.Any()) { foreach (var cmd in cmds) { if (!cmd.CanRun(user, channel, out error)) { } //output.AppendLine(error ?? DefaultPermissionError); else { if (isFirstCmd) isFirstCmd = false; else output.AppendLine(); ShowCommandHelpInternal(cmd, user, channel, output); } } } else { output.Append('`'); output.Append(map.FullName); output.Append("`\n"); } bool isFirstSubCmd = true; foreach (var subCmd in map.SubGroups.Where(x => x.CanRun(user, channel, out error) && x.IsVisible)) { if (isFirstSubCmd) { isFirstSubCmd = false; output.AppendLine("Sub Commands: "); } else output.Append(", "); output.Append('`'); output.Append(subCmd.Name); if (subCmd.SubGroups.Any()) output.Append("*"); output.Append('`'); } if (isFirstCmd && isFirstSubCmd) //Had no commands and no subcommands { output.Clear(); output.AppendLine("There are no commands you have permission to run."); } return (replyChannel ?? channel).SendMessage(output.ToString()); }