Beispiel #1
0
        public void RegisterCommand(string name, string description, Func <BotMessage, int, int> handler, Func <BotMessage, bool> filter = null, bool allowShortcut = false)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Invalid command name");
            }

            var cmd = new BotCommand(name, description, handler, filter, allowShortcut);

            _commands[name] = cmd;
        }
Beispiel #2
0
        private void ProcessMessage(BotMessage msg)
        {
            var text = msg.Text;

            string queue = null;

            if (msg.Visibility == MessageVisibility.Public && !_groupList.ContainsKey(msg.Sender.ID))
            {
                var content = $"{msg.Sender.Platform} => {msg.Sender.ID} / {msg.Sender.Handle}";
                Console.WriteLine("Listening in group: " + content);
                _groupList[msg.Sender.ID] = content;
            }

            BotCommand cmd    = null;
            var        prefix = _connections[msg.Sender.Platform].GetCommandPrefix();

            if (_cmds.ContainsKey(msg.Sender.ID))
            {
                cmd = _cmds[msg.Sender.ID];
            }
            else
            if (text.StartsWith(prefix))
            {
                var temp = text.Substring(prefix.Length).Split(new char[] { ' ' }, 2);

                var key = temp[0];

                if (_commands.ContainsKey(key))
                {
                    cmd = _commands[key];

                    if (temp.Length == 2 && cmd.AllowShortcut)
                    {
                        queue = temp[1];
                    }
                }
            }

            if (cmd != null)
            {
                var state = _state.ContainsKey(msg.Sender.ID) ? _state[msg.Sender.ID] : 0;
                if (state != 0 || (cmd.filter == null || cmd.filter(msg)))
                {
                    if (queue != null && state == 0)
                    {
                        msg.Text = queue;
                        state    = 1;
                    }

                    state = cmd.handler(msg, state);
                    if (state > 0)
                    {
                        _state[msg.Sender.ID] = state;
                        _cmds[msg.Sender.ID]  = cmd;
                    }
                    else
                    {
                        try
                        {
                            _state.Remove(msg.Sender.ID);
                            _cmds.Remove(msg.Sender.ID);
                        }
                        catch
                        {
                            // ignore
                        }
                    }
                }
                else
                {
                    OnPermissionFailedForCommand(msg);
                }

                return;
            }

            OnMessage(msg);

            // update last seen
            var lastSeen = GetLastSeen(msg.Sender);
            var diff     = (DateTime.UtcNow - lastSeen).TotalHours;

            if (diff > 1)
            {
                var timestamp = DateTime.UtcNow.Ticks;
                Times.Set(msg.Sender, timestamp.ToString());
            }
        }