Exemple #1
0
        private void HandleMessage(NewMessage newMessage)
        {
            if (newMessage.user == null)             //The message is from us, ignore
            {
                newMessage.user = SlackBot.MySelf.id;
            }
            //return;

            SlackBotCommand command;

            if (!SlackBotCommand.GetCommand(newMessage, SlackBot, out command))
            {
                return;
            }

            // DM to slackbot doesn't have a channel.
            if (command.Channel == null)
            {
                return;
            }

            if (CommandTypeRegistry.HasHandler(command.Name))
            {
                PrivilegeLevel privilegeLevel = CommandTypeRegistry.GetCommandType(command.Name).GetPrivilegeLevel();
                if ((int)GetUserPrivilege(command.User.name) < (int)privilegeLevel)
                {
                    Reply(command, "http://i.imgur.com/egUwx5Q.gif");
                    Reply(command, "You better check your privilege!");
                    return;
                }

                if (!CommandHandlerRegistry.HasCommand(command))
                {
                    CommandHandlerRegistry.RegisterCommand(command, this);
                }

                ICommandHandler handler = CommandHandlerRegistry.GetCommand(command);
                if (!handler.Execute(command))
                {
                    CommandHandlerRegistry.DeRegisterCommand(command);
                }
            }
            else
            {
                Reply(command, ":clippy: \"Were you trying to run this command?\"");
                Reply(command, string.Format("@badjob {0} not knowing the commands.", command.User.name));
            }
        }
        public void RegisterCommand(SlackBotCommand command, SlackBotV3 slackBot)
        {
            ICommandType    commandType    = CommandTypeRegistry.GetCommandType(command.Name);
            ICommandHandler commandHandler = commandType.MakeCommandHandler(slackBot);

            CommandContainer[GetCommandKey(command)] = commandHandler;
        }
        private int GetCommandKey(SlackBotCommand command)
        {
            int          commandId = CommandTypeRegistry.GetCommandId(command.Name);
            CommandScope scope     = CommandTypeRegistry.GetCommandType(command.Name).GetCommandScope();

            object preKey = null;

            switch (scope)
            {
            case CommandScope.Global:
                preKey = commandId; break;

            case CommandScope.Channel:
                preKey = new Tuple <int, string>(commandId, command.Channel.id); break;

            case CommandScope.User:
                preKey = new Tuple <int, string, string>(commandId, command.Channel.id, command.User.id); break;
            }

            return(preKey.GetHashCode());
        }