public override async Task CheckAsync(CommandContext e) { if (e == null) { return; } if (e.Message.Type != MessageType.GUILDTEXT) { return; } var channel = await e.Message.GetChannelAsync(); if (!(channel is IDiscordGuildChannel guildChannel)) { return; } var guild = await guildChannel.GetGuildAsync(); var cache = e.GetService <IExtendedCacheClient>(); IEnumerable <Token> tokens = null; string[] args = e.Message.Content.Substring(e.PrefixUsed.Length) .Split(' '); string commandName = args.FirstOrDefault() .ToLowerInvariant(); if (e.EventSystem .GetCommandHandler <SimpleCommandHandler>() .GetCommandByIdOrDefault(commandName) != null) { return; } var cachePackage = await cache.HashGetAsync <ScriptPackage>(CommandCacheKey, commandName + ":" + guild.Id); if (cachePackage != null) { tokens = ScriptPacker.Unpack(cachePackage); } else { var db = e.GetService <MikiDbContext>(); var command = await db.CustomCommands .FindAsync(guild.Id.ToDbLong(), commandName); if (command != null) { tokens = new Tokenizer().Tokenize(command.CommandBody); } } if (tokens != null) { var context = CreateContext(e); e.Channel.QueueMessage(new Parser(tokens).Parse(context)); } }
public async ValueTask CheckAsync(IDiscordMessage data, IMutableContext e, Func <ValueTask> next) { if (e == null) { return; } if (e.GetMessage().Type != DiscordMessageType.DEFAULT) { return; } var channel = e.GetChannel(); if (!(channel is IDiscordGuildChannel guildChannel)) { return; } var guild = await guildChannel.GetGuildAsync(); var cache = e.GetService <IExtendedCacheClient>(); IEnumerable <Token> tokens = null; string[] args = e.GetMessage().Content.Substring(e.GetPrefixMatch().Length).Split(' '); string commandName = args.FirstOrDefault().ToLowerInvariant(); var cachePackage = await cache.HashGetAsync <ScriptPackage>( CommandCacheKey, commandName + ":" + guild.Id); if (cachePackage != null) { tokens = ScriptPacker.Unpack(cachePackage); } else { var db = e.GetService <IUnitOfWork>(); var repository = db.GetRepository <CustomCommand>(); var command = await repository.GetAsync((long)guild.Id, commandName); if (command != null) { tokens = new Tokenizer().Tokenize(command.CommandBody); } } if (tokens != null) { var context = CreateContext(e); e.GetChannel().QueueMessage(e, null, new Parser(tokens).Parse(context)); } }
private void RunTest(ScriptTestData test, bool pack) { var result = Tokenizer.Parse(test.Script); _output.WriteLine(test.Path); _output.WriteLine(new string('-', test.Path.Length)); var tokenLength = result.Tokens.Max(t => t.TokenType.ToString().Length) + 2; var sb = new StringBuilder(); if (result.Warnings.Count > 0) { _output.WriteLine("Warnings:"); foreach (var warning in result.Warnings) { _output.WriteLine("[" + warning.Range.StartLine + ":" + warning.Range.StartColumn + "] " + warning.Message); } _output.WriteLine(""); } var tokens = result.Tokens; _output.WriteLine("Tokens:"); foreach (var token in tokens) { var type = token.TokenType.ToString(); sb.Append(type); if (token.Value != null) { sb.Append(new string(' ', tokenLength - type.Length)); sb.Append('"'); sb.Append(token.Value); sb.Append('"'); } _output.WriteLine(sb.ToString()); sb.Clear(); } if (pack) { var package = ScriptPacker.Pack(tokens); _output.WriteLine(""); _output.WriteLine("Packaged:"); _output.WriteLine(ScriptPacker.ToString(package, true)); tokens = ScriptPacker.Unpack(package); } Dictionary <string, object> context; if (test.Model == null) { context = new Dictionary <string, object>(); } else { var deserializer = new YamlDotNet.Serialization.Deserializer(); context = deserializer.Deserialize <Dictionary <string, object> >(test.Model); } var parser = new Parser.Parser(tokens); var actual = parser.Parse(context); if (test.Expected != null) { Assert.Equal(test.Expected, actual); } }