private async Task MessageReceivedInner(IMessage msg) { Session session = CreateSession(((IGuildChannel)msg.Channel).Guild); string cmdName = msg.Content.Substring(session.CommandPrefix.Length); int ix = cmdName.IndexOf(' '); if (ix != -1) { cmdName = cmdName.Remove(ix); } cmdName = cmdName.ToLowerInvariant(); try { ICmd cmd = HardCmds.ContainsKey(cmdName) ? (ICmd)HardCmds[cmdName] : session.Cmds.ContainsKey(cmdName) ? session.Cmds[cmdName] : null; if (cmd != null) { await msg.Channel.TriggerTypingAsync(); CmdContext ctx = new CmdContext(msg, session, data.AdvancedUsers.Contains(msg.Author.Id)); string res = cmd.Run(ctx); if (ctx.Result == CmdContext.CmdError.None) { bool empty = string.IsNullOrWhiteSpace(res); if (!empty || !ctx.Processed) { if (!empty && res.Length > 2000) { res = res.Remove(2000); } await ctx.Channel.SendMessageAsync(string.IsNullOrWhiteSpace(res)?ctx.GetString("ret_empty_cmd") : res); } } else if (ctx.Result == CmdContext.CmdError.NoAccess) { await msg.Channel.SendMessageAsync(ctx.GetString("err_notadmin")); } else if (ctx.Result == CmdContext.CmdError.ArgCount) { await msg.Channel.SendMessageAsync(ctx.GetString("err_params")); } //else if (ctx.Result == CmdContext.CmdError.BadFormat) // await msg.Channel.SendMessageAsync(ctx.GetString("err_badformat")); } } catch (Exception ex) { Log(ex); await msg.Channel.SendMessageAsync(session.GetString("err_generic") + ": " + ex.Message); } }
public string Run(CmdContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } string content; lock (this) content = Content; try { if (Type == CmdType.Switch) { return(new SwitchEvaluator(context).Evaluate(content) ?? "Switch " + context.GetString("ret_empty")); } else if (Type == CmdType.Lua) { return(Sandbox.Lua.Execute(content, context)); } else if (Type == CmdType.CSharp) { return(Sandbox.CS.Execute(content, context)); } else { return(ScriptEngine(content, context)); } } catch (OperationCanceledException ex) { if (ex.InnerException is Antlr4.Runtime.RecognitionException inner) { return(context.GetString("err_syntax", inner.OffendingToken.Text, inner.OffendingToken.Column, inner.OffendingToken.Line)); } // TODO: Wrap and throw Lua and C# compilation errors and catch them here else { return(context.GetString("err_syntax", null, null, null)); } } }