Esempio n. 1
0
 public RuntimeCommandEvent(ICommandEvent commandEvent) : base(commandEvent)
 {
     CheckCommand     = commandEvent?.CheckCommand;
     Cooldown         = commandEvent.Cooldown;
     GuildPermissions = commandEvent?.GuildPermissions;
     ProcessCommand   = commandEvent?.ProcessCommand;
 }
Esempio n. 2
0
 public RuntimeCommandEvent(ICommandEvent commandEvent) : base(commandEvent)
 {
     Aliases          = commandEvent.Aliases;
     Cooldown         = commandEvent.Cooldown;
     GuildPermissions = commandEvent?.GuildPermissions;
     ProcessCommand   = commandEvent?.ProcessCommand;
     CommandPool      = commandEvent?.CommandPool;
 }
Esempio n. 3
0
        public ConsoleIn(ProcessCommandDelegate commandProcessor)
        {
            base.Multiline  = false;
            base.ReadOnly   = false;
            base.ScrollBars = ScrollBars.Horizontal;
            this.KeyPress  += new System.Windows.Forms.KeyPressEventHandler(this.KeyPressHandler);
            this.KeyDown   += new System.Windows.Forms.KeyEventHandler(this.KeyHandler);

            this.commandProcessor = commandProcessor;
            this.history          = new List <string>(HISTORY_SIZE);
            historyIdx            = 0;
        }
Esempio n. 4
0
        public ConsoleIn(ProcessCommandDelegate commandProcessor)
        {
            base.Multiline = false;
            base.ReadOnly = false;
            base.ScrollBars = ScrollBars.Horizontal;
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.KeyPressHandler);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyHandler);

            this.commandProcessor = commandProcessor;
            this.history = new List<string>(HISTORY_SIZE);
            historyIdx = 0;
        }
Esempio n. 5
0
        private async Task <bool> TryProcessCommand(ProcessCommandDelegate cmd, EventContext context)
        {
            bool success = false;
            await MeruUtils.TryAsync(async() =>
            {
                Log.Message($"Do we get here? {Name}");
                await cmd(context);
                success = true;
            },
                                     async (ex) =>
            {
                Log.ErrorAt(Name, ex.Message + "\n" + ex.StackTrace);
                await eventSystem.OnCommandError(ex, this, context.message);
            });

            return(success);
        }
Esempio n. 6
0
        private async Task <bool> TryProcessCommand(ProcessCommandDelegate cmd, EventContext context)
        {
            bool success = false;

            try
            {
                await cmd(context);

                success = true;
            }
            catch (Exception ex)
            {
                Log.ErrorAt(Name, ex.Message + "\n" + ex.StackTrace);
                await eventSystem.OnCommandError(ex, this, context.message);
            }
            return(success);
        }
Esempio n. 7
0
        // --------------------------------------------------------------------------------------------------
        #region Constructor

        public CommandManager(IRServerInfo server = null, BlastIrDelegate BlastFunc = null, LearnIrDelegate LearnIrFunc = null, ProcessCommandDelegate processCommand = null, bool script = false)
        {
            InitializeComponent();

            IRServer = server;
            _BlastFunc = BlastFunc;
            _LearnIrFunc = LearnIrFunc;
            _ProcessCommand = processCommand;

            _showGeneralCmds = true;
            _showScriptCmds = script;
            _showMacroCmds = true;
            _showMediaPortalCmds = false;
            _showServerCmds = BlastFunc != null && LearnIrFunc != null;
            _macroCommands = null;

            learnIRCommandToolStripMenuItem.Enabled = _LearnIrFunc != null;

            PopulateCommandList();
        }
Esempio n. 8
0
        // --------------------------------------------------------------------------------------------------
        #region Constructor

        public CommandManager(IRServerInfo server = null, BlastIrDelegate BlastFunc = null, LearnIrDelegate LearnIrFunc = null, ProcessCommandDelegate processCommand = null, bool script = false)
        {
            InitializeComponent();

            IRServer        = server;
            _BlastFunc      = BlastFunc;
            _LearnIrFunc    = LearnIrFunc;
            _ProcessCommand = processCommand;

            _showGeneralCmds     = true;
            _showScriptCmds      = script;
            _showMacroCmds       = true;
            _showMediaPortalCmds = false;
            _showServerCmds      = BlastFunc != null && LearnIrFunc != null;
            _macroCommands       = null;

            learnIRCommandToolStripMenuItem.Enabled = _LearnIrFunc != null;

            PopulateCommandList();
        }
Esempio n. 9
0
        public async Task Check(IDiscordMessage e, ICommandHandler c, string identifier = "")
        {
            Log.Message($"Entering {Name}!");
            string        command    = e.Content.Substring(identifier.Length).Split(' ')[0];
            string        args       = "";
            List <string> allAliases = new List <string>();

            string[] arguments = new string[0];

            if (e.Content.Split(' ').Length > 1)
            {
                args      = e.Content.Substring(e.Content.Split(' ')[0].Length + 1);
                arguments = args.Split(' ');
            }

            if (Module != null)
            {
                if (Module.Nsfw && !e.Channel.Nsfw)
                {
                    return;
                }
            }

            if (Aliases != null)
            {
                allAliases.AddRange(Aliases);
                allAliases.Add(Name);
            }

            if (!await IsEnabled(e.Channel.Id))
            {
                Log.WarningAt(Name, " is disabled");
                return;
            }

            if (IsOnCooldown(e.Author.Id))
            {
                Log.WarningAt(Name, " is on cooldown");
                return;
            }

            if (GuildPermissions.Count > 0)
            {
                foreach (DiscordGuildPermission g in GuildPermissions)
                {
                    if (!e.Author.HasPermissions(e.Channel, g))
                    {
                        await e.Channel.SendMessage($"Please give me the guild permission `{g}` to use this command.");

                        return;
                    }
                }
            }

            ProcessCommandDelegate targetCommand = ProcessCommand;

            if (arguments.Length > 0)
            {
                if (CommandPool.ContainsKey(arguments[0]))
                {
                    targetCommand = CommandPool[arguments[0]];
                    args          = args.Substring((arguments[0].Length == args.Length) ? arguments[0].Length : arguments[0].Length + 1);
                }
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            EventContext context = new EventContext();

            context.commandHandler = c;
            context.arguments      = args;
            context.message        = e;

            Log.Message($"Starting Command!!! {Name}");

            if (await TryProcessCommand(targetCommand, context))
            {
                await eventSystem.OnCommandDone(e, this);

                TimesUsed++;
                Log.Message($"{Name} called from {e.Guild.Name} in {sw.ElapsedMilliseconds}ms");
            }
            sw.Stop();
            Log.Message($"Leaving {Name}");
        }
Esempio n. 10
0
 public ICommandEvent Default(ProcessCommandDelegate command)
 {
     ProcessCommand = command;
     return(this);
 }
Esempio n. 11
0
 public ICommandEvent On(string args, ProcessCommandDelegate command)
 {
     CommandPool.Add(args, command);
     return(this);
 }
Esempio n. 12
0
        // TODO: clean up
        public async Task Check(EventContext e, string identifier = "")
        {
            string        command   = e.message.Content.Substring(identifier.Length).Split(' ')[0];
            string        args      = "";
            List <string> arguments = new List <string>();

            if (e.message.Content.Split(' ').Length > 1)
            {
                args = e.message.Content.Substring(e.message.Content.Split(' ')[0].Length + 1);
                arguments.AddRange(args.Split(' '));
                arguments = arguments
                            .Where(x => !string.IsNullOrWhiteSpace(x))
                            .ToList();
            }

            if (Module != null)
            {
                if (Module.Nsfw && !(await e.message.GetChannelAsync()).IsNsfw)
                {
                    throw new ChannelNotNsfwException();
                }
            }

            if (IsOnCooldown(e.message.Author.Id))
            {
                Log.WarningAt(Name, " is on cooldown");
                return;
            }

            if (GuildPermissions.Count > 0)
            {
                foreach (GuildPermission g in GuildPermissions)
                {
                    if (!(await(await e.message.GetChannelAsync() as IDiscordGuildChannel).GetPermissionsAsync(e.message.Author as IDiscordGuildUser)).HasFlag(g))
                    {
                        await(await e.message.GetChannelAsync()).SendMessageAsync($"Please give me the guild permission `{g}` to use this command.");
                        return;
                    }
                }
            }

            ProcessCommandDelegate targetCommand = ProcessCommand;

            if (arguments.Count > 0)
            {
                if (CommandPool.ContainsKey(arguments[0]))
                {
                    targetCommand = CommandPool[arguments[0]];
                    args          = args.Substring((arguments[0].Length == args.Length) ? arguments[0].Length : arguments[0].Length + 1);
                }
            }

            if (e.Channel is IDiscordGuildChannel c)
            {
                e.Guild = await c.GetGuildAsync();
            }

            e.Arguments = new Args(args);

            await targetCommand(e);
        }
Esempio n. 13
0
        // --------------------------------------------------------------------------------------------------
        #region Constructor


        /// <summary>
        /// Creates a Macro Editor windows form.
        /// </summary>
        /// <param name="name">The name of an existing macro (default: new macro).</param>
        public MacroEditor(string name = "", IRServerInfo server = null, BlastIrDelegate blast = null, LearnIrDelegate learnIr=null, ProcessCommandDelegate processCommand=null, bool insertionEnabled = true)
        {
            if (name==null)  name = "";

            _insertionEnabled = insertionEnabled;
            _editionEnabled = !IsOpen(name);
            _OpenInstances.Add(name.ToLower());

            _name = name;
            _server = server;
            _blast = blast;
            _learnIr = learnIr;
            _ProcessCommand = processCommand;

            InitializeComponent();

            textBoxName.Text = name;
            buttonTest.Enabled = _ProcessCommand != null;
            buttonOK.Visible = _insertionEnabled || _editionEnabled;
            buttonOK.Enabled = _insertionEnabled;
            _MacroNameValid = name != "";
            buttonShortcut.Enabled = _MacroNameValid;
            if (_editionEnabled && !_insertionEnabled || _name == "")
            {  // Show save first
                buttonOK.Enabled = false;
                buttonOK.Text = "Save";
                this.buttonOK.Image = global::IrssUtils.Properties.Resources.Save;
            }
            else
            {
                buttonOK.Enabled = _insertionEnabled;
            }

            if (_editionEnabled)
            {
                InitializeCommandManager();
            } 
            else 
            {
                // Relayout for Read-only mode
                labelInvalid.Text = "Macro is already open for edition";
                labelInvalid.Show();
                textBoxName.Enabled = false;

                groupBoxCommandSequence.Controls.Remove(splitContainerMain);
                groupBoxCommandSequence.Controls.Add(panelActions);
                this.MinimumSize = new System.Drawing.Size(310, this.Height);
                this.Width = 350;
            }

            if (_name == "") return;

            try
            {
                string fileName = Path.Combine(FolderMacros, name + Common.FileExtensionMacro);
                string[] commands = IrssMacro.ReadFromFile(fileName);
                foreach (var cmd in commands)
                {
                    InsertCommand(cmd); 
                }
                
            }
            catch (Exception ex)
            {
                IrssLog.Error(ex);
                MessageBox.Show(this, ex.Message, "Failed to load macro: " + name + Common.FileExtensionMacro, MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxName.Text = "";
            }

            _editedMacro = false;
        }
Esempio n. 14
0
        public async Task Check(IDiscordMessage e, ICommandHandler c, string identifier = "")
        {
            string        command    = e.Content.Substring(identifier.Length).Split(' ')[0];
            string        args       = "";
            List <string> allAliases = new List <string>();
            List <string> arguments  = new List <string>();

            if (e.Content.Split(' ').Length > 1)
            {
                args = e.Content.Substring(e.Content.Split(' ')[0].Length + 1);
                arguments.AddRange(args.Split(' '));
                arguments = arguments
                            .Where(x => !string.IsNullOrWhiteSpace(x))
                            .ToList();
            }

            if (Module != null)
            {
                if (Module.Nsfw && !e.Channel.Nsfw)
                {
                    return;
                }
            }

            if (Aliases != null)
            {
                allAliases.AddRange(Aliases);
                allAliases.Add(Name);
            }

            if (IsOnCooldown(e.Author.Id))
            {
                Log.WarningAt(Name, " is on cooldown");
                return;
            }

            if (GuildPermissions.Count > 0)
            {
                foreach (DiscordGuildPermission g in GuildPermissions)
                {
                    if (!e.Author.HasPermissions(e.Channel, g))
                    {
                        await e.Channel.SendMessageAsync($"Please give me the guild permission `{g}` to use this command.");

                        return;
                    }
                }
            }

            ProcessCommandDelegate targetCommand = ProcessCommand;

            if (arguments.Count > 0)
            {
                if (CommandPool.ContainsKey(arguments[0]))
                {
                    targetCommand = CommandPool[arguments[0]];
                    args          = args.Substring((arguments[0].Length == args.Length) ? arguments[0].Length : arguments[0].Length + 1);
                }
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            EventContext context = new EventContext();

            context.commandHandler = c;
            context.arguments      = args;
            context.message        = e;

            if (await TryProcessCommand(targetCommand, context))
            {
                await eventSystem.OnCommandDone(e, this, true, sw.ElapsedMilliseconds);

                TimesUsed++;
                Log.Message($"{Name} called by {e.Author.Username}#{e.Author.Discriminator} [{e.Author.Id}] from {e.Guild.Name} in {sw.ElapsedMilliseconds}ms");
            }
            else
            {
                await eventSystem.OnCommandDone(e, this, false, sw.ElapsedMilliseconds);
            }
            sw.Stop();
        }
Esempio n. 15
0
        // --------------------------------------------------------------------------------------------------
        #region Constructor


        /// <summary>
        /// Creates a Macro Editor windows form.
        /// </summary>
        /// <param name="name">The name of an existing macro (default: new macro).</param>
        public MacroEditor(string name = "", IRServerInfo server = null, BlastIrDelegate blast = null, LearnIrDelegate learnIr = null, ProcessCommandDelegate processCommand = null, bool insertionEnabled = true)
        {
            if (name == null)
            {
                name = "";
            }

            _insertionEnabled = insertionEnabled;
            _editionEnabled   = !IsOpen(name);
            _OpenInstances.Add(name.ToLower());

            _name           = name;
            _server         = server;
            _blast          = blast;
            _learnIr        = learnIr;
            _ProcessCommand = processCommand;

            InitializeComponent();

            textBoxName.Text       = name;
            buttonTest.Enabled     = _ProcessCommand != null;
            buttonOK.Visible       = _insertionEnabled || _editionEnabled;
            buttonOK.Enabled       = _insertionEnabled;
            _MacroNameValid        = name != "";
            buttonShortcut.Enabled = _MacroNameValid;
            if (_editionEnabled && !_insertionEnabled || _name == "")
            {  // Show save first
                buttonOK.Enabled    = false;
                buttonOK.Text       = "Save";
                this.buttonOK.Image = global::IrssUtils.Properties.Resources.Save;
            }
            else
            {
                buttonOK.Enabled = _insertionEnabled;
            }

            if (_editionEnabled)
            {
                InitializeCommandManager();
            }
            else
            {
                // Relayout for Read-only mode
                labelInvalid.Text = "Macro is already open for edition";
                labelInvalid.Show();
                textBoxName.Enabled = false;

                groupBoxCommandSequence.Controls.Remove(splitContainerMain);
                groupBoxCommandSequence.Controls.Add(panelActions);
                this.MinimumSize = new System.Drawing.Size(310, this.Height);
                this.Width       = 350;
            }

            if (_name == "")
            {
                return;
            }

            try
            {
                string   fileName = Path.Combine(FolderMacros, name + Common.FileExtensionMacro);
                string[] commands = IrssMacro.ReadFromFile(fileName);
                foreach (var cmd in commands)
                {
                    InsertCommand(cmd);
                }
            }
            catch (Exception ex)
            {
                IrssLog.Error(ex);
                MessageBox.Show(this, ex.Message, "Failed to load macro: " + name + Common.FileExtensionMacro, MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxName.Text = "";
            }

            _editedMacro = false;
        }