Esempio n. 1
0
 public static string Run(string name, string[] args, CmdContext context)
 {
     if (!Exists(name))
     {
         return(Utils.ErrorCode("UnknownFunc"));
     }
     try { return(vars[name](args, context)); }
     catch (IndexOutOfRangeException) { return(Utils.ErrorCode("ArgCount")); }
     catch (Exception ex) { return(ex.ErrorCode()); }
 }
Esempio n. 2
0
 public CmdContext(CmdContext copy, string args)
 {
     if (copy == null)
     {
         throw new ArgumentNullException(nameof(copy));
     }
     Args         = args;
     Server       = copy.Server;
     Channel      = copy.Channel;
     Sender       = copy.Sender;
     Session      = copy.Session;
     Processed    = false;
     AdvancedUser = copy.AdvancedUser;
 }
Esempio n. 3
0
        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);
            }
        }
Esempio n. 4
0
        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));
                }
            }
        }
Esempio n. 5
0
 public string Run(CmdContext ctx)
 {
     if (AdminOnly && (!ctx.Sender.GuildPermissions.Administrator || ctx.Sender.IsBot) && !ctx.AdvancedUser)
     {
         ctx.Result = CmdContext.CmdError.NoAccess;
         return(null);
     }
     else
     {
         string[] args = ctx.Args.Length == 0 ? new string[0] : ctx.Args.Split(new[] { ' ' }, ArgsMax, StringSplitOptions.RemoveEmptyEntries);
         if (args.Length < ArgsMin)
         {
             ctx.Result = CmdContext.CmdError.ArgCount;
             return(null);
         }
         else
         {
             return(Func(args, ctx));
         }
     }
 }
Esempio n. 6
0
 public static string ScriptEngine(string content, CmdContext context) => Unescape(new ScriptEvaluator(context).Evaluate(content));